[Java]
Sharebench/* POI implements simple Excel reading and writing, which is similar to jxl! However, when using jxl, you can create a file as a template, but it does not seem to exist in poi. You can only create read Excel files, it can also be writable and readable (but cannot be written as an object created by a template, because data cannot be written into a file ). I haven't found it yet. I wonder if you can write it like this! Let me know when I write it out!
[Java]
The following describes the basic classes:
[Java]
HSSFWorkbook // The workbook is equivalent to the Xcel file.
[Java]
HSSFSheet // worksheet Sheet
[Java]
HSSFRow // a record row
[Java]
HSSFCell // grid */
[Java]
// Below is an example of using poi to implement Excel:
Using Volume package com. poi;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. SQL. ResultSet;
Import org. apache. poi. hssf. usermodel. HSSFCell;
Import org. apache. poi. hssf. usermodel. HSSFClientAnchor;
Import org. apache. poi. hssf. usermodel. HSSFPatriarch;
Import org. apache. poi. hssf. usermodel. HSSFRow;
Import org. apache. poi. hssf. usermodel. HSSFSheet;
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;
Public class JiandanWrite {
Public static void main (String [] args) throws FileNotFoundException {
ReadExcel ();
}
/**
* If you create data using a template, there is only a template, and no data can only be read.
*
*/
Public static void readExcel () throws FileNotFoundException {
// File to be generated
String path = "e:/An exceltemplate-2003)_test.xls ";
// Generate a file using this template
String src = "e:/test/One Capital Trust plan essentials table exceltemplate (2003).xls ";
InputStream ins = new FileInputStream (src );
OutputStream output = new FileOutputStream (path );
HSSFWorkbook hworkBook = null;
HSSFSheet writableSheet = null;
Try {
POIFSFileSystem fs = new POIFSFileSystem (ins );
// This is a read-only Excel Workbook.
HworkBook = new HSSFWorkbook (fs );
WritableSheet = hworkBook. getSheetAt (1 );
// The data is not added. The object used to read the Excel file of the template cannot write data.
HSSFRow row = writableSheet. createRow (5 );
HSSFCell cell = row. createCell (0 );
Cell. setCellValue ("I'm the first in line 5th! ");
System. out. println ("content of the fifth line:" + cell. getStringCellValue ());
/* Row. createCell (0). setCellValue ("111 ");
Row. createCell (1). setCellValue ("222 ");
Row. createCell (2). setCellValue ("333 ");
Row. createCell (3). setCellValue ("eeeeee ");
Row. createCell (4). setCellValue ("123455 ");*/
// Use the object that reads the template Excel file to read the data OK
/* HSSFRow readRow = writableSheet. getRow (0 );
HSSFCell readCell = readRow. getCell (0 );
String value = readCell. getStringCellValue ();
System. out. print ("the first data read:" + value );*/
HworkBook. write (output );
Output. flush ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (output! = Null)
Output. close ();
If (ins! = Null)
Ins. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
} Www.2cto.com
// Directly create an Excel file and write data to it. OK
Public static void createExcel (String [] args) throws FileNotFoundException {
String path = "e:/An exceltemplate-2003)_test.xls ";
String src = "e:/test/One Capital Trust plan essentials table exceltemplate (2003).xls ";
InputStream ins = new FileInputStream (src );
OutputStream output = new FileOutputStream (path );
HSSFWorkbook hworkBook = null;
HSSFSheet writableSheet = null;
Try {
// Excel readable and writable workbook
HworkBook = new HSSFWorkbook ();
WritableSheet = hworkBook. createSheet ("table ");
Byte I = 4;
// Add Sheet data
HSSFRow row = writableSheet. createRow (1 );
HSSFCell cell = row. createCell (0 );
Cell. setCellValue ("I'm the first in line 5th! ");
// Row. createCell (0). setCellValue ("111 ");
Row. createCell (1). setCellValue ("222 ");
Row. createCell (2). setCellValue ("333 ");
Row. createCell (3). setCellValue ("eeeeee ");
Row. createCell (4). setCellValue ("123455 ");
System. out. println ("content of the fifth line:" + cell. getStringCellValue ());
HworkBook. write (output );
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
Output. close ();
Ins. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}