public static List
<List
<List
<String>>> getExcelData(XSSFWorkbook wb
){
int numSheets
= wb
.getNumberOfSheets();
List
<List
<List
<String>>> sheets
= new ArrayList<>();
for(int i
=0;i
<numSheets
;i
++){
XSSFSheet sheet
= wb
.getSheetAt(i
);
System
.out
.println(sheet
.getSheetName());
int numRows
= sheet
.getLastRowNum();
List
<List
<String>> rows
= new ArrayList<>();
for (int j
=0;j
<numRows
;j
++){
XSSFRow row
= sheet
.getRow(j
);
if (row
==null
){
break;
}
int numCells
= row
.getLastCellNum();
List
<String> cells
= new ArrayList<>();
for (int k
=0;k
<numCells
;k
++){
cells
.add(getValue(k
,row
));
}
rows
.add(cells
);
}
sheets
.add(rows
);
}
return sheets
;
}
public static String
getValue(int rowNum
, int colNum
, XSSFSheet sheet
){
XSSFRow row
= sheet
.getRow(rowNum
);
return getValue(colNum
, row
);
}
public static String
getValue(int colNum
, XSSFRow row
){
XSSFCell cell
= row
.getCell(colNum
);
if (cell
==null
){
return "";
}
String cellValue
= "";
CellType cellTypeEnum
= cell
.getCellTypeEnum();
try {
DecimalFormat df
= new DecimalFormat("0.00");
if (cell
.getCellTypeEnum() == CellType
.NUMERIC
) {
if (HSSFDateUtil
.isCellDateFormatted(cell
)) {
cellValue
= DateFormatUtils
.format(cell
.getDateCellValue(), "yyyy-MM-dd");
} else {
NumberFormat nf
= NumberFormat
.getInstance();
cellValue
= String
.valueOf(nf
.format(cell
.getNumericCellValue())).replace(",", "");
}
} else if (cell
.getCellTypeEnum() == CellType
.STRING
) {
cellValue
= String
.valueOf(cell
.getStringCellValue());
} else if (cell
.getCellTypeEnum() == CellType
.BOOLEAN
) {
cellValue
= String
.valueOf(cell
.getBooleanCellValue());
} else if (cell
.getCellTypeEnum() == CellType
.ERROR
) {
cellValue
= "错误类型";
} else if (cell
.getCellTypeEnum() == CellType
.FORMULA
) {
cellValue
= df
.format(cell
.getNumericCellValue());
} else {
cellValue
= "";
}
}catch (Exception e
){
logger
.error(e
.getMessage(),e
);
cellValue
= "-9999999999";
}
return cellValue
;
}
转载请注明原文地址: https://lol.8miu.com/read-7030.html