The project need create the Excel files. I try to use MS Lib in. net but it cost a lot of menory. so I get the JexcelApi and POI to create excel file, it is great.
This page is keep code, nothing else,:>
This is the process Class:
Package jExcel;
Import jxl .*;
Import jxl. write .*;
Import jxl. write. biff. RowsExceededException;
Import java. io .*;
Import java. util .*;
Public class Export {
/**
* Data Items
*/
Private static List <String []> items = new ArrayList <String []> ();
/**
* Generate Items
*
* @ Param Address
* @ Param Zip
*/
Public static void GenerateItem (String Address, String Zip ){
Items. add (new String [] {Address, Zip });
}
/**
* Export To Excel
*
* @ Param filepath
* @ Throws IOException
* @ Throws RowsExceededException
* @ Throws WriteException
*/
Public static void ExportToExcel (String filepath) throws IOException,
RowsExceededException, WriteException {
File file = new File (filepath );
// Check file exsist
If (file. exists ()){
} Else {
File. createNewFile ();
}
// Create a workbook
WritableWorkbook book = Workbook. createWorkbook (file );
// Create a sheet
WritableSheet sheet = book. createSheet ("sheet1", 0 );
// Init a new label
Label label = null;
/**
* Create head line
*/
// Create a new label
Label = new Label (0, 0, "Address ");
// Add label into sheet
Sheet. addCell (label );
// TODO: you can add cell format here like: WritableCellFormat
Label = new Label (1, 0, "Zip ");
Sheet. addCell (label );
// Add item cells
For (int I = 0; I <items. size (); I ++ ){
String [] temp = items. get (I );
For (int s = 0; s <temp. length; s ++ ){
Label = new Label (s, I + 1, temp [s]);
Sheet. addCell (label );
}
}
// Begin to write excel to disk
Book. write ();
// Close this steam
Book. close ();
}
}
This is the main function class:
Import java. io .*;
Import java. util .*;
Import jxl. write. WriteException;
Import jxl. write. biff. RowsExceededException;
Import jExcel .*;
Public class ExApplication {
Static String s = "";
Static BufferedReader input = new BufferedReader (new InputStreamReader (
System. in ));
Static String getFilename (){
String temp = "";
File file = new File (".");
Temp = file. getAbsolutePath (). substring (0,
File. getAbsolutePath (). lastIndexOf ("\\"))
+ "\\";
Date day = new date ();
Temp + = day. tostring (). Replace ("", ""). Replace (":", "") + ". xls ";
Return temp;
}
/**
* @ Param ARGs
* @ Throws ioexception
* @ Throws writeexception
* @ Throws rowsexceededexception
*/
Public static void main (string [] ARGs) throws ioexception,
Rowsexceededexception, writeexception {
System. Out. println ("Please input address, zip ");
While (! S. tolowercase (). Equals ("done ")){
If (S. tolowercase (). indexof (",")>-1 ){
String [] temp = S. tolowercase (). Split (",");
If (temp. Length = 2 ){
Export. GenerateItem (temp [0], temp [1]);
}
}
S = input. readLine ();
}
If (s. toLowerCase (). equals ("done ")){
Try {
String name = getFilename ();
Export. ExportToExcel (name );
System. out. println ("Create Complete File:" + name );
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
}
}