Excel
This operation uses the POI class library and downloads to the following URL:
http://apache.justdn.org/jakarta/poi/release/bin/poi-bin- 2.5.1-final-20040804.zip
The following is the source code:
Package com.test;
Import Java.io.FileOutputStream;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Java.io.FileInputStream;
Import Javax.swing.JOptionPane;
/**
* Level of an Excel file: Excel file-> worksheet-> row-> cell
* Corresponds to POI, as: Workbook->sheet->row->cell
*/
public class Poitest
{
public static String outputfile = "E:\\test.xls";
public static String Filetoberead = "E:\\test.xls";
public void Createexcel ()
{
Try
{
Create a new Excel workbook
Hssfworkbook workbook = new Hssfworkbook ();
Create a worksheet in an Excel workbook with the name default
To create a new worksheet that is a benefit metric, the statement is:
Hssfsheet sheet = workbook.createsheet ("benefit indicator");
Hssfsheet sheet = workbook.createsheet ("benefit indicator");
Create a row at index 0 (topmost row)
Hssfrow row = Sheet.createrow ((short) 0);
Create a cell at index 0 (upper left)
Hssfcell cell = Row.createcell ((short) 0);
Defines a cell as a string type
Cell.setcelltype (hssfcell.cell_type_string);
Enter some content in a cell
Cell.setcellvalue ("POI Excel Model");
Next line
Row=sheet.createrow ((short) 1);
Cell=row.createcell ((short) 0);
Set cell contents
Cell.setcellvalue ("Powered by Summer");
Create a new output file stream
FileOutputStream fout = new FileOutputStream (outputfile);
Save the appropriate Excel workbook
Workbook.write (Fout);
Fout.flush ();
End of operation, close file
Fout.close ();
SYSTEM.OUT.PRINTLN ("File generation ...");
}
catch (Exception e)
{
System.out.println ("has run Xlcreate ():" + E);
}
}
public void Readexcel ()
{
Try
{
Create a reference to an Excel workbook file
Hssfworkbook workbook = new Hssfworkbook (new FileInputStream (Filetoberead));
System.out
. println ("===sheetsnum===" + workbook.getnumberofsheets ());//Get sheet number
for (int numsheets = 0; numsheets < workbook.getnumberofsheets (); numsheets++)
{
if (null!= workbook.getsheetat (numsheets))
{
Hssfsheet Asheet = Workbook.getsheetat (numsheets); Get a sheet
for (int rownumofsheet = 0; Rownumofsheet <= asheet.getlastrownum (); rownumofsheet++)
{
if (null!= asheet.getrow (rownumofsheet))
{
Hssfrow Arow = Asheet.getrow (Rownumofsheet);
for (short cellnumofrow = 0; Cellnumofrow <= arow.getlastcellnum (); cellnumofrow++)
{
if (null!= Arow.getcell (Cellnumofrow))
{
Hssfcell Acell = Arow.getcell (Cellnumofrow);
System.out.println (Acell.getstringcellvalue ());
}
}
}
}
}
}
}
catch (Exception e)
{
System.out.println ("Readexcelerror" + e);
}
}
public static void Main (string[] args)
{
Poitest poi = new Poitest ();
Poi.createexcel ();
Joptionpane.showmessagedialog (NULL, "file write succeeded", "POI", 1);
Poi.readexcel ();
Joptionpane.showmessagedialog (NULL, "file read successful", "POI", 1);
}
}