记一次解析特别老版本Excel文件

前言

        公司业务需求,需要定时任务去读取一个excel文件,然后更新数据到数据库里面,都是这个excel文件太老了,用poi解析报错:

Exception in thread "main" org.apache.poi.hssf.OldExcelFormatException: The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003) 

意思也就是poi只支持BIFF8,这个文件比较老poi不支持。

然后在网上找了很多,都是坑,还是有靠谱的博主发了解决方法.

可以参考:记一次解析旧版本Excel文件_oldexcelextractor-CSDN博客

大致的思路是正确的,具体的我也没有试. 我这里提供几种思路

方法一:

利用一些开源的接口,可以将老版本的xls转换为xlsx;

https://www.freeconvert.com 这个网站有提供的对应的api接口,但是要钱,这等于要我的命

方法二:

模拟另存为,通过代码模拟excel文件另存为最新的xlsx文件,然后在将文件进行读取操作;

方法三:

通过jxl读取文件.然后将内容写入最新版的excel文件,

第一步导入依赖:

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.5.7</version>
</dependency>

这里还需要引用poi,我这里用的是easyexcel,里面包含了poi的依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.2</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.3.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.3.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.3.0</version>
</dependency>

第二步:然后写了一个工具类,封装方法

public class OldExcelUtils {

    public static void convertExcel(String sourceFilePath, String targetFilePath) throws BiffException, IOException{
        // 创建输入流并设置编码格式为 GBK
        FileInputStream is = new FileInputStream(new File(sourceFilePath));
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding("GBK");
        Workbook workbook = Workbook.getWorkbook(is, workbookSettings);
        // 获取第一个工作表
        Sheet sheet = workbook.getSheet(0);
        // 写入 xlsx 格式
        org.apache.poi.ss.usermodel.Workbook poiWorkbook = new XSSFWorkbook();
        org.apache.poi.ss.usermodel.Sheet poiSheet = poiWorkbook.createSheet();
        // 遍历读取数据并写入新文件
        for (int row = 0; row < sheet.getRows(); row++) {
            Row poiRow = poiSheet.createRow(row);
            for (int column = 0; column < sheet.getColumns(); column++) {
                Cell cell = sheet.getCell(column, row);
                //String contents = new String(cell.getContents().getBytes("ISO-8859-1"), "GBK");
                String contents = cell.getContents();
                poiRow.createCell(column).setCellValue(contents);
            }
        }
        FileOutputStream outputStream = new FileOutputStream(targetFilePath);
        poiWorkbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }

    public static void main(String[] args) throws Exception {
        // xls文件绝对路径
        String xlsFilePath = "E:\\xuan\\file\\R06_item_details.xls";
        // 生成的xlsx文件绝对路径
        String xlsxFilePath = "E:\\xuan\\file\\R06_item_details.xlsx";
        convertExcel(xlsFilePath, xlsxFilePath);
    }
}

生成的excel文件可以用poi去解析了,目前遇到的坑是:读取的excel文件中文乱码,已在代码里面解决,

还有什么问题可以留言

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值