注:最近公司业务涉及到导出业务,遂使用poi-tl作为导出工具,但先一步的同事使用poi3.14版本,因为poi-tl需要配合poi使用,但是最低只支持poi3.16+版本,所以修改api语法并记录下他们的变更关系
需要注意的是:不仅仅是poi一个需要更新版本,用到的都要更新,其中poi要和poi-ooxml版本对应,然后选择对应的poi-tl版本
这里提供个poi版本和poil-tl版本不冲突的版本,其他的不是报方法没找到就是重复依赖还有什么要log4j支持的(我一个导入导出要什么日志,还是必须配置日志不然报错)
compile "org.apache.poi:poi:4.1.2"
implementation 'com.deepoove:poi-tl:1.9.1'
implementation group: 'org.apache.poi', name: 'ooxml-schemas', version: '1.4'
compile "org.apache.poi:poi-ooxml:4.1.2"
compile "org.apache.poi:poi:5.1.0"
compile "org.apache.poi:ooxml-schemas:1.4"
compile "org.apache.poi:poi-ooxml:5.1.0"
implementation 'com.deepoove:poi-tl:1.10.2'
旧:
InputStream in = file.getInputStream();
XSSFWorkbook wb = new XSSFWorkbook(in);
XSSFSheet sheet = wb.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
XSSFCell cell = null;
String mbid = "";
int TWO = 2;
for (int i = TWO; i <= lastRowNum; i++) {
XSSFRow rowValue = sheet.getRow(i);
cell = rowValue.getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
String name = rowValue.getCell(0).getStringCellValue();
}
新:
InputStream in = file.getInputStream();
Workbook wb = new XSSFWorkbook(in);
Sheet sheet = wb.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
String mbid = "";
int TWO = 2;
for (int i = TWO; i <= lastRowNum; i++) {
Row rowValue = sheet.getRow(i);
DataFormatter formatter = new DataFormatter();
String name = formatter.formatCellValue(rowValue.getCell(0))
}
对比:XSSFWorkbook 对象被换成Workbook,XSSFSheet 换成Sheet,XSSFRow 换成Row,需要注意的是更新版本后cell.setCellType(Cell.CELL_TYPE_STRING) 过时不能用了,会报错,需换成DataFormatter对象直接得数据,不需要再单独进行转换,详见以上代码