主è¦ç¨poi.jar å
ãå
å«ä¸¤ä¸ªjarå°±å¯ä»¥äºï¼poi-3.16.jarãpoi-ooxml-3.16.jar
主è¦æ¹æ³åä¸æ¥ï¼
/**
* filePath æ件路å¾
* unCaseRow è¦æé¤çè¡æ°ï¼ä»ä¸å¾ä¸ï¼
* unCaseLine è¦æé¤çåæ°ï¼ä»å·¦å¾å³ï¼
*/
public List<String[]> readExcel(String filePath, int unCaseRow, int unCaseLine) throws Exception {
Sheet sheet = null;
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
Workbook workBook = WorkbookFactory.create(inStream);
sheet = workBook.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
List<String[]> list = init(sheet, unCaseRow, unCaseLine);// éè¦æé¤è¡æ°
return list;
}
// åå§åè¡¨æ ¼ä¸çæ¯ä¸è¡ï¼å¹¶å¾å°æ¯ä¸ä¸ªåå
æ ¼çå¼
private List<String[]> init(Sheet sheet, int unCaseRow, int unCaseLine) {
int rowNum = sheet.getLastRowNum() + 1; // ä»é¶å¼å§
List<String[]> result = new ArrayList<String[]>();
String[] rowArr = null;
Row row = null;
Cell cell = null;
int rowLength = 0;
int rowIndex = 0;
String rowStr = null;
for (int i = unCaseRow; i < rowNum; i++) {
row = sheet.getRow(i);
// æ¯ææ°çä¸è¡ï¼å建ä¸ä¸ªæ°çLinkedList对象
rowLength = row.getLastCellNum();
rowIndex = 0;
rowArr = new String[LINECOUNT];
for (int j = unCaseLine; j < rowLength; j++) {
cell = row.getCell(j);
// è·ååå
æ ¼çå¼
rowStr = getCellValue(cell);
// å°å¾å°çå¼æ¾å
¥é¾è¡¨ä¸
rowArr[rowIndex++] = rowStr;
}
result.add(rowArr);
}
return result;
}
// è·ååå
æ ¼çå¼
@SuppressWarnings("deprecation")
private String getCellValue(Cell cell) {
String cellValue = "";
DataFormatter formatter = new DataFormatter();
if (cell != null) {
// å¤æåå
æ ¼æ°æ®çç±»åï¼ä¸åç±»åè°ç¨ä¸åçæ¹æ³
switch (cell.getCellType()) {
// æ°å¼ç±»å
case Cell.CELL_TYPE_NUMERIC:
// è¿ä¸æ¥å¤æ ï¼åå
æ ¼æ ¼å¼æ¯æ¥ææ ¼å¼
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
// æ°å¼
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
// å¤æåå
æ ¼æ¯å
¬å¼æ ¼å¼ï¼éè¦åä¸ç§ç¹æ®å¤çæ¥å¾å°ç¸åºçå¼
case Cell.CELL_TYPE_FORMULA: {
try {
cellValue = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
cellValue = String.valueOf(cell.getRichStringCellValue());
}
}
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
解ææ对象以åï¼ä¸è®ºæ¯æå
¥æ°æ®åºï¼è¿æ¯jspï¼é½æ¯ä¸æ ·çã
æå
¥æ°æ®åºï¼hibernateãmybatis
å¨jspæ¾ç¤ºï¼å¯¹è±¡å°è£
è¿listï¼å¨é¡µé¢æ¾ç¤ºlistã