java excel 导入 搜集列 公式及校验 计算值
·
private List<List<String>> impHistoryExcelByCol(MultipartFile file, int index, int column, Map map) throws FileNotFoundException, IOException {
List<List<String>> alllist = new ArrayList<List<String>>();
// 构造 Workbook 对象,execelFile 是传入文件路径(获得Excel工作区)
Workbook book = null;
try {
// Excel 2007获取方法
book = new XSSFWorkbook(file.getInputStream());
} catch (Exception ex) {
try {
// Excel 2003获取方法
book = new HSSFWorkbook(file.getInputStream());
} catch (Exception e) {
throw e;
}
}
// 读取表格的第一个sheet页
Sheet sheet = book.getSheetAt(0);
// 定义 row、cell
Row row;
//String cell = "";
// 总共有多少行,从0开始
int totalRows = sheet.getLastRowNum();
//公式
FormulaEvaluator evaluator = book.getCreationHelper().createFormulaEvaluator();
// 总共有多少列,从0开始 sheet.getRow(1).getLastCellNum();此处写成1是为了兼容wps,因wps认不出第一行合并单元格后 以为是一列
int totalCells = sheet.getRow(1).getLastCellNum();
//totalCells = 15;
for (int j = column; j < totalCells; j++) {
//每一列的值的集合
List<String> arrList = new ArrayList<String>();
// 循环输出表格中的内容,首先循环取出行,再根据行循环取出列
for (int i = index; i <= totalRows; i++) {
row = sheet.getRow(i);
// 处理空行
if (row == null) {
continue;
}
// 处理空行
if (row.getCell(j) == null) {
arrList.add("");
continue;
}
String cellObj = getStringCellValue(row.getCell(j)).trim();
CellValue cellValue=new CellValue(0);
//增加公式判断
if(row.getCell(j).getCellType()==Cell.CELL_TYPE_FORMULA){
cellValue = evaluator.evaluate(row.getCell(j));
Double celldata = cellValue.getNumberValue();
cellObj = celldata.toString();
}
try {
if (i >= 2 && j >= 3 && !isInteger(cellObj)) {
System.out.println("行==" + i + ",列==" + j);
map.put("success", false);
return null;
}
} catch (Exception e) {
System.out.println("行==" + i + ",列==" + j);
map.put("success", false);
return null;
}
arrList.add(cellObj);
}
alllist.add(arrList);
}
return alllist;
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)