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 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();
}
}
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 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();
}
}
}
}
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();
}
}
}
}