JAVA中EXLS导出功能实现 - 代码:
基于SSH框架下的exls导出
public void exportExl() throws Exception{ String text = (String) super.getRequest().getParameter("text"); String flowid="051021";//(String)super.getRequest().getParameter("flowid"); ResultPage resulta=queryDataService.getexportkey(flowid, text); //获取主键属性 List<String> list=resulta.getExportlist(); //获取修改属性 List<String> reviselist=resulta.getExportreviselist(); //获取属性中文 List<String>cnlist=resulta.getExportkeycnlist(); //获取已选择数据 List datalist=resulta.getList(); //装载主键信息及数据 /** * 装载的数据类型为List<List<String>> */ List<List<String>> data=new ArrayList(); for(int i=0;i<datalist.size();i++){ Map map=(Map)datalist.get(i); List<String> datas=new ArrayList(); for(int n=0;n<list.size();n++){ String key=list.get(n); String value=String.valueOf(map.get(key)); datas.add(value); } data.add(datas); } List<List<String>> revisedata=new ArrayList(); for(int i=0;i<datalist.size();i++){ Map map=(Map)datalist.get(i); List<String> revise=new ArrayList(); for(int n=0;n<reviselist.size();n++){ String key=reviselist.get(n); // System.out.println("key>>>>>>>>>"+key); String value=String.valueOf(map.get(key)); revise.add(value); } revisedata.add(revise); } HttpServletResponse response=super.getResponse(); ServletOutputStream out=null; response.setContentType("applicatin/vnd.ms-excel"); HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook) //设置表名 String classname="基础调整-"+text+""; //表名导出中文格式化 String name=new String(classname.getBytes("gbk"),"iso-8859-1"); response.setHeader("content-disposition", "attchment;filename="+name+".xls"); HSSFSheet sheet = workbook.createSheet("基础调整");//创建工作表(Sheet) try { //创建表头属性 第一行 sheet.setColumnWidth(0, 5120);//设置第i列的宽度是31个字符宽度 HSSFRow row = sheet.createRow(0); for(int n=0;n<cnlist.size();n++){ String cnkeys=cnlist.get(n); HSSFCellStyle style=workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 HSSFFont font = workbook.createFont(); font.setFontName("仿宋");//设置字体名称 font.setFontHeightInPoints((short)12);//设置字号 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 style.setFont(font); //列 HSSFCell cell = row.createCell(n); cell.setCellValue(cnkeys); cell.setCellStyle(style); } //创建剩下行并添加值 for (int i=1;i<data.size()+1;i++){ List<String> strings = data.get(i-1); //行 row = sheet.createRow(i); sheet.setColumnWidth(i, 5120);//设置第i列的宽度是31个字符宽度 //System.out.println("strings>>>>>>>>>"+data.size()); //添加主键列数据 for (int j=0;j<strings.size();j++){ HSSFCellStyle style=workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 HSSFFont font = workbook.createFont(); font.setFontName("仿宋");//设置字体名称 font.setFontHeightInPoints((short)12);//设置字号 font.setColor(HSSFColor.RED.index);//设置字体颜色 style.setFont(font); String str= strings.get(j); //列 HSSFCell cell = row.createCell(j); cell.setCellValue(str); cell.setCellStyle(style); } //添加可修改属性数据 int k=list.size(); //System.out.println("revisedata>>>>>>>>>"+revisedata.size()); Object obj=JSONArray.fromObject(revisedata); System.out.println("revisedata>>>>>>>>>"+obj.toString()); List<String> str= revisedata.get(i-1); for(int w=0;w<str.size();w++){ HSSFCellStyle style=workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 HSSFFont font = workbook.createFont(); font.setFontName("仿宋");//设置字体名称 font.setFontHeightInPoints((short)12);//设置字号 style.setFont(font); HSSFCell cell = row.createCell(k++); String value=str.get(w); cell.setCellValue(value); cell.setCellStyle(style); } } //添加尾部 备注 HSSFRow lastrow = sheet.createRow(data.size()+2); HSSFCell cells=lastrow.createCell(0); HSSFCellStyle styles=workbook.createCellStyle(); styles.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 styles.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 HSSFFont font = workbook.createFont(); font.setFontName("仿宋");//设置字体名称 font.setFontHeightInPoints((short)20);//设置字号 font.setColor(HSSFColor.RED.index);//设置字体颜色 styles.setFont(font); cells.setCellValue("备注:红色字体区域为不可更改主键。请不要修改,以免发生错误,谢谢!"); cells.setCellStyle(styles); CellRangeAddress region=new CellRangeAddress(data.size()+2,data.size()+2, 0, cnlist.size()); sheet.addMergedRegion(region); out=response.getOutputStream(); // FileOutputStream out = new FileOutputStream(filePath); workbook.write(out);//保存Excel文件 } catch (IOException e) { // TODO: handle exception System.out.println("导出失败!"); }finally{ try { out.flush(); out.close();//关闭文件流 } catch (Exception e) { // TODO: handle exception } } System.out.println("OK!"); }