struts2导出excel

来源:互联网 发布:卡巴斯基官网软件 编辑:程序博客网 时间:2024/05/18 00:44
使用jar包 jxl.jar

import jxl.Workbook;import jxl.write.Label;

@ActionDef(type = ActionType.PRIVATE)@Action(value = "compareExport", results = { @Result(name = "success", type = "stream") })      public String compareExport() throws Exception {  String header="始发地,始发中转班次,目的地,产品类型,出入港,路由属性,流向货量,路由环节1,路由环节2,路由环节3,路由环节4,路由环节5,路由环节6,路由环节7,路由环节8,路由环节9,路由环节10,路由环节11";        String column="start_place,start_transfer_batch,dest,product_type,out_or_in,route_attr,volume,detail1,detail2,detail3,detail4,detail5,detail6,detail7,detail8,detail9,detail10,detail11";        List<Map<String,Object>>  list = evaluatedao.findCompareList(areaType, createTm, null, null,null).getList();        export(header,column,list,"路由对比");        return null;    }

public void export(String header,String column,List<Map<String,Object>>  list,String excelName) throws Exception{ HttpServletResponse response = ServletActionContext.getResponse();          response.setCharacterEncoding("UTF-8");          response.setContentType("application/vnd.ms-excel");          excelName=excelName+ new SimpleDateFormat("MMddHHmm").format(new Date())+".xls";        response.setHeader("Content-Disposition", "attachment; filename="                  + new String(excelName.getBytes("GB2312"), "iso8859-1"));          response.setHeader("Pragma", "No-cache");          response.setHeader("Cache-Control", "No-cache");          response.setDateHeader("Expires", 0);          // 这个地方一定要进行编码的转换要不然中文字符会出现乱码.          // 设置下载头信息.end,          OutputStream output = null;          InputStream fis = null;          try {              output = response.getOutputStream();              jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(output);              jxl.write.WritableSheet ws = wwb.createSheet("用户通讯录", 0);             String[] headerArr=header.split(",");            String[] columnArr=column.split(",");            for (int i=0;i<headerArr.length;i++) {            //设置表头            ws.addCell(new Label(i, 0, headerArr[i]));            // 设置显示长度.            ws.setColumnView(i, 20); }            for (int i = 0; i < list.size();i++) {              Map<String,Object> obj = (Map<String,Object>) list.get(i);                  for(int j=0;j<columnArr.length;j++){                Object val=obj.get(columnArr[j]);                if (null==val || "null".equals(val.toString())) {                val="";}                ws.addCell(new Label(j, i + 1, val.toString() ));                 }            }              wwb.write();              wwb.close();          } catch (Exception e) {              System.out.println("Error!");              e.printStackTrace();          } finally {// 正常关闭输入输出流.              try {                  if (fis != null) {                      fis.close();                      fis = null;                  }              } catch (Exception e) {                  e.printStackTrace();              }              try {                  if (output != null) {                      output.close();                      output = null;                  }              } catch (Exception e) {                  e.printStackTrace();              }          }  }


1 0