Use Java to create and read Excel documents

Source: Internet
Author: User
To ensure the running of the sample program, Java 2 sdk1.4.0 and Jakarta poi must be installed, and the Jakarta poi web site is: http://jakarta.apache.org/poi/

Example 1 demonstrates how to use the Jakarta poi API to create an Excel document.

Example 1 program is as follows:
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import java. Io. fileoutputstream;
Public class createxl {
/** The location where the Excel file is to be stored. Assume that the file is in the jtest directory of drive D */
Public static string outputfile = "D:/jtest/gongye.xls ";
Public static void main (string argv [])
{
Try
{
// Create a new Excel Workbook
Hssfworkbook workbook = new hssfworkbook ();
// Create a worksheet in an Excel worksheet named the default value
// To create a worksheet named "benefit indicator", the statement is:
// Hssfsheet sheet = Workbook. createsheet ("benefit indicator ");
Hssfsheet sheet = Workbook. createsheet ();
// Create a row (the top row) at index 0)
Hssfrow ROW = sheet. createrow (short) 0 );
// Create a cell at index 0 (upper left)
Hssfcell cell = row. createcell (short) 0 );
// Define cells as strings
Cell. setcelltype (hssfcell. cell_type_string );
// Enter some content in the cell
Cell. setcellvalue ("added value ");
// Create an output file stream
Fileoutputstream fout = new fileoutputstream (outputfile );
// Save the corresponding Excel Workbook
Workbook. Write (fout );
Fout. Flush ();
// The operation ends. close the file.
Fout. Close ();
System. Out. println ("file generation ...");

} Catch (exception e ){
System. Out. println ("xlcreate ():" + E );
}
}
}

Read data from Excel documents
Example 2 demonstrates how to read data from an Excel document. Assume that an Excel file named gongye.xls is under the ddrive jtestdirectory.
Example 2:
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import java. Io. fileinputstream;
Public class readxl {
/** Storage location of Excel files. Note that it is a forward slash */
Public static string filetoberead = "D:/jtest/gongye.xls ";
Public static void main (string argv []) {
Try {
// Create a reference to an Excel Workbook File
Hssfworkbook workbook = new hssfworkbook (New fileinputstream (filetoberead ));
// Create a reference to the worksheet.
// In this example, reference by name (let's assume that the table has the default name "sheet1 ")
Hssfsheet sheet = Workbook. getsheet ("sheet1 ");
// You can also use getsheetat (INT index) to reference data by index,
// In the Excel document, the default index of the first worksheet is 0,
// The statement is: hssfsheet sheet = Workbook. getsheetat (0 );
// Read the upper left Unit
Hssfrow ROW = sheet. getrow (0 );
Hssfcell cell = row. getcell (short) 0 );
// Output unit content. Cell. getstringcellvalue () is used to obtain the value of the unit.
System. Out. println ("upper left Unit:" + cell. getstringcellvalue ());
} Catch (exception e ){
System. Out. println ("xlread ():" + E );
}
}
}

Set cell format
Here, we will only introduce some statements related to format settings. We assume that the workbook is a reference to a workbook. In Java

The first step is to create and set the font and cell format, and then apply the format:

1. Create a font and set it to red or bold:
Hssffont font = Workbook. createfont ();
Font. setcolor (hssffont. color_red );
Font. setboldweight (hssffont. boldweight_bold );
2. Create a format
Hssfcellstyle cellstyle = Workbook. createcellstyle ();
Cellstyle. setfont (font );
3. Application Format
Hssfcell cell = row. createcell (short) 0 );
Cell. setcellstyle (cellstyle );
Cell. setcelltype (hssfcell. cell_type_string );
Cell. setcellvalue ("title ");

In short, as demonstrated in this article, Java programmers do not have to worry about the data in the Excel worksheet. Using Jakarta poi API,

We can easily access the Excel document in the program.

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.