Write an Excel file in JSP

Source: Internet
Author: User
Excel is one of the common office software. In a Java application, generating part of the data into an Excel format is an important means of seamless connection with other systems. Poi is a sub-project of Apache Jakarta. It is easy to use, supports Chinese characters, and has powerful functions. The following address is http://jakarta.apache.org/poi. This API is pure Java and does not depend on Windows systems. Even if it runs in Linux, it can process Excel files correctly. Currently download the latest package name from the network is poi-bin-2.5.1-final-20040804.zip, unzip the file, in the generated folder, find the poi-2.5.1-final-20040804.jar package. Then place the package under the Lib of the application's WEB-INF and restart tomcat.

The following is an example of creating an Excel worksheet using poi:

<% @ Page contenttype = "text/html; charset = gb2312" %>

<% @ Page import = "Java. Io. *, org. Apache. Poi. hssf. usermodel. *" %>

<%

Hssfworkbook workbook = new hssfworkbook (); // create a new Excel worksheet

Hssfsheet sheet = Workbook. createsheet ("jsp"); // create a worksheet in the Excel worksheet named default

Hssfrow ROW = sheet. createrow (short) 0); // create a line at index 0 (the top row)

Hssfcell cell = row. createcell (short) 0); // create a cell at index 0

Cell. setencoding (hssfcell. encoding_utf_16); // defines the cell as a string type.

Cell. setcellvalue ("author"); // enter some content in the cell

Cell = row. createcell (short) 1 );

Cell. setencoding (hssfcell. encoding_utf_16); // defines the cell as a string type.

Cell. setcellvalue ("edit"); // enter some content in the cell

Row = sheet. createrow (short) 1); // create a row (the top row) at Index 1)

Cell = row. createcell (short) 0); // create a cell at index 0 (upper left)

; Cell. setencoding (hssfcell. encoding_utf_16); // defines the cell type as a string.

 

Cell. setcellvalue ("Michael"); // enter some content in the cell

Cell = row. createcell (short) 1 );

Cell. setencoding (hssfcell. encoding_utf_16); // defines the cell as a string type.

Cell. setcellvalue ("Li Si ");

String filename = application. getrealpath ("/") + "text.xls"; // filename is the storage location of the workbook, which is stored in the root directory of the current application.

Fileoutputstream fout = new fileoutputstream (filename); // creates an output file stream.

Workbook. Write (fout); // Save the corresponding Excel worksheet

Fout. Flush ();

Fout. Close (); // The operation ends. close the file.

Out. println ("the Excel file has been generated and stored in <br>" + filename );

%>

Related Article

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.