主要是用到了POI包,循环遍历。重点是要判断cell.getCellType()也就是cell的类型,因为cell表格数据可能是数字,字符串,日期等。我是根据表的数据只判断了字符串和数字,有时候可能会有布尔类型,日期等。
// 解析xlsx
public List<Repeater> readText(String path) {
List<Repeater> reList = new ArrayList<Repeater>();
Repeater re = null;
try {
File file = new File(path);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStream is = new FileInputStream(path);
Workbook wb = null;
wb = new XSSFWorkbook(is);// 2007以上版本
// 得到第一个shell
Sheet sheetAt = wb.getSheetAt(0);
// 得到Excel的行数
int rows = sheetAt.getPhysicalNumberOfRows();
int cells = 0;
// 得到Excel的列数(前提是有行数)
if (rows > 1 && sheetAt.getRow(0) != null) {
cells = sheetAt.getRow(0).getPhysicalNumberOfCells();
}
// 循环Excel行数
for (int r = 1; r < rows; r++) {
Row row = sheetAt.getRow(r);
if (row == null) {
continue;
}
re = new Repeater();
// 循环Excel的列
for (int c = 0; c < cells; c++) {
Cell cell = row.getCell(c);
if (null != cell) {
re.setCreateDate(null);
re.setModifyDate(new Date());
if (c == 0) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String id = String.valueOf(cell
.getNumericCellValue());
re.setRepeaterId(id);
} else {
re.setRepeaterId(cell.getStringCellValue());
}
} else if (c == 1) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String rptname = String.valueOf(cell
.getNumericCellValue());
re.setRptname(rptname);
} else {
re.setRptname(cell.getStringCellValue());
}
} else if (c == 2) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String number = String.valueOf(cell
.getNumericCellValue());
re.setRptnumber(number);
} else {
re.setRptnumber(cell.getStringCellValue());
}
} else if (c == 3) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String equ = String.valueOf(cell
.getNumericCellValue());
re.setEquipid(equ);
} else {
re.setEquipid(cell.getStringCellValue());
}
} else if (c == 4) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String ven = String.valueOf(cell
.getNumericCellValue());
re.setVendername(ven);
} else {
re.setVendername(cell.getStringCellValue());
}
} else if (c == 5) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String cna = String.valueOf(cell
.getNumericCellValue());
re.setVindicname(cna);
} else {
re.setVindicname(cell.getStringCellValue());
}
} else if (c == 6) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String are = String.valueOf(cell
.getNumericCellValue());
re.setAreaname(are);
} else {
re.setAreaname(cell.getStringCellValue());
}
} else if (c == 7) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String ctr = String.valueOf(cell
.getNumericCellValue());
re.setCtrlarea(ctr);
} else {
re.setCtrlarea(cell.getStringCellValue());
}
} else if (c == 8) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String typ = String.valueOf(cell
.getNumericCellValue());
re.setRpttype(typ);
} else {
re.setRpttype(cell.getStringCellValue());
}
} else if (c == 9) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String ch = String.valueOf(cell
.getNumericCellValue());
re.setTypechina(ch);
} else {
re.setTypechina(cell.getStringCellValue());
}
} else if (c == 10) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String bt = String.valueOf(cell
.getNumericCellValue());
re.setBtschina(bt);
} else {
re.setBtschina(cell.getStringCellValue());
}
} else if (c == 11) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String slave = String.valueOf(cell
.getNumericCellValue());
re.setSlave(slave);
} else {
re.setSlave(cell.getStringCellValue());
}
} else if (c == 12) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String ph = String.valueOf(cell
.getNumericCellValue());
re.setCtrlphone(ph);
} else {
re.setCtrlphone(cell.getStringCellValue());
}
} else if (c == 13) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String flag = String.valueOf(cell
.getNumericCellValue());
re.setCtrlflag(flag);
} else {
re.setCtrlflag(cell.getStringCellValue());
}
} else if (c == 14) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String st = String.valueOf(cell
.getNumericCellValue());
re.setStatus(st);
} else {
re.setStatus(cell.getStringCellValue());
}
} else if (c == 15) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String ce = String.valueOf(cell
.getNumericCellValue());
re.setCellid(ce);
} else {
re.setCellid(cell.getStringCellValue());
}
} else if (c == 16) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String lo = String.valueOf(cell
.getNumericCellValue());
re.setLongitude(lo);
} else {
re.setLongitude(cell.getStringCellValue());
}
} else if (c == 17) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String la = String.valueOf(cell
.getNumericCellValue());
re.setLatitude(la);
} else {
re.setLatitude(cell.getStringCellValue());
}
} else if (c == 18) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String po = String.valueOf(cell
.getNumericCellValue());
re.setPostion(po);
} else {
re.setPostion(cell.getStringCellValue());
}
} else if (c == 19) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String name = String.valueOf(cell
.getNumericCellValue());
re.setOwnername(name);
} else {
re.setOwnername(cell.getStringCellValue());
}
} else if (c == 20) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String phone = String.valueOf(cell
.getNumericCellValue());
re.setOwnerphone(phone);
} else {
re.setOwnerphone(cell.getStringCellValue());
}
} else if (c == 21) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String con = String.valueOf(cell
.getNumericCellValue());
re.setConfirmed(con);
} else {
re.setConfirmed(cell.getStringCellValue());
}
}
}
}
reList.add(re);
}
// is.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
reList = null;
e.printStackTrace();
return reList;
}
return reList;
}