当前位置: 首页 > 知识库问答 >
问题:

为什么在我的情况下编写simple.xlsx的书非常慢?

柯骏
2023-03-14

我真的需要有人帮忙。我有非常非常奇怪的代码,即使只写2个单元格也要花35秒。(两倍于智能手机等设备)。

  1. 注:在计算过程中,这两种方法都没有问题。他们挺快的。不需要调试这些
    null
static Double compareStrings(String stringA, String stringB) {===>to compute distance
    
    null
public static void writeAndCloseALLStream() throws IOException {
        /*
         * System.out.println ("excelInputFile"+excelInputFile);
         * System.out.println ("excelOutputFile"+excelOuputFile);
         */
        
        
        fis.close();
        //workbook.write(fos);

        SXSSFWorkbook book = new SXSSFWorkbook(workbook,100,true); 
         book.setCompressTempFiles(true);
        SXSSFSheet sheetSXSS= book.getSheetAt(0);
         book.write(fos);
         workbook.close();
         book.dispose();
         book.close();
        fos.close();
    }
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.text.similarity.JaroWinklerDistance;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteExcelUtility {
    public static int globalSelectedIndex = 0;

    public static FileInputStream fis;
    public static XSSFWorkbook workbook;
    public static XSSFSheet sheet;
    public static FileOutputStream fos;

    public static String excelInputFile;
    public static String excelOuputFile;

    public static String excelInputFileForBacklog;
    public static String excelOutputFileForBacklog;
    public static XSSFSheet sheetBacklogForm;

    public static int banyakSheet;
    public static ArrayList<String> daftarSheet;

    // Jangan lupa set variable "excelOuputFile" sebelum constructInputStream di
    // invoke
    public static void constructInputStream() throws IOException {
        // ZipSecureFile.setMinInflateRatio(0.009);
        daftarSheet = new ArrayList<>();
        fis = new FileInputStream(new File(excelInputFile));

        workbook = new XSSFWorkbook(fis);
        sheet = workbook.getSheetAt(globalSelectedIndex);
        
        System.out.println("excelInputFile = " + excelInputFile);

    }

    // Jangan lupa set variable "excelOuputFile" sebelum constructInputStream di
    // invoke
    public static void constructInputStreamForBacklogForm(int sheetInd) throws IOException {
        if (fis!=null) {
            fis.close();
        }
        fis = new FileInputStream(new File(excelInputFileForBacklog));
        workbook = new XSSFWorkbook(fis);
        sheetBacklogForm = workbook.getSheetAt(sheetInd);

        System.out.println("excelInputFileForBacklog = " + excelInputFileForBacklog);
        System.out.println("sheetnya = " + workbook.getSheetName(sheetInd));

    }

    public static void constructOutputStream(String excelOuputFiles) throws FileNotFoundException {
        WriteExcelUtility.excelOuputFile = excelOuputFiles;
        // wajib nama lain
        fos = new FileOutputStream(new File(excelOuputFile));
        System.out.println("excelOutputFile" + excelOuputFile);

    }

    public static void constructOutputStreamForBacklogForm(String excelOuputFilesForBaglog)
            throws FileNotFoundException {
        // WriteExcelUtility.excelOuputFile = excelOuputFiles;
        // wajib nama lain
        fos = new FileOutputStream(new File(excelOuputFilesForBaglog));
        System.out.println("excelOuputFilesForBaglog = " + excelOuputFilesForBaglog);

    }

    public static void getNumberofsheetAndTheirName() throws IOException {
        // Log.d ("WriteExcelUtility.getNumberofsheetAndTheirName
        // :","excelInputFile = "+excelInputFile);
        // System.out.println ("excelOutputFile"+excelOuputFile);

        banyakSheet = workbook.getNumberOfSheets();
        System.out.println("Jumlah sheet = " + banyakSheet);
        for (int i = 0; i < banyakSheet; i++) {
            /* XSSFSheet sheetName = workbook.getSheetAt(i); */
            String namaSheet = workbook.getSheetName(i).toString();
            if (namaSheet.contains("Sheet")) {
                // do nothing
            } else {
                System.out.println(namaSheet);
                daftarSheet.add(namaSheet);
            }

        }
        fis.close();

    }

    // Cuma untuk update dan set cell value. Penulisan final di method
    // writeAndCloseALLStream()
    public static void updateDataPM(int sheetN, int row, int sell, String value)
            throws IOException, InvalidFormatException {
        System.out.println("excelInputFile" + excelInputFile);
        System.out.println("excelOutputFile" + excelOuputFile);

        XSSFRow row1 = sheet.getRow(row);
        if (row1 == null) {
            row1 = sheet.createRow(row);
        }
        XSSFCell cell1 = row1.getCell(sell);
        if (cell1 == null) {
            cell1 = row1.createCell(sell);
        }
        cell1.setCellValue(value);

    }

    // Cuma untuk update dan set cell value. Penulisan final di method
    // writeAndCloseALLStream()
    public static void updateDataLogback(int sheetN, int row, int sell, String value)
            throws IOException, InvalidFormatException {
        System.out.println("excelInputFileForBacklog =" + excelInputFileForBacklog);
        System.out.println("excelOutputFileForBacklog =" + excelOutputFileForBacklog);

        XSSFRow row1 = sheetBacklogForm.getRow(row);
        if (row1 == null) {
            row1 = sheetBacklogForm.createRow(row);
        }
        XSSFCell cell1 = row1.getCell(sell);
        if (cell1 == null) {
            cell1 = row1.createCell(sell);
        }
        cell1.setCellValue(value);

    }

    public static void writeAndCloseALLStream() throws IOException {
        /*
         * System.out.println ("excelInputFile"+excelInputFile);
         * System.out.println ("excelOutputFile"+excelOuputFile);
         */
        
        
        fis.close();
        //workbook.write(fos);

        SXSSFWorkbook book = new SXSSFWorkbook(workbook,100,true); 
         book.setCompressTempFiles(true);
        SXSSFSheet sheetSXSS= book.getSheetAt(0);
        
        
        
 
         book.write(fos);
         workbook.close();
         book.dispose();
         book.close();
        fos.close();
    }

    // Require apache commong text 1.8
    public static Double compareStrings(String stringA, String stringB) {
        return new JaroWinklerDistance().apply(stringA, stringB);
        // return StringUtils.Jar(stringA, stringB);
    }

    // for backlog doc
    // Method ini mereturnkan starting row dimana kita akan tulisi
    // titleCategory = misalkan ENGINE COMPARTMENT & ACCESSORIES, DRIVE TRAIN,
    // etc
    public static int getStartingRowToWriteBacklog(String pathDoc, int sheet, String titleCategory) throws IOException {

        XSSFSheet mySheet = workbook.getSheetAt(sheet);
        XSSFCell checked = null;
        int startingRowInACategory = 0;

        /**
         * =================================================================
         * cari di (kategori mis. Engine compartment, drive train, etc) mana
         * kita akan mulai loop (untuk start penulisan data ke row)
         * ===================================================================
         */
        for (int i = 0; i < 100; i++) {
            try {
                // 2 == column "inspection backlog" yg berisi misalkan ENGINE
                // COMPARTMENT & ACCESSORIES, DRIVE TRAIN, etc
                checked = mySheet.getRow(i).getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                // kalau nilai cell "founded" tidak blank
                // if percentage similarity is high, i.e, 1.00 == SIMILAR
                if (WriteExcelUtility.compareStrings(titleCategory, checked.toString()) > (double) 0.39) {
                    System.out.println(" checked =" + checked.toString().trim());
                    startingRowInACategory = i;
                    System.out.println("Loop ended at baris =" + i);
                    break; // break loop
                }
            }
            // kalau nilai cell "founded" blank
            catch (NullPointerException npe) {
                // kalau nilai cell "founded" blank
                System.out.println(" checked =" + checked.toString().trim());
                
            }

        } // end loop

        int finalRowToWrite = 0;// ini nomor baris yang akan mulai ditulisi
    
        for (int i = (startingRowInACategory + 1); i < 100; i++) {

            if (mySheet.getRow(i).getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString()
                    .equalsIgnoreCase("")) {
                finalRowToWrite = i;
                //System.out.println("finalRowToWrite = " + finalRowToWrite);
                break; // break loop
            }
        }
        
        fis.close();

        return finalRowToWrite; // end reading close input stream
    }

    public static void main(String[] args) throws IOException, Exception {
         String tempTask = "nangla";
         long start = System.nanoTime();

        ZipSecureFile.setMinInflateRatio(0.0);
        long end = System.nanoTime();
        long duration = end - start;
        duration = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS);
        System.out.println("Durasinya Zipsecure = "+duration+" seconds");
        
        long start2 = System.nanoTime();
        WriteExcelUtility.excelInputFileForBacklog = "D:\\DIR.xlsx";
        WriteExcelUtility.constructInputStreamForBacklogForm(0);
        long end2 = System.nanoTime();
        long duration2 = end2 - start2;
        duration = TimeUnit.SECONDS.convert(duration2, TimeUnit.NANOSECONDS);
        System.out.println("Durasinya constructInputStreamForBacklogForm = "+duration+" seconds");
        
        
        int rowtowrite = WriteExcelUtility.getStartingRowToWriteBacklog(WriteExcelUtility.excelInputFileForBacklog, 0,
                "Engine compartment");
        System.out.println("Mulai penulisan pada row = "+ rowtowrite);
        WriteExcelUtility.updateDataLogback(3, rowtowrite, 2, tempTask); 
         String fileNameBacklogtosavetointernal = "D:\\DIR1.xlsx";
         WriteExcelUtility.constructOutputStream(fileNameBacklogtosavetointernal);
        
         long start3 = System.nanoTime();
         WriteExcelUtility.writeAndCloseALLStream();
         long end3 = System.nanoTime();
            long duration3 = end3 - start3;
            //duration = TimeUnit.SECONDS.convert(duration3, TimeUnit.NANOSECONDS);
            System.out.println("Duration writeAndCloseALLStream = "+(Double.valueOf (duration3/ 1000000))/1000+" s");
        
    }

}
Durasinya Zipsecure = 0 seconds
excelInputFileForBacklog = D:\DIR.xlsx
sheetnya = Backlog 14 0416
Durasinya constructInputStreamForBacklogForm = 2 seconds
 checked =ENGINE COMPARTMENT & ACCESSORIES
Loop ended at baris =14
Mulai penulisan pada row = 33
excelInputFileForBacklog =D:\DIR.xlsx
excelOutputFileForBacklog =null
excelOutputFileD:\DIR1.xlsx
Duration writeAndCloseALLStream = 40.024 s

共有1个答案

端木兴国
2023-03-14

挣扎了大约一个星期。修改了这么多东西,几乎失去了按时完成这个软件的希望。我刚意识到这个医生本身就是问题的根源。它最初是由一些ERP SAP软件创建的。在文档的创建过程中,我相信SAP应用程序生成的xmls中有一些代码与POI并不兼容。或者,它们是兼容的,但导致POI需要很长时间来适应一些约束。因此,缓慢的一代。为了解决这个问题,我别无选择,只能将数据复制到一个新的工作簿中,以处理工作簿上的任何数据。但是我必须把所有的数据复制粘贴到工作簿的25张纸上。但那很好。毕竟,我的工作簿生成时间在新工作簿上只有0.337秒(从40秒下降)。

 类似资料:
  • 问题内容: 今天,在浏览各种问题时,我遇到了一个问题,在我看来有点不可思议,为什么一个人要在上面加上a ,对于这种情况会不会有什么真正的原因,所以这只是微不足道的吗? 问题答案: 动画图像作为GUI的BG。我使用HTML来调整此尺寸(x3),但是如果它已经是所需的尺寸,则可以直接将其设置为标签的。 不知道它是否是“真正的”。这似乎是一个主观术语,需要更多说明。我从来没有使用过这种方法,只是想通了,

  • 本文向大家介绍在什么情况下选择webpack?在什么情况下选择rollup?相关面试题,主要包含被问及在什么情况下选择webpack?在什么情况下选择rollup?时的应答技巧和注意事项,需要的朋友参考一下 非要一句话区分的话 如果是用,如果是用其实界限并不是特别明显。在某些特殊情况下可以互用

  • 问题内容: 说我有一个清单。在什么情况下被称为? 我基本上理解了文档,但是我也想看到一个示例来毫无疑问地阐明其用法。 问题答案: 当Python尝试将两个对象相乘时,它首先尝试调用左侧对象的方法。如果左对象没有方法(或者该方法返回,表明它不适用于所讨论的右操作数),则Python希望知道右对象是否可以进行乘法。如果右操作数与左操作数的类型相同,Python就会知道它不能,因为如果左对象不能做到这一

  • 问题内容: 在下面的源代码中,我抛出一个。 为什么没有必要将关键字放在方法的签名上? 问题答案: 仅在Java 1.7上会出现此现象。使用1.6进行编译时,出现以下编译器错误消息: 但是,使用Java 1.7可以编译。 …直到我实际把一个块扔了进去: 编译中… 看起来Java 1.7足够聪明,可以通过分析块代码来检测可能抛出的类型,而1.6刚看到类型并为此给出了错误。 对其进行更改以使其按预期方式

  • 如果这是存在的,这个函数很快完成,但如果上面不存在,那么它运行很长时间。 有人能帮我解决这个问题吗?这样检查就可以很快进行了?

  • 我有这个枚举: 并且想实现CustomStringConvertible,所以我做到了。(不要在这上面戳洞,这个问题已经简化了:): 在Playground中运行它完全符合要求。但是,它会在最终的中出错。(同样的运行时失败发生在我的应用程序中,所以这与在游乐场中无关。) 经过大量的头疼和代码设置,我发现这是因为没有实现CustomStringConverable,所以开关中没有匹配的情况。我假设这