使用Apache POI在一次调用中返回多个Excel文件
我试图通过一个调用下载多个excel文件。 我有一个存储过程返回公司分解的数据,并为每个公司,我需要一个单独的Excel文件。 一些excel文件的值是相互依赖的(所以第一个excel文件会在第二个文件中出现。
除了我已经validation的一些function正常工作之外,我已经包含了下面的全部代码。 就目前来看,代码完全按照它应该运行,如果我没有检查输出,我会认为这是完美的,但是,我期望看到两个文件下载,而我只能得到第一个。 第二次写入ServletOutputStream的调用会触发,但是没有文件被下载。
任何人都可以看到这里有什么问题? 这不是文件大小,每个文件中最多应该有5条logging,而且我已经下载了单个的excel文件,没有问题。
package export; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import main.getFund; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * * @author mmarino */ public class NavExport extends HttpServlet { String dateStr = ""; String ERDate = ""; Double absoluteTotal = 0.0; Double iTotal = 0.0; getFund g = new getFund(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String str = ""; Workbook wb = new HSSFWorkbook(); Workbook pWB = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); Sheet oSheet = wb.createSheet("Recon"); Sheet pSheet = pWB.createSheet("Recon"); String prevCompany = ""; dateStr = new Date().toString(); dateStr = new SimpleDateFormat("MM/dd/yyy").format(new Date()); ERDate = "ER" + dateStr.substring(6, 10) + dateStr.substring(0,2) + dateStr.substring(3,5); try{ Connection conn = null; ResultSet rs = null; PreparedStatement prst = null; String s = request.getParameter("ids"); String roll = request.getParameter("roll"); String[] ids = s.split("\\|"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connUrl = "My Connection URL"; conn = DriverManager.getConnection(connUrl); String sql = "My SQL Query"; prst = conn.prepareStatement(sql); rs = prst.executeQuery(); int pInt = 1; int oInt = 1; g.main(); while(rs.next()){ if(rs.getString("Company").equals("10")){ addRow(pSheet, rs, createHelper, pInt); pInt++; }else{ if(!prevCompany.equals(rs.getString("Company")) && !prevCompany.equals("") && !prevCompany.equals("10")){ writeWorkBook(wb, prevCompany, response); oInt = 1; wb = new HSSFWorkbook(); oSheet = wb.createSheet("Recon"); iTotal = 0.0; } prevCompany = rs.getString("Company"); addRow(oSheet, rs, createHelper, oInt); iTotal += rs.getDouble("Approved"); oInt++; } absoluteTotal += rs.getDouble("Approved"); }// end rs.next(); if(!prevCompany.equals("10")){ addTopRow(oSheet, createHelper, prevCompany, iTotal); writeWorkBook(wb, prevCompany, response); } addTopRow(pSheet, createHelper, "10", absoluteTotal); writeWorkBook(pWB, "10", response); }catch(Exception e){ str="Got exception: " + e.getMessage(); } } private void addTopRow(Sheet sheet, CreationHelper createHelper, String Company, Double amount){ try{ Row row = sheet.createRow((short)0); row.createCell(0).setCellValue(createHelper.createRichTextString(dateStr)); row.createCell(1).setCellValue(createHelper.createRichTextString("")); row.createCell(2).setCellValue(createHelper.createRichTextString(ERDate)); row.createCell(3).setCellValue(createHelper.createRichTextString("G/L Account")); row.createCell(4).setCellValue(passSwitch(Company)); row.createCell(5).setCellValue(createHelper.createRichTextString("")); row.createCell(6).setCellValue(createHelper.createRichTextString("1000-0000")); row.createCell(7).setCellValue(createHelper.createRichTextString("")); row.createCell(8).setCellValue(createHelper.createRichTextString("")); row.createCell(9).setCellValue(createHelper.createRichTextString("")); row.createCell(10).setCellValue(createHelper.createRichTextString("")); row.createCell(11).setCellValue(createHelper.createRichTextString("Null")); row.createCell(12).setCellValue(createHelper.createRichTextString("")); row.createCell(13).setCellValue(createHelper.createRichTextString("")); row.createCell(14).setCellValue(createHelper.createRichTextString("")); row.createCell(15).setCellValue(createHelper.createRichTextString("")); row.createCell(16).setCellValue(createHelper.createRichTextString("")); row.createCell(17).setCellValue(amount); row.createCell(18).setCellValue(createHelper.createRichTextString("")); row.createCell(19).setCellValue(createHelper.createRichTextString("")); row.createCell(20).setCellValue(createHelper.createRichTextString("")); row.createCell(21).setCellValue(createHelper.createRichTextString("G/L Account")); }catch(Exception e){ String error = e.toString(); } } private void addRow(Sheet sheet, ResultSet rs, CreationHelper createHelper, int i){ try{ Row row = sheet.createRow((short)i); row.createCell(0).setCellValue(createHelper.createRichTextString(dateStr)); row.createCell(1).setCellValue(createHelper.createRichTextString("")); row.createCell(2).setCellValue(createHelper.createRichTextString(ERDate)); row.createCell(3).setCellValue(createHelper.createRichTextString("G")); row.createCell(4).setCellValue(rs.getString("Code")); row.createCell(5).setCellValue(createHelper.createRichTextString("")); row.createCell(6).setCellValue(createHelper.createRichTextString(g.getFundVar(rs.getString("Company") + ":" + rs.getString("Dept")))); row.createCell(7).setCellValue(createHelper.createRichTextString("")); row.createCell(8).setCellValue(createHelper.createRichTextString("")); row.createCell(9).setCellValue(createHelper.createRichTextString("")); row.createCell(10).setCellValue(createHelper.createRichTextString("")); String emNum = rs.getString("EM"); while(emNum.length() != 9){ emNum = "0" + emNum; } emNum = "E" + emNum; row.createCell(11).setCellValue(createHelper.createRichTextString(emNum)); row.createCell(12).setCellValue(createHelper.createRichTextString("")); row.createCell(13).setCellValue(createHelper.createRichTextString("")); row.createCell(14).setCellValue(createHelper.createRichTextString("")); row.createCell(15).setCellValue(createHelper.createRichTextString("")); row.createCell(16).setCellValue(createHelper.createRichTextString("")); row.createCell(17).setCellValue(rs.getDouble("Approved")); row.createCell(18).setCellValue(createHelper.createRichTextString("")); row.createCell(19).setCellValue(createHelper.createRichTextString("")); row.createCell(20).setCellValue(createHelper.createRichTextString("")); row.createCell(21).setCellValue(createHelper.createRichTextString("G/L Account")); }catch(Exception e){ String error = e.toString(); } } private void writeWorkBook(Workbook wb, String Company, HttpServletResponse response) { response.setContentType("application/vnd.ms-excel"); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setHeader("Content-Disposition", "attachment; filename=" + Company +".xls"); try { ServletOutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
要么你必须压缩的Excel文件,并作为响应发送,或者你有它作为一个单一的Excel文件,并使用多个工作表,而不是文件