有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么编写简单。xlsx的书在我的情况下很慢?

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

  1. 注: 在计算过程中,这两种方法没有问题。它们相当快。无需调试这些

//需要apache commong text 1.8

static Double compareStrings(String stringA, String stringB) {===>to compute distance
    
  • static int getStartingRowToWriteBacklog(String pathDoc, int sheet, String titleCategory) throws IOException {==>;计算从哪一行开始写入单元格
  1. 。xls文件: https://www.file.io/download/lx2Zq2N1lTDX 或 这里https://ufile.io/uzy4wvxz

我的问题出现在这个方法/行上:

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();
    }

这花了35~51秒完成。我迫切需要解决这个问题。经过近一周的研究,最终没有找到解决办法

完整代码:

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) 个答案

  1. # 1 楼答案

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