Excel is one of the most common office software. In a Java application, a subset of the data generated in Excel format is an important means of seamlessly connecting with other systems. POI is a sub project of Apache Jakarta, easy to use, very good for Chinese support and powerful in function. The following address is: Http://jakarta.apache.org/poi. This set of APIs is pure Java and does not depend on windows, even if it is running on Linux, it can handle Excel files correctly. Currently downloading the latest installation package from the Internet is the name of Poi-bin-2.5.1-final-20040804.zip, after the file uncompressed, in the generated folder, find the Poi-2.5.1-final-20040804.jar package. Then, place the package under the Web-inf Lib of the application, and restart Tomcat.
Here's an example of creating an Excel workbook with POI:
Hssfworkbook workbook = new Hssfworkbook (); Create a new Excel workbook
Hssfsheet sheet = workbook.createsheet ("JSP"); To build a worksheet in an Excel workbook, named default
Hssfrow row = Sheet.createrow ((short) 0); In the position of index 0 CCB (top line)
Hssfcell cell = Row.createcell ((short) 0); To build a cell at the location of index 0
Cell.setencoding (HSSFCELL.ENCODING_UTF_16); Defines a cell as a string type
Cell.setcellvalue ("author"); Enter some content in a cell
Cell = Row.createcell ((short) 1);
Cell.setencoding (HSSFCELL.ENCODING_UTF_16); Defines a cell as a string type
Cell.setcellvalue ("edit"); Enter some content in a cell
row = Sheet.createrow ((short) 1); Create a row at index 1 (topmost row)
Cell = Row.createcell ((short) 0); Create a cell at index 0 (upper left)
Cell.setencoding (HSSFCELL.ENCODING_UTF_16); Defines a cell as a string type
Cell.setcellvalue ("John"); Enter some content in a cell
Cell=row.createcell ((short) 1);
Cell.setencoding (HSSFCELL.ENCODING_UTF_16); Defines a cell as a string type
Cell.setcellvalue ("Dick");
String filename = Application.getrealpath ("/") + "Text.xls"; FileName is the location of the workbook, stored in the root directory of the current application
FileOutputStream fout = new FileOutputStream (filename); New output file stream
Workbook.write (Fout); Save the corresponding Excel workbook
Fout.flush ();
Fout.close (); End of operation, close file
OUT.PRINTLN ("Excel file has been generated, stored in <BR>" + filename);
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.