List<TZIn> list = tzInService.getList(tzIn,user); String fileName = "入库台账统计.xls"; response.setContentType("application/vnd.ms-excel;charset=UTF-8"); try { if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){ // ie浏览器 fileName = URLEncoder.encode(fileName, "UTF-8"); } else { fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); } response.setHeader("Content-Disposition", "p_w_upload; filename="+ fileName); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } WritableWorkbook web = null; ServletOutputStream sos = null; try { sos = response.getOutputStream(); web = Workbook.createWorkbook(sos); WritableSheet ws = web.createSheet("入库台账统计", 0); ws.setColumnView(0, 20); ws.setColumnView(1, 20); ws.setColumnView(2, 20); ws.setColumnView(3, 20); ws.setColumnView(4, 20); ws.setColumnView(5, 20); ws.setColumnView(6, 20); ws.setColumnView(7, 20); ws.setColumnView(8, 20); ws.setColumnView(9, 20); ws.setColumnView(10, 20); WritableCellFormat cellFormat = new WritableCellFormat(); // 设置单元格居中 cellFormat.setAlignment(Alignment.CENTRE); WritableFont font = new WritableFont(WritableFont.COURIER, 16,WritableFont.BOLD, true); WritableCellFormat format = new WritableCellFormat(font); format.setAlignment(Alignment.CENTRE); Label label0 = new Label(0, 0, "单位名称"); Label label1 = new Label(1, 0, "入库时间 "); Label label2 = new Label(2, 0, "废物名称"); Label label3 = new Label(3, 0, "废物类别"); Label label4 = new Label(4, 0, "废物代码"); Label label5 = new Label(5, 0, "废物形态"); Label label6 = new Label(6, 0, "废物来源"); Label label7 = new Label(7, 0, "包装形式"); Label label8 = new Label(8, 0, "数量(吨)"); Label label9 = new Label(9, 0, "接收仓库 "); Label label10 = new Label(10, 0, "经办人"); label0.setCellFormat(cellFormat); label1.setCellFormat(cellFormat); label2.setCellFormat(cellFormat); label3.setCellFormat(cellFormat); label4.setCellFormat(cellFormat); label5.setCellFormat(cellFormat); label6.setCellFormat(cellFormat); label7.setCellFormat(cellFormat); label8.setCellFormat(cellFormat); label9.setCellFormat(cellFormat); label10.setCellFormat(cellFormat); ws.addCell(label0); ws.addCell(label1); ws.addCell(label2); ws.addCell(label3); ws.addCell(label4); ws.addCell(label5); ws.addCell(label6); ws.addCell(label7); ws.addCell(label8); ws.addCell(label9); ws.addCell(label10); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { TZIn tzIn = list.get(i); Label cname = new Label(0, i+1, tzIn.getCname()); Label inTime = new Label(1, i+1, tzIn.getInTime().toString().substring(0, 10)); Label wasteName = new Label(2, i+1, tzIn.getWasteName()); Label wasteType = new Label(3, i+1, tzIn.getWasteType()); Label wasteCode = new Label(4, i+1, tzIn.getWasteCode()); Label wasteForm = new Label(5, i+1, TZToolUtil.formShow(tzIn.getWasteForm())); Label wasteSource = new Label(6, i+1, TZToolUtil.wasteSourceShow(tzIn.getWasteSource())); Label packageWay = new Label(7, i+1, TZToolUtil.packageWayShow(tzIn.getPackageWay())); Label wasteAmount = new Label(8, i+1, String.valueOf(tzIn.getWasteAmount())); Label acceptPlace = new Label(9, i+1, tzIn.getAcceptPlace()); Label handleMan = new Label(10, i+1, tzIn.getHandleMan()); cname.setCellFormat(cellFormat); inTime.setCellFormat(cellFormat); wasteName.setCellFormat(cellFormat); wasteType.setCellFormat(cellFormat); wasteCode.setCellFormat(cellFormat); wasteForm.setCellFormat(cellFormat); wasteSource.setCellFormat(cellFormat); packageWay.setCellFormat(cellFormat); wasteAmount.setCellFormat(cellFormat); acceptPlace.setCellFormat(cellFormat); handleMan.setCellFormat(cellFormat); ws.addCell(cname); ws.addCell(inTime); ws.addCell(wasteName); ws.addCell(wasteType); ws.addCell(wasteCode); ws.addCell(wasteForm); ws.addCell(wasteSource); ws.addCell(packageWay); ws.addCell(wasteAmount); ws.addCell(acceptPlace); ws.addCell(handleMan); } } web.write(); sos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { web.close(); sos.close(); } catch (Exception e1) { e1.printStackTrace(); } }
java WritableWorkbook导出Excel
原创
©著作权归作者所有:来自51CTO博客作者猴子也疯狂的原创作品,谢绝转载,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java Excel导入导出
Java的两种方式操作Excel
数据 java excel 导出数据 -
excel java 导出转换 java导出excel poi
需要注意的几个地方:1.导入导出的顺序要和打上注解的字段顺序一致2.web环境下导出时,不能使用异步,比如ajax,可用location.href或者a标签跳转都可以
excel java 导出转换 Excel POI java 字段