Combine online article, practice code. 1. Create an Excel table =============================================================================================================================================================================== .Hssfsheet; import org.apache.poi.hssf.usermodel.hssfrow; Import Org.apache.poi.hssf.usermodel.hssfcell; Import Java.io.FileoutputStream; Public Class CreateXL {
/ ** Excel file is stored, assume that in the D disk jtest directory * /
Public static string outputfile = "d: /jtest/ss.xls";
Public static void main (String Argv []) {
Try {
// Create a new Excel workbook
HSSFWORKBOOK WORKBOOK = New HSSFWORKBOOK ();
// Construct a worksheet in the Excel workbook, which is known as the default value // to create a worksheet that is "benefit indicators", whose statement is: // hssfsheet sheet = workbook.createsheet ("Benefits Indicators ");
HSSFSHEET Sheet = Workbook.createsheet ();
/ / Create a line in the location of index 0 (top row)
HSSFrow Row = Sheet.createrow ((Short) 0);
/ / Create a single element (left upper end) hssfcell cell = row.createcell (short) 0); // Define the cell type Cell.setcellType (hssfcell.cell_type_string); cell.setencoding (Hssfcell .Encoding_utf_16); // Enter some content in cell Cell.setcellValue ("Add value"); // Newly build an output file stream FileOutputStream Fout = New FileOutputStream (OutputFile); // Put the corresponding Excel workbook storage Workbook. Write (fout); fout.flush (); // end, close file fout.close (); system.out.println ("File Generate ...");
} catch (exception e) {system.out.println ("has run xlcreate ():" e);}}}
2. Read the Excel form =============================================== .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 {/ * * The storage location of the Excel file. Note that positive slash * / public static string filetoberead = "d: /jte/ss.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.