Create and read Excel documents with Java

xiaoxiao2021-03-06  71

In order to ensure the operation of the sample program, Java 2 SDK1.4.0 and Jakarta Poi, Jakarta Poi, must be installed, and the Web site is:

http://jakarta.apache.org/poi/

Example 1 will demonstrate how to create an Excel document using Jakarta Poi API.

Example 1 procedure 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 {/ ** Excel file The location you want to store, assume that * / public static string outputfile = "d: / jtest / gongye in the D disk jtest directory. XLS "; public static void main (string argv []) {try {// Create a new Excel workbook hssfworkbook workbook = new hssfworkbook (); // Construction a worksheet in the Excel workbook, which is named default // If you want to create a worksheet that is "benefit indicators", its statement is: // hssfsheet sheet = workbook.createsheet; hssfsheet sheet = workbook.createsheet (); // In index 0 Location Creation Row (Top Rows) HSSFRow Row = Sheet.createrow ((Short) 0); // Create a single element (left upper end) hssfcell cell = rower = rower = Rower Cell ((Short) 0); / / / Define the cell is a string type cell.setcellType (hssfcell.cell_type_string); // Enter some content in cell Cell.SetCellValue ("Add Value"); // New Output File FileOutputStream Fout = New FileoutputStream (OutputFile ); // End the corresponding Excel workbook store Workbook.Write (fout); //, close the file fout.close (); s YSTEM.OUT.PRINTLN ("File Generate ...");

} catch (exception e) {system.out.println ("has run xlcreate ():" e);}}}

Data Example 2 of the Excel document will demonstrate how to read the data in the Excel document. Assume that there is an Excel file named gongye.xls in the D disk jtest directory. Example 2 procedure 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.fileInputStream; Public Class READXL {/ ** Excel file storage location. Note that positive slash * / public static string filetoberead = "d: / jtest / gongye.xls"; public static void main (string argv []) {Try {// Create a reference to Excel workbook files HSSFWorkbook Workbook = New HSSFWorkbook (New fileInputStream (fileInetOberead); // Create a reference to the worksheet. // This example is referenced by name (let us assume that the table has a default name "Sheet1") hssfsheet sheet = workbook.getsheet ("sheet1"); // can also use getSheetat (int index) to use, // In the Excel document, the default index of the first worksheet is 0, // The statement is: hssfsheet sheet = workbook.getsheetat (0); // Read the left upper end unit hssfrow rot = Sheet.Getrow (0); Hssfcell Cell = Row.getcell ((Short) 0); // Output unit content, cell.getstringcellValue () is taken to take the value of the unit system.out.println ("The upper end unit is:" Cell.getstringcellValue ()) } Catch (Exception E) {System.out.println ("Run XLRead ():" E);}}} Setting cell format here, we will only introduce some and format settings related statements, we assume Workbook is a reference to a workbook. In Java

In the first step is to create and set the format of the font and cells, and then apply these formats:

1, create a font, which is disposed in red, 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); cellstyle; cellstyle; cell.setcellType (hssfcell.cell_type_string); cell.setcellValue ("title" );

In short, as in this article, Java programmers don't have to worry about the data in the Excel worksheet, using the Jakarta Poi API, we can easily access the Excel document in the program.

转载请注明原文地址:https://www.9cbs.com/read-108338.html

New Post(0)