有 Java 编程相关的问题?

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

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

我正在编写一个web应用程序,创建一个用户可以下载的Excel文件。我认为我的文件创建代码有问题,所以我用一个在这里可以正常工作的代码替换了它

当达到:

XSSFWorkbook wb = new XSSFWorkbook();

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

我正在使用ApachePOI3.14并在Netbeans上开发。我对此完全感到困惑,任何帮助都将不胜感激

有关守则:

Servlet条目:

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

    }

线程:

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类:

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 楼答案

    结果是我得到了一个错误,只是报告速度很慢。我为ApachePOI提供了不匹配的JAR

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