我正在编写一个Web应用程序,用于创建用户可以下载的Excel文件。我以为我的文件创建代码出了问题,所以我把它替换成了可能有用的东西。
到达时:
XSSFWorkbook wb = new XSSFWorkbook();
它离开CreateExcel类而不运行其余代码或生成错误。
我正在使用Apache POI 3.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;
}
}
答案 0 :(得分:1)
原来我发现了一个错误,只是报告得很慢。我为Apache POI设置了不匹配的jar。
如果其他人有此问题,您可以通过以下链接获取正确版本的广告:https://poi.apache.org/overview.html