标签:
在structs.xml中的配置
1 <action name="exportExcelApplication" class="applicationAction" method="exportExcelApplication"> 2 <result type="stream"> 3 <param name="contentType">application/vnd.ms-excel</param> 4 <param name="contentDisposition">attachment;filename="APPLICATION_OLINE.xls"</param> 5 <param name="inputName">inputStream</param> 6 </result> 7 8 </action>
编写ExportExcelUtil方法
public class ExportExcelUtil { public static InputStream export(String sheetName, String[] titleName, String[][] data) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); // new 一个HSSFWorkbook实例 HSSFCellStyle setBorder = wb.createCellStyle(); setBorder.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 边框的大小 setBorder.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色 setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 边框的大小 setBorder.setBottomBorderColor(HSSFColor.BLACK.index); setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); setBorder.setTopBorderColor(HSSFColor.BLACK.index); setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN); HSSFSheet sheet = wb.createSheet(sheetName); // 创建一个sheet脚本 sheet.setDefaultColumnWidth(40); // 创建 title HSSFRow row = sheet.createRow(0); for (int i = 0; i < titleName.length; i++) { HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(titleName[i])); HSSFFont f = wb.createFont(); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 HSSFCellStyle style = wb.createCellStyle(); sheet.setColumnWidth(i, 6000); style.setFont(f); style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 边框的大小 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 边框的大小 cell.setCellStyle(style); } if (data != null && data.length > 0) { for (int j = 1; j <= data.length; j++) { row = sheet.createRow(j); HSSFCell cell = null; for (int i = 0; i < titleName.length; i++) { cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING); cell.setCellValue(new HSSFRichTextString(data[j - 1][i])); cell.setCellStyle(setBorder); } } } ByteArrayOutputStream os = new ByteArrayOutputStream(); wb.write(os); InputStream is = new ByteArrayInputStream(os.toByteArray()) ; return is; } }
ACTION中的方法
/** * 导出统计数据 */ public String exportExcelApplication() { String sheetName = this.getText("app.online.statis");
//标题 String titleName[] = new String []{ "ID", "应用名称", "信息", "嘻嘻嘻", "哈哈", "呵呵", "嘿嘿", "啦啦", "xx", "xx", "xx", "xxx", "xx", "xx", };
// 获取数据 String[][] data = new String[0][titleName.length]; try { ResultBean result = applicationService.queryListAll(new ParamBean().addInputObject(initObject(Application.class))); long total =result.getTotal(); data = new String[(int) total+1][14]; @SuppressWarnings("unchecked") List<Application> list1 =(List<Application>) result.getReturnSet(); if(list1!=null && list1.size()>0){ int i =0; for(Application p:list1){ data[i][0] = p.getId().toString(); data[i][1] = p.getAppName().toString(); data[i][2] = p.getCreatorName().toString(); data[i][3] = p.getAppTypeName().toString();
//截取字符串数字,根据数字替换 String[] conditions = p.getArea().toString().split(","); String areaStr = ""; String sprlitStr = " , "; for ( int m=0 ;m<conditions.length;m++) { for (int n=0; n<32;n++) { if (n<10) { if(("0"+n).equals(conditions[m])) { String str = "area.0"+n; areaStr+=this.getText(str)+sprlitStr; } }else { if(String.valueOf(n).equals(conditions[m])) { areaStr+=this.getText("area."+String.valueOf(n))+sprlitStr; } } } if("any".equals(conditions[m])) { areaStr+=this.getText("area.any")+sprlitStr; } if("internet".equals(conditions[m])) { areaStr+=this.getText("area.internet"); } } data[i][4] = areaStr ; if(String.valueOf(p.getIsRecommend()).equals("1")){ data[i][5] =this.getText("recommend"); }else{ data[i][5] = this.getText("yes"); } data[i][6] = String.valueOf(p.getSearchLevel()); if(p.getIsFree().toString().equals("1")){ data[i][7]=this.getText("yes"); }else{ data[i][7] =this.getText("no"); } if(p.getStatus().toString().equals("5")){ data[i][8] =this.getText("online"); }else if(p.getStatus().toString().equals("6")){ data[i][8] =this.getText("offline"); } if(p.getPublishStatus().toString().equals("1")){ data[i][9] = this.getText("success"); }else if(p.getPublishStatus().toString().equals("3")){ data[i][9] =this.getText("unbegin"); }else if (p.getPublishStatus().toString().equals("2")) { data[i][9] =this.getText("failure"); } data[i][10] = p.getMd5Reason(); data[i][11] = p.getOnlineDate(); data[i][12] = p.getUpdateDate().toString(); data[i][13] = p.getSource().toString(); i++; } //总计 data[i][0] = "总计: "+ String.valueOf(total); } inputStream = ExportExcelUtil.export(sheetName, titleName, data); } catch (Exception e) { log.error(e); sendResponse(new ResultBean().setSuccess(false)); } return "success"; }
标签:
原文地址:http://www.cnblogs.com/lk-java/p/4826130.html