Java學習之道:Java操作Excel之匯出下載

來源:互聯網
上載者:User

標籤:ppa   評價   auto   exists   tty   資料   back   and   esc   

頁面放置一個button進行點擊匯出事件
<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之匯出下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.