日期:2014-05-16 浏览次数:20717 次
public ActionForward doExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ExportExcelService eeService = new ExportExcelService(); RawDataExportSearchForm rawDataExportSearchForm = (RawDataExportSearchForm) form; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=\" " + "Reporting.xls" + "\""); HSSFWorkbook wb = eeService.doExportExcel(request, rawDataExportSearchForm); BufferedOutputStream buf = new BufferedOutputStream(response .getOutputStream()); wb.write(buf); buf.close(); return mapping.findForward("doExporting"); }
public HSSFWorkbook doExportExcel(HttpServletRequest request, RawDataExportSearchForm form){ HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet worksheet = workbook.createSheet("Sheet1"); // index from 0,0... cell A1 is cell(0,0) HSSFRow rowHead = worksheet.createRow((short) 0); HSSFCell cellHeadA = rowHead.createCell((short) 0); cellHeadA.setCellValue("第一行列名"); HSSFCell cellHeadB = rowHead.createCell((short) 1); cellHeadB.setCellValue("第一行列名"); HSSFCell cellHeadC = rowHead.createCell((short) 2); cellHeadC.setCellValue("第一行列名"); HSSFCell cellHeadD = rowHead.createCell((short) 3); cellHeadD.setCellValue("第一行列名"); ………… //第一行列名写完后 //从DB中获取Data,组装成HashMap HashMap<Long, RawData> rawDataMap = getRawDataFromDB(form); Set keys = rawDataMap.keySet(); RawData rawData = null; int i = 1; //遍历List中的每一个Object for(Iterator iter = keys.iterator(); iter.hasNext(); i++){ rawData = rawDataMap.get(iter.next()); HSSFRow row = worksheet.createRow((short) i); HSSFCell cellA = row.createCell((short) 0); cellA.setEncoding(HSSFCell.ENCODING_UTF_16); cellA.setCellValue(rawData.getField1()); HSSFCell cellB = row.createCell((short) 1); cellB.setEncoding(HSSFCell.ENCODING_UTF_16); cellB.setCellValue(rawData.getField2()); HSSFCell cellC = row.createCell((short) 2); cellC.setEncoding(HSSFCell.ENCODING_UTF_16); cellC.setCellValue(rawData.getField3()); HSSFCell cellD = row.createCell((short) 3); cellD.setEncoding(HSSFCell.ENCODING_UTF_16); cellD.setCellValue(rawData.getField4()); …… //till to end, cell.setEncoding(HSSFCell.ENCODING_UTF_16);是为解决显示中文出乱码问题 } return workbook; }
return mapping.findForward("doExporting");
return null;