项目是springboot+mybatis的项目框架
首先pom文件引包 我使用的版本是3.16版本,xlsx-streamer包使用的1.2.0 本地引入的(如果maven无法下载,可以选择本地引入,下载好jar包之后)
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>1.2.0</version>
</dependency>
包引进来之后下面看详细代码。
1.读取xls和xlsx,
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(yktFilePath);
HSSFWorkbook hssfWorkbook = null;
XSSFWorkbook xssfWorkbook = null;
if (fileType.equals("xls")) {
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fileInputStream);
hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
}else{
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
2.处理大文件的时候只能以流式方式读取 否则会出现内存溢出。样例:
for (Row row : sheetAt) {
// 存放行的数据
rowlist = new ArrayList();
// 当前行
int rowNum = row.getRowNum();
// 新文件
Row create_row = xlsxsh.createRow(rowNum);
if (0 == rowNum) {
// 得到标题栏
titlelist = dealwithExcel.readExcelTitlebyrow(row);
// 校验标题-不通过则跳出循环返回
boolean boo = checkyktTitle(titlelist, titileArr);
if (!boo) {
// 1表示标题校验不通过
xlsxmap.put("title", 1);
break;
} else {
for (int i = 0; i < titlelist.length; i++) {
Cell cell = row.getCell(i);
// 列号
int columnIndex = cell.getColumnIndex();
Cell create_cell = create_row.createCell(columnIndex);
create_cell.setCellStyle(titleStyle);
create_cell.setCellValue(titlelist[i]);
}
xlsxmap.put("title", 0);
continue;
}
}
System.out.println("开始遍历第" + row.getRowNum() + "行数据:");
int length = titlelist.length;
// 读取数据
for (int i = 0; i < titlelist.length; i++) {
String column = titlelist[i];
Cell cell = row.getCell(i);
// 列号
int columnIndex = i;
// 新文件
Cell create_cell = create_row.createCell(columnIndex);
// 单元格值
String stringCellValue = getCellValue(cell);
switch (column) {
case "姓名":
if (!ExcelValidUtil.isNotNull(stringCellValue)) {
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue("此列信息必填!");
errRows.add(rowNum);// 添加需要保留的数据
}
break;
case "居民户编码":
if (!ExcelValidUtil.isNotNull(stringCellValue)) {
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue("此列信息必填!");
errRows.add(rowNum);// 添加需要保留的数据
}
break;
case "补贴项目名称":
if (!ExcelValidUtil.isNotNull(stringCellValue)) {
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue("此列信息必填!");
errRows.add(rowNum);// 添加需要保留的数据
} else if (!ExcelValidUtil.checkOneCardType(stringCellValue)) {
// 校验一卡通的项目类型
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue(stringCellValue + "(项目名称不对!)");
errRows.add(rowNum);
}
break;
case "发放金额(单位:元)":
if (!ExcelValidUtil.isNotNull(stringCellValue)) {
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue("此列信息必填!");
errRows.add(rowNum);// 添加需要保留的数据
} else if (!ExcelValidUtil.isNumber(stringCellValue)) {// 如果金额正确 记录某行某列
create_cell.setCellStyle(xslxStyle);
create_cell.setCellValue(stringCellValue + "(格式不正确!)");
errRows.add(rowNum);
}
break;
default:
break;
}
// 数据校验完毕
create_cell.setCellValue(stringCellValue);
rowlist.add(stringCellValue);
}
// 有效数据循环一行加一
userfulData += 1;
// 每行遍历完毕之后检查-这个是个校验方法
Map<String, Object> checkusefulmap = dealwithExcel.checkRowValue(rowlist, create_row, length,
clearCellStyle, errRows, rowNum, userfulData);
errRows = (Set<Integer>) checkusefulmap.get("error");
userfulData = (int) checkusefulmap.get("userfulData");
}
/**
* @param clearCellStyle 清除的格式样式
* @param length 列数
* @param create_row 当前行
* @param rowlist 当前行的数据
* @param errRows 校验出错的记录
* @param rowNum 当前行
* @param userfulData
* @return
* @Description:当前行的数据全部为空的时候清除该数据的校验格式
* void
* @throws
* @author yl
* @date 2018年4月27日 上午11:16:30
*/
public Map<String, Object> checkRowValue(List rowlist, Row create_row, int length, CellStyle clearCellStyle, Set<Integer> errRows, int rowNum, int userfulData) {
Set checkSet = new HashSet();
if (null!=rowlist&&rowlist.size()>0) {
for (int i = 0; i < rowlist.size(); i++) {
Object rowValueobj = rowlist.get(i);
if (null!=rowValueobj&&!"".equals(rowValueobj.toString())) {
//校验是有效的-跳出验证
rowlist.clear();
break;
}else {
checkSet.add(1);
continue;
}
}
if (checkSet.size()==1) {
errRows.remove(rowNum);
userfulData = userfulData-1;
//全部为空的情况当前行的校验无效-清除格式
for (int i = 0; i < length; i++) {
Cell ceratecell = create_row.getCell(i);
clearCellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
ceratecell.setCellStyle(clearCellStyle);
}
}
}
Map<String, Object> checkMap = new HashMap<String, Object>();
checkMap.put("error", errRows);
checkMap.put("userfulData", userfulData);
return checkMap;
}