Apache POI - Excel函数提前退出,没有错误

时间:2021-10-30 20:23:36

I'm writing a web app that creates an Excel file that the user can download. I thought there was something wrong with my file creation code, so I replaced it with something off of here that supposedly works.

我正在编写一个Web应用程序,用于创建用户可以下载的Excel文件。我认为我的文件创建代码有问题,所以我用一些可能有效的东西替换它。

When it reaches:

到达时:

XSSFWorkbook wb = new XSSFWorkbook();

it leaves the CreateExcel class without running the rest of the code or generating an error.

它离开CreateExcel类而不运行其余代码或生成错误。

I'm using Apache POI 3.14 and developing on Netbeans. I am completely baffled by this, and any help would be appreciated.

我正在使用Apache POI 3.14并在Netbeans上进行开发。我完全被这个困惑,任何帮助都将不胜感激。

The relevant code:

相关代码:

Servlet entry:

else if (request.getParameter("formType").equalsIgnoreCase("downloadExcel")) {
        String filePath = "";
        try {
            ThreadB b = new ThreadB();
            int viewId = Integer.parseInt(request.getParameter("viewId"));
            b.tc = DatabaseUtil.getClassDetails(viewId);
            b.classList = DatabaseUtil.getClassRoster(viewId);
            b.start();

            synchronized (b) {
                try {
                    b.wait();
                } catch (InterruptedException e) {
                    PrintWriter out = response.getWriter();
                    out.write(TrainingRegistrationServlet.stackTraceToString(e));
                    out.close();
                }
                filePath = b.filePath;
            }
        } catch (ClassNotFoundException | SQLException ex) {
            PrintWriter out = response.getWriter();
            out.write(TrainingRegistrationServlet.stackTraceToString(ex));
            out.close();
        }
        sendFile(response, filePath);

    }

Thread:

class ThreadB extends Thread {

    String filePath;
    TrainingClass tc = null;
    List<Registrant> classList = null;

    @Override
    public void run() {
        synchronized (this) {
            try {
                filePath = CreateExcel.createRoster(tc, classList);
            } catch (IOException ex) {
                Logger.getLogger(ThreadB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

CreateExcel class:

package org.bcso.com.TrainingRegistration.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

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;

import org.bcso.com.TrainingRegistration.data.Registrant;
import org.bcso.com.TrainingRegistration.data.TrainingClass;

public class CreateExcel {

    public CreateExcel(TrainingClass tc, List<Registrant> rosterList) {

    }

    public static String createRoster(TrainingClass tc, List<Registrant> rosterList) throws FileNotFoundException, IOException {
        String excelFileName = "C:/tmp/Test.xlsx";//name of excel file
        String sheetName = "Sheet1";//name of sheet

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName);

        for (int r = 0; r < 5; r++) {
            XSSFRow row = sheet.createRow(r);
            for (int c = 0; c < 5; c++) {
                XSSFCell cell = row.createCell(c);
                cell.setCellValue("Cell " + r + " " + c);
            }
        }

        FileOutputStream fileOut = new FileOutputStream(excelFileName);

        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
        return excelFileName;
    }
}

1 个解决方案

#1


1  

Turned out that I WAS getting an error, it was just being reported very slowly. I had mismatched jars for Apache POI.

原来我得到了一个错误,只是报告的速度很慢。我为Apache POI设置了不匹配的jar。

If anyone else has this issue, you can get the correct version of the jars via a link at: https://poi.apache.org/overview.html

如果其他人有此问题,您可以通过以下链接获取正确版本的罐子:https://poi.apache.org/overview.html

#1


1  

Turned out that I WAS getting an error, it was just being reported very slowly. I had mismatched jars for Apache POI.

原来我得到了一个错误,只是报告的速度很慢。我为Apache POI设置了不匹配的jar。

If anyone else has this issue, you can get the correct version of the jars via a link at: https://poi.apache.org/overview.html

如果其他人有此问题,您可以通过以下链接获取正确版本的罐子:https://poi.apache.org/overview.html