Java 提高读取和持久保存Apache POI excel文件的速度

Java 提高读取和持久保存Apache POI excel文件的速度,java,mysql,apache,jpa,apache-poi,Java,Mysql,Apache,Jpa,Apache Poi,我有一个Excel文件,目前可以导入到mySQL数据库中,但我想稍微提高导入速度。我的读取Excel文件(每个文件中约40000行,10-15列)和持久化设置如下: *Java代码* public void importFileToDataBase(String file) { try { FileInputStream latestExcelFile = new FileInputStream(file); wb = Workbo

我有一个Excel文件,目前可以导入到mySQL数据库中,但我想稍微提高导入速度。我的读取Excel文件(每个文件中约40000行,10-15列)和持久化设置如下:

*Java代码*

public void importFileToDataBase(String file) {
        try {
            FileInputStream latestExcelFile = new FileInputStream(file);
            wb = WorkbookFactory.create(myFile);
        } catch (Exception ex) {
        }

        Sheet bigSheet = wb.getSheetAt(0);

        for (int i = 1; i <= bigSheet.getLastRowNum(); i++) {

            try {

                row = bigSheet.getRow(i);
                String bigSheetId = i + "";
                Date column1 = row.getCell(0).getDateCellValue();
                int column2 = (int) row.getCell(1).getNumericCellValue();
                int column3 = (int) row.getCell(3).getNumericCellValue();
                int column4 = (int) row.getCell(4).getNumericCellValue();
                int column5 = (int) row.getCell(5).getNumericCellValue();
                int column6 = (int) row.getCell(6).getNumericCellValue();
                int column7 = (int) row.getCell(7).getNumericCellValue();
                String column8 = row.getCell(9).toString();
                String column9 = row.getCell(11).toString();
                String column10 = row.getCell(12).toString();
                String column11 = row.getCell(13).toString();
                int column12 = (int) row.getCell(8).getNumericCellValue();
                Double column13 = row.getCell(10).getNumericCellValue();
                String column14 = row.getCell(2)
                        .toString();

                BigSheet mySheet = new BigSheet(
                        baseDataTableId, column1, column2, column3, column4,
                        column5, column6, column7, column8, column9,
                        column10, column11, column12, column13, column14);

                putInDatabase(mySheet);
            } catch (Exception ex) {}
        }
public void导入文件数据库(字符串文件){
试一试{
FileInputStream latestExcelFile=新的FileInputStream(文件);
wb=WorkbookFactory.create(myFile);
}捕获(例外情况除外){
}
Sheet bigSheet=wb.getSheetAt(0);

对于(int i=1;i您正在使用哪个数据库?在大多数情况下,批量更新将提高性能。有时,根据数据库的不同,提交每一行(例如500行)也会加快速度。使用mySql数据库-通过JPA持久化-目前运行正常,但我只需要快速获得它。您必须找出消耗时间最多的部分st.如果您已经识别了零件,例如使用探查器,您可以对其进行改进。为什么不将电子表格导出为CSV格式,然后编写一个简单的MySQL导入脚本?性能可能与读取电子表格无关,而是来自您未显示的插入(即putInDatabase()方法)。请评论,看看它是否运行得更快。正如Timo所说,SQL更新通常可以成批进行,从而大大提高了速度。