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 );
%>