Java: Use jexcelapi to create an Excel file

Source: Internet
Author: User

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

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.