首页 > 编程语言 > 详细

Java学习之道:Java操作Excel之导出下载

时间:2014-08-04 14:35:07      阅读:368      评论:0      收藏:0      [点我收藏+]

页面放置一个按钮进行点击导出事件
<h:commandLink target="_parent" value="导出"
           action="#{eveluatesubject.doSaveExcel}"
           styleClass="cssButtonBG">

</h:commandLink>
          
后台的导出方法
/**
  * 导出EXCEL
  * 
  * @param request
  * @return
  */
 public String doSaveExcel() {
  HttpServletRequest request = (HttpServletRequest) FacesContext
    .getCurrentInstance().getExternalContext().getRequest();
  dooutof(request);

  return null;
 }

 public String dooutof(HttpServletRequest request) {

  // 文件名称与路径
  String realPath = request.getRealPath("");
  String fileName = "导出的文件名.xls";
  File file = new File(realPath + "//xxfzpj//tempPath//");//导出文件存放的位置
  if (!file.exists()) {
   file.mkdirs();
  }
  realPath = realPath + "//xxfzpj//tempPath//" + fileName;

  // 建立工作薄并写表头
  try {
   WritableWorkbook wwb = Workbook.createWorkbook(new File(realPath));
   WritableSheet ws = wwb.createSheet("Sheet1", 0);// 建立工作簿

   // 写表头
   jxl.write.Label label1 = new jxl.write.Label(0, 0, "列1");
   jxl.write.Label label2 = new jxl.write.Label(1, 0, "列2");
   jxl.write.Label label3 = new jxl.write.Label(2, 0, "列3");
   jxl.write.Label label4 = new jxl.write.Label(3, 0, "列4");
   jxl.write.Label label5 = new jxl.write.Label(4, 0, "列5");

   ws.addCell(label1);// 放入工作簿
   ws.addCell(label2);
   ws.addCell(label3);
   ws.addCell(label4);
   ws.addCell(label5);

   // 写入信息
   List<Map> listmsg = 数据库中读出的内容(执行SQL);

   for (int i = 0; i < listmsg.size(); i++) {
    label1 = new jxl.write.Label(0, i + 1, listmsg.get(i).get(
      "subject_id").toString().trim());// 建立第一列
    label2 = new jxl.write.Label(1, i + 1, listmsg.get(i).get(
      "subject_name").toString().trim());// 建立第二列
    if (listmsg.get(i).get("object_name") != null
      && "" != listmsg.get(i).get("object_name")) {
     label3 = new jxl.write.Label(2, i + 1, listmsg.get(i).get(
       "object_name").toString().trim());// 建立第三列
    } else {
     label3 = new jxl.write.Label(2, i + 1, "没有定义评价对象名称!");
    }
    label4 = new jxl.write.Label(3, i + 1, listmsg.get(i).get(
      "mode_name").toString().trim());// 建立第四列
    label5 = new jxl.write.Label(4, i + 1, listmsg.get(i).get(
      "type_name").toString().trim());// 建立第五列
    ws.addCell(label1);// 放入工作簿
    ws.addCell(label2);
    ws.addCell(label3);
    ws.addCell(label4);
    ws.addCell(label5);
   }

   // 写入Exel工作表
   wwb.write();
   // 关闭Excel工作薄对象
   wwb.close();

   // 下载
   FacesContext ctx = FacesContext.getCurrentInstance();

   String contentType = "application/x-download";
   HttpServletResponse response = (HttpServletResponse) ctx
     .getExternalContext().getResponse();

   response.setContentType(contentType);
   response.setHeader("Content-Disposition", "attachment;filename="
     + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
   HttpServletRequest request1 = (HttpServletRequest) FacesContext
     .getCurrentInstance().getExternalContext().getRequest();

   ServletOutputStream out = response.getOutputStream();

   byte[] bytes = new byte[0xffff];
   InputStream is = new FileInputStream(new File(realPath));
   int b = 0;
   while ((b = is.read(bytes, 0, 0xffff)) > 0) {
    out.write(bytes, 0, b);
   }
   is.close();
   out.flush();
   ctx.responseComplete();

  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }// 此处建立路径
  return null;
 }

Java学习之道:Java操作Excel之导出下载,布布扣,bubuko.com

Java学习之道:Java操作Excel之导出下载

原文:http://blog.csdn.net/u011511429/article/details/38366771

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!