@SuppressWarnings("unchecked") public String exportExcelMonthBase() { try { Struts2Utils.getResponse().setContentType( "application/vnd.ms-excel"); //设置导出文件名 String sj = tjTime.substring(0,4)+"年"+tjTime.substring(tjTime.length()-2)+"月"; String fileName="测试"; //文件名转码,这样写可以创建中文名称的*.xls Struts2Utils.getResponse().setHeader("content-disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO-8859-1") + ".xls"); //还可以直接这样写
OutputStream out = Struts2Utils.getResponse().getOutputStream();try {List<ReportJSP> list = this.tServiceQualityBaseService.findTServiceQualityBaseByStationOidAndTime(stationOid, tjTime);// 创建一个工作薄WritableWorkbook wwb = Workbook.createWorkbook(out);//Struts2Utils.getResponse().setHeader("content-disposition", // "attachment;filename="+ fileName + ".xls");
//设置字体格式以及表格格式WritableFont font3 = new WritableFont(WritableFont.createFont("宋体_UTF-8"),12);// 设置字体为宋体,大小12号WritableCellFormat format3 = new WritableCellFormat(font3);//把字体格式放入表格格式中format3.setAlignment(jxl.format.Alignment.CENTRE);//设置表格内容水平居中format3.setVerticalAlignment(VerticalAlignment.CENTRE);//设置表格内容垂直居中format3.setWrap(true);//设置表格内容字体加粗// 创建一张工作表WritableSheet ws = wwb.createSheet("随便写", 0);ws.setColumnView(20, 15);//设置第21列列宽为15ws.setColumnView(21, 25);ws.setRowView(1, 500);//设置第2行行高500ws.setRowView(2, 500);ws.mergeCells(0, 0, 21, 0);//合并第1列第1行到第22列第1行ws.addCell(new Label(0, 0, fileName,format3));//0,0是表格位置,fileName是表格内容,format3是上面设置好的样式赋给要用这个样式的表格即可String[] titles = { "区域", "站点数", "应到报", "及时报", "逾限报", "到报率",
"缺报" };//表头
// 循环添加表头
for (int i = 0; i < titles.length; i++) {
ws.addCell(new Label(i, 1, titles[i]));
}
// 循环添加内容
ReportJSP reportJSP = new ReportJSP();
for (int i = 0; i < list.size(); i++) { reportJSP = listReportJSPs.get(i);
ws.addCell(new Label(0, i + 2, reportJSP.getTArea()
.getName()));
ws.addCell(new Label(1, i + 2, String.valueOf(reportJSP
.getStationNUM())));
ws.addCell(new Label(2, i + 2, String.valueOf(reportJSP
.getYdb())));
ws.addCell(new Label(3, i + 2, String.valueOf(reportJSP
.getJsb())));
ws.addCell(new Label(4, i + 2, String.valueOf(reportJSP
.getYxb() + reportJSP.getWdb())));
ws.addCell(new Label(5, i + 2, String.valueOf(reportJSP
.getDbl())));
ws.addCell(new Label(6, i + 2, String.valueOf(reportJSP
.getQb()), colorFormat));}wwb.write();wwb.close();} finally {out.close();}} catch (Exception ex) {System.out.println("excel创建失败" + ex);}return null;}
java导出Excel表格
最新推荐文章于 2022-06-20 16:52:24 发布