POI创建Excel并下载

来源:互联网 发布:淘宝店快速提高信誉 编辑:程序博客网 时间:2024/04/29 09:08

过程:

由controller接受session中保存的list。传到service中处理, 处理后传回controller接收,list也可以是从数据库中直接导出的。直接service处理后可以直接传到controller中(框架使用的ssm)。


代码:


controller中:

@ResponseBody@RequestMapping(value = "/load")public void downloadFalse(HttpServletResponse response,HttpSession session) throws IOException {     List<Map<Integer,Object>> list =(List)session.getAttribute("falseList");     File file = null;     RandomAccessFile raf = null;     OutputStream responseOS = response.getOutputStream();     try {     responseOS = response.getOutputStream();         file = membersService.downloadFalse(list);         response.addHeader("Content-Length", "" + file.length());         response.setHeader("content-disposition","attachment;filename=" + new  String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));         raf = new RandomAccessFile(file, "rw");         byte[] buffer = new byte[1024 * 1024];         int avariable = -1;         while ((avariable = raf.read(buffer)) > 0) {         responseOS.write(buffer, 0, avariable);         }     buffer = null;         responseOS.flush();     } catch (Exception e) {     LOGGER.error(e.getMessage(), e);     } finally {   if(raf != null)         raf.close();      if(file != null)         file.delete();      if(responseOS != null)       responseOS.close();     }}

Service中:

public File downloadFalse(List<Map<Integer,Object>> falseList) throws IOException {   //创建Excel     HSSFWorkbook wb = new HSSFWorkbook();     //创建Sheet     HSSFSheet sheet = wb.createSheet("user_input_false");     Integer rowCount = falseList.size()+1; //设置行数     //创建行  HSSFRow[] row = new HSSFRow[rowCount];     for (int i=0; i<rowCount; i++){     row[i] = sheet.createRow(i);     }//设置表头内容存到数组中  String[] header = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15" };//设置列宽,存到数组中     short[] weight = {5500,5500,5500,5500,3140,3140,3140,8000,7270,15000,4730,8580,8480,7540,22360};     for(int i=0; i<15; i++){     sheet.setColumnWidth(i, weight[i]);     }   //设置字体  HSSFFont f = wb.createFont();     f.setFontHeightInPoints((short) 11);     //设置head样式  HSSFCellStyle styleHead = wb.createCellStyle();     HSSFDataFormat format = wb.createDataFormat();     styleHead.setDataFormat(format.getFormat("@"));     styleHead.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);     styleHead.setFillPattern(CellStyle.SOLID_FOREGROUND);     styleHead.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);     styleHead.setFont(f);     //设置数据样式  HSSFCellStyle styleExample = wb.createCellStyle();     styleExample.setFont(f);     styleExample.setDataFormat(format.getFormat("@"));     //输入第一行,使用样式  HSSFCell[] headerCell = new HSSFCell[15];     for (int i=0; i<15; i++){     headerCell[i] = row[0].createCell(i);         headerCell[i].setCellValue(header[i]);         headerCell[i].setCellStyle(styleHead);         row[0].setHeightInPoints(25);     }  //输入其余行  for (int total=0; total<falseList.size(); total++){     HSSFCell[] exampleCell = new HSSFCell[15];         for(int i=0;i<15;i++){       if (i==3){           if(falseList.get(total).get(i)!=null&&!falseList.get(total).get(i).toString().equals("")) {       String code = falseList.get(total).get(i).toString();             String registerCode = String.format("%05d", Integer.parseInt(code));             exampleCell[i] = row[total + 1].createCell(i);             exampleCell[i].setCellValue(registerCode);             exampleCell[i].setCellStyle(styleExample);         }     }else {     exampleCell[i] = row[total + 1].createCell(i);     exampleCell[i].setCellValue(falseList.get(total).get(i).toString());         exampleCell[i].setCellStyle(styleExample);         }  }}//导出   File file = new File("C:/Users/Administrator/Desktop/user_input_false.xls");        file.getParentFile().mkdirs();        FileOutputStream out = new FileOutputStream(file);        wb.write(out);        out.close();        return file;}


JS中:

如果要点击某个地方下载报表的话,不能用post发送请求,会有结果返回回来,并被接受,然后就导致无法出现下载的界面。

我用的方法是创建个表单提交。

function getReport() {var params = {};//参数都放到param里var form=$("<form>");//定义一个form表单        form.attr("style","display:none");        form.attr("target","");        form.attr("method","post");        for(var i in params){          form.append('<input type="hidden" name="'+i+'" value="'+params[i]+'" >');        }        console.log(form.html());        form.attr("action","/aaa/bbb");        $("body").append(form);//将表单放置在web中        form.submit();//表单提交}




原创粉丝点击