日前做一个月参加了公司两个小项目,结业快两年,参加项目还真少!之所以呢,本人掌握的常识仍是很有限,完成了某些效能,急需放到这边,供今后参照!
何以从数据库中导出数据生成Excel文档
第一第一步,我参照了POI 这个开源项目,从其官方网站download亟需用到的jar包。 http://poi.apache.org/
其后项目采取的是Struts1.* 的版本,ok,那么放一些要害代码上来:
Action中的代码:
Java代码
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 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");
}
说明停: RawDataExportSearchForm 是前台页面用于查询的ActionForm,用来传到参数(parameters)
Service的代码:
Java代码
public HSSFWorkbook doExportExcel(HttpServletRequest request, RawDataExportSearchForm form){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("Sheet一");
// index from 零,0... cell A一 is cell(零,零)
HSSFRow rowHead = worksheet.createRow((short) 零);
HSSFCell cellHeadA = rowHead.createCell((short) 零);
cellHeadA.setCellValue("第一队列名");
HSSFCell cellHeadB = rowHead.createCell((short) 一);
cellHeadB.setCellValue("第一队列名");
HSSFCell cellHeadC = rowHead.createCell((short) 二);
cellHeadC.setCellValue("第一队列名");
HSSFCell cellHeadD = rowHead.createCell((short) 三);
cellHeadD.setCellValue("第一队列名");
…………
//第一队列名写完后
//从DB中获取Data,组建成HashMap
HashMap rawDataMap = getRawDataFromDB(form);
Set keys = rawDataMap.keySet();
RawData rawData = null;
int i = 一;
//遍历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) 零);
cellA.setEncoding(HSSFCell.ENCODING_UTF_16);
cellA.setCellValue(rawData.getField一());
HSSFCell cellB = row.createCell((short) 一);
cellB.setEncoding(HSSFCell.ENCODING_UTF_16);
cellB.setCellValue(rawData.getField二());
HSSFCell cellC = row.createCell((short) 二);
cellC.setEncoding(HSSFCell.ENCODING_UTF_16);
cellC.setCellValue(rawData.getField三());
HSSFCell cellD = row.createCell((short) 三);
cellD.setEncoding(HSSFCell.ENCODING_UTF_16);
cellD.setCellValue(rawData.getField四());
……
//till to end, cell.setEncoding(HSSFCell.ENCODING_UTF_16);是为解决展示汉语出乱码问题
}
return workbook;
}
本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception
- 3046 - org.hibernate.LazyInitializationException: could not initialize proxy
- 3047 - ssh中报java.lang.NullPointerException异常
- 3048 - java.lang.IllegalStateException: getWriter() has already been called for this response
- 3049 - org.hibernate.type.SerializationException:could not deserialize
- 3050 - java.lang.OutOfMemoryError:Java heap space
- 3051 - Internal Error
- 3052 - Exception对象
- 3053 - Hibernate异常处理机制
- 3054 - could not instantiate id generator
- 3055 - javax.faces.FacesException