首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

创造Excel 并导出到Web客户端

2012-08-25 
创建Excel 并导出到Web客户端//以下代码是通过poi框架实现//导出Excel列表public ActionForward printExce

创建Excel 并导出到Web客户端







     //以下代码是通过poi框架实现       //导出Excel  列表public ActionForward printExcel(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception{response.setContentType("application/vnd.ms-excel");response.setHeader("Content-Disposition", "inline; filename="order.xls"");response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0);String order = request.getParameter("order");HSSFWorkbook wb  = this.getHSSFWorkbook(orderMgr.getCommodityByOrderNo(order),order) ; wb.write(response.getOutputStream());response.flushBuffer() ;return null;}private HSSFWorkbook getHSSFWorkbook(List<Object[]> list , String order){HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("sheet1");sheet.setColumnWidth(0,5000) ;sheet.setColumnWidth(1,7000) ;sheet.setColumnWidth(2,5000) ;sheet.setColumnWidth(3,7000) ;sheet.setColumnWidth(4,5000) ;//        HSSFCellStyle   style   =   wb.createCellStyle();//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER) ;//        style.setFont(font);                 HSSFCellStyle  pubStyle  = wb.createCellStyle() ;        pubStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER) ;        HSSFRow row = sheet.createRow(0);row.setHeight((short) 400 ) ;HSSFCell orderNO  = row.createCell(0) ;orderNO.setCellValue("订单号");orderNO.setCellStyle(pubStyle);row.createCell(1).setCellValue(order);HSSFRow rowb = sheet.createRow(1);HSSFCell ca = rowb.createCell(0);ca.setCellValue("货号");ca.setCellStyle(pubStyle);HSSFCell cb  =rowb.createCell(1);cb.setCellValue("书名");cb.setCellStyle(pubStyle);HSSFCell cc = rowb.createCell(2);cc.setCellValue("报定数量");cc.setCellStyle(pubStyle);HSSFCell cd =rowb.createCell(3);cd.setCellValue("当前架位号");cd.setCellStyle(pubStyle);HSSFCell ce = rowb.createCell(4);ce.setCellValue("当前库存数量");ce.setCellStyle(pubStyle); for (int i = 0; i < list.size(); i++){Object[] obj = list.get(i) ;HSSFRow tmpR = sheet.createRow(i+2);HSSFCell plucode =tmpR.createCell(0);plucode.setCellValue(obj[0].toString());plucode.setCellStyle(pubStyle);HSSFCell bookName = tmpR.createCell(1);bookName.setCellValue(obj[1].toString());bookName.setCellStyle(pubStyle);HSSFCell buyCount = tmpR.createCell(2);buyCount.setCellValue(obj[2].toString());buyCount.setCellStyle(pubStyle);HSSFCell point = tmpR.createCell(3);point.setCellValue(obj[3].toString());point.setCellStyle(pubStyle);HSSFCell discount=tmpR.createCell(4);discount.setCellValue(obj[4].toString());discount.setCellStyle(pubStyle);}return wb ;}



热点排行