There are two ways to Operate Excel in Java: poi and jxi. It seems that jxi has better support for Chinese.
1. Use POI as an example:
Import org. Apache. Poi. hssf. usermodel .*;
Import org. Apache. Poi. poifs. filesystem .*;
Import java. Io .*;
Public class write {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try
{
// Todo auto-generated method stub
Hssfworkbook WB = new hssfworkbook ();
Hssfsheet sheet = WB. createsheet ("new sheet ");
Hssfrow ROW = sheet. createrow (short) 1 );
Row. createcell (short) 1). setcellvalue (1.2 );
Row. createcell (short) 2). setcellvalue ("Hellow way ");
Row. createcell (short) 3). setcellvalue ("AAAAA ");
Fileoutputstream fileout = new fileoutputstream ("C: \ aa.xls ");
WB. Write (fileout );
Fileout. Close ();
System. Out. println ("AAA ");
}
Catch (exception E)
{}
}
2 read example:
Customerdao = new customerdao ();
String accounts = (string) request. getsession ()
. Getvalue ("accounts ");
String filetoberead = request. getrealpath ("/")
+ "Customer \ Excel \ upload _" + accounts + ". xls ";
Try {
Hssfworkbook workbook = new hssfworkbook (New fileinputstream (
Filetoberead ));
Hssfsheet sheet = Workbook. getsheetat (0 );
/* Read the information in the file and store the data in the database */
For (Int J = 1; j ++ ){
Hssfrow ROW = sheet. getrow (j );
If (row! = NULL ){
Customerdto = new customerdto ();
Hssfcell cell;
Int I = 0;
/* Skip customerinformationid In the first column */
I ++;
Cell = row. getcell (short) I ++ );
Customerdto. setcompanyname (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setcustomername (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setnameforshort (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto
. Setcompanyaddress (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setcountry (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setprovince (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setcity (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setpostalcode (cell. getstringcellvalue ());
Cell = row. getcell (short) I ++ );
Customerdto. setcompanyhomepage (Cell
. Getstringcellvalue ());
Customerdao. insertcustomer (customerdto );
} Else {
Break;
}
}
} Catch (exception e ){
E. printstacktrace ();
}
For jxi, you can read these two articles.
Http://malipei.javaeye.com/blog/70044
Http://www.alchemistrex.com/article.asp? Id = 138
Http://www.blogjava.net/jhengfei/archive/2006/05/22/47480.html