As long as there is a form, there will be Microsoft Excel, and the data with Microsoft Excel has become a lot of people's habits. JAKARTA POI API provides a magical path to the Microsoft document format for Java programmers, where the most mature is to access the HSSF API of the Microsoft Excel document.
This article will exemplify how to create and read Excel documents with Java, and set cell fonts and formats.
In order to ensure the operation of the sample program, Java 2 SDK1.4.0 and Jakarta Poi, Jakarta Poi, Jakarta Poi, are: http://jakarta.apache.org/poi/
Creating Excel Document Example 1 How to deliver an Excel document using Jakarta Poi API. Example 1 The 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 {/ ** Excel file to be stored, assume that in the D disk jtest directory * / public static string outputfile = "d: / jtest / gongye.xls"; public static void main (string argv []) {try {// Create a new Excel workbook hssfworkbook workbook = new hssfworkbook (); // Built a work table in the Excel workbook, named the default // To create A worksheet for "benefit indicators", whose statement is: // hssfsheet sheet = workbook.createsheet; hssfsheet sheet = workbook.createsheet (); // Create row in the index 0 position (most Top lines) HSSFROW ROW = Sheet.Createrow ((Short) 0); // Create a single element (left upper end) hssfcell cell = row.createcell (short) 0); // Define the cell String type cell.setcellType (hssfcell.cell_type_string); // Enter some content in cell Cell.setcellValue ("Add value"); // New output file stream FileOutputStream Fout = new fileoutputstream (OutputFile); // The corresponding Excel workbook storage Workbook.write (fout); fout.flush (); // End, close file fout.close (); syste M.out.println ("File Generate ...");} catch (exception e) {system.out.println ("Run XLCReate ():" E);}}} Read the data in the Excel document Example 2 will demonstrate how to read data in the Excel document. Assume that there is an Excel file named gongye.xls in the D disk jtest directory. Example 2 The 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.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, the first step is to create and set the format of the font and cells, then apply these formats: 1. Create a font, set it for red, bold: hssffont font = workbook.createfont (); font .SetColor (hssffont.color_red); font.setBoldWeight (HSSFFont.BoldWeight_Bold); 2, create 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, Java, Java, 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.
Author Blog: