public void importExcel() {
System.out.println("--------Begin---------");
/*UploadFile uploadFile = this.getFile("excelFile");
File file = uploadFile.getFile()*/
File file = new File("D://excel/test.xls");
List<String[]> lists = ExcelUtil.getExcelData(file).get(0);
for (String[] strings : lists) {
if (strings[0] != null && strings[0] != "") {
String EXCEL_ID = strings[0];
String NAME = strings[1];
String SEX = strings[2];
String PWD = strings[3];
String DATE = strings[4];
ExcelInfo excelInfo = new ExcelInfo();
excelInfo.set("EXCEL_ID",EXCEL_ID);
excelInfo.set("NAME",NAME);
excelInfo.set("SEX",SEX);
excelInfo.set("PWD",PWD);
excelInfo.set("DATE",DATE);
System.out.println(excelInfo.toString());
/*excelInfo.save();*/
}
}
}
public static List<List<String[]>> getExcelData(File file) {
HSSFWorkbook workbook;
List<List<String[]>> dataList = new ArrayList<List<String[]>>();
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet;
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
sheet = workbook.getSheetAt(i);
List<String[]> rows = new ArrayList<String[]>();
int colsnum = 0;
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
HSSFRow row = sheet.getRow(j);
if (row != null) {
colsnum = sheet.getRow(1).getPhysicalNumberOfCells();
String[] cols = new String[colsnum];
for (int k = 0; k < colsnum; k++) {
Cell cell = row.getCell(k);
if (cell != null) {
if (cell.getCellTypeEnum().equals("NUMERIC")) {
if (String.valueOf(cell.getNumericCellValue()).matches(".*[E|e].*")) {
DecimalFormat df = new DecimalFormat("#.#");
df.setMaximumFractionDigits(2);
cols[k] = df.format(row.getCell(k).getNumericCellValue());
} else if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
cols[k] = formater.format(date);
} else {
cols[k] = (cell + "").trim();
}
} else {
cols[k] = (cell + "").trim();
}
} else {
cols[k] = "";
}
}
rows.add(cols);
}
}
dataList.add(rows);
}
} catch (IOException e) {
e.printStackTrace();
}
return dataList;
}
原文:https://blog.csdn.net/qx30339/article/details/54020308