java excel匯出

來源:互聯網
上載者:User

標籤:excel

看了下同事寫的匯出excel 很簡單   記下來備用

     1、加入必須的jar包 poi-3.10-FINAL-20140208.jar 

     2、建立檔案名稱 “XXXX.xlsx” 

     3、Workbook wb = null;

     4、Workbook wb = new XSSFWorkbook();

     5、Sheet sheet = wb.createSheet(fileName);

     6、String[] title = {"合約編號"}

     7、Row titleRow = sheet.createRow(0);

     8、初始化表頭  

       private void initialiseRow(Row row,String[] title){
for (int i = 0; i < title.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(title[i]);
}
}

    9、填充資料

     for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i+1);
row.createCell((short) 0).setCellValue(list.get(i).getCode());

         }

  10、響應請求

     OutputStream os = null;  
        try {
            os = new BufferedOutputStream(response.getOutputStream());// 取得輸出資料流  
            response.reset();// 清空輸出資料流 
            response.addHeader("Content-disposition","attachment;filename=" 
            + new String( fileName.getBytes("gbk"), "ISO8859-1" ));
            response.setContentType("application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
        logger.error("匯出購保情況的Excel出錯: ", e);
        }


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.