Export an Excel file from a Java Web project and a download box is displayed.

Source: Internet
Author: User

Export an Excel file from a Java Web project and a download box is displayed.
Introduction

Reports are often involved in Java Web development. In a recent project, you need to display the data in the database as a table and export the data as an Excel file.

Related jar packages

Using POI can solve the problem of Excel Import and Export. POI:
Poi-3.6-20091214.jar

Key code

First, import the jar package.
When an excel file is generated, the data source is generally in the form of a List. The code in the Excel format is pasted below:

/*** The following is an Excel creation operation * // 1. create a workbook, corresponding to an Excel file HSSFWorkbook wb = new HSSFWorkbook (); // 2. add a sheet to the workbook, corresponding to a sheet in Excel. createSheet ("XXX table"); // 3. add row 0th to the table header in sheet. in earlier versions, poi has a limit on the number of rows in excel. short HSSFRow row = sheet. createRow (int) 0); // 4. create a cell, set the value header, and set the header to center HSSFCellStyle = wb. createCellStyle (); // The Center style. setAlignment (HSSFCellStyle. ALIGN_CENTER); // sets the header HSSFCell cell = row. createCell (0); cell. setCellValue ("header 1"); cell. setCellStyle (style); cell = row. createCell (1); cell. setCellValue ("header 2"); cell. setCellStyle (style); cell = row. createCell (2); cell. setCellValue ("header 3"); cell. setCellStyle (style); cell = row. createCell (3); cell. setCellValue ("header 4"); cell. setCellStyle (style); cell = row. createCell (4); cell. setCellValue ("header 5"); cell. setCellStyle (style );

After the excel format is generated, write the data to the excel file:

// Cyclically write data into Excel for (int I = 0; I <lists. size (); I ++) {row = sheet. createRow (int) I + 1); List list = lists. get (I); // create a cell and set the value row. createCell (0 ). setCellValue (list. getXXX (); row. createCell (1 ). setCellValue (list. getXXX (); row. createCell (2 ). setCellValue (list. getXXX (); row. createCell (3 ). setCellValue (list. getXXX (); row. createCell (4 ). setCellValue (list. getXXX ());}

Then, the generated Excel file is output as a stream.
* The download box is not displayed.

FileOutputStream out =new FileOutputStream("E:/XXX.xls");wb.write(out); out.close();

* The download box is displayed.

String fileName = "XXX table"; ByteArrayOutputStream OS = new ByteArrayOutputStream (); wb. write (OS); byte [] content = OS. toByteArray (); InputStream is = new ByteArrayInputStream (content); // set the response parameter to open the download page res. reset (); res. setContentType ("application/vnd. ms-excel; charset = UTF-8 "); res. setHeader ("Content-Disposition", "attachment; filename =" + new String (fileName + ". xls "). getBytes (), "iso-8859-1 ") ); ServletOutputStream out = res. getOutputStream (); BufferedInputStream bis = null; BufferedOutputStream bos = null; try {bis = new BufferedInputStream (is); bos = new BufferedOutputStream (out ); byte [] buff = new byte [2048]; int bytesRead; // Simple read/write loop. while (-1! = (BytesRead = bis. read (buff, 0, buff. length) {bos. write (buff, 0, bytesRead) ;}} catch (Exception e) {// TODO: handle exception e. printStackTrace ();} finally {if (bis! = Null) bis. close (); if (bos! = Null) bos. close ();}

After completing the preceding operations, you can jump to other pages.
At the same time, POI can upload and parse the Excel file and display it on the webpage. This is a summary of this article.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.