// Obtain the Template
String dir = servlet. getservletcontext (). getrealpath (
"/Template/test.xls ");
Poifsfilesystem FS = new poifsfilesystem (New fileinputstream (DIR ));
Hssfworkbook WB = new hssfworkbook (FCM );
Hssfsheet sheet1 = WB. getsheetat (0 );
// Set the cell value
//................
// Out to excel
Try {
Response. setcontenttype ("application/MS-Excel; charset =/" UTF-8 /"");
Response. setheader ("content-disposition", "attachment; filename ="
+ "Testout.xls ");
Outputstream out = response. getoutputstream ();
WB. Write (out );
Out. Close ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Exclusive excelutil
Import java. util. date;
Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfcellstyle;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Public class excelutil {
// Set the cell value
Public static void setsheetcellvalue (hssfsheet sheet, int rowindex,
Short colindex, object Value ){
Hssfcell cell = excelutil. ensurecellexist (sheet, rowindex, colindex );
If (value instanceof date ){
// Date type
Cell. setcellvalue (date) value );
} Else if (value instanceof number ){
// Numeric type: Double, float, integer and so on.
Cell. setcellvalue (number) value). doublevalue ());
} Else if (value instanceof string ){
// String type
// String Encoding
Cell. setencoding (hssfcell. encoding_utf_16 );
//
String strvalue = (string) value;
Strvalue = strvalue. replaceall ("/R/N", "/N ");
Cell. setcellvalue (strvalue );
}
}
// Set cell Rules
Public static void setsheetcellformula (hssfsheet sheet, int rowindex,
Short colindex, string formula ){
Hssfcell cell = excelutil. ensurecellexist (sheet, rowindex, colindex );
Cell. setcellformula (formula );
}
// Set the cell format
Public static void setsheetcellstyle (hssfsheet sheet, int rowindex,
Short colindex, hssfcellstyle style ){
Hssfcell cell = excelutil. ensurecellexist (sheet, rowindex, colindex );
Cell. setcellstyle (style );
}
// Obtain the cell format
Public static hssfcellstyle getsheetcellstyle (hssfsheet sheet,
Int rowindex, short colindex ){
Hssfcell cell = excelutil. ensurecellexist (sheet, rowindex, colindex );
Return cell. getcellstyle ();
}
// Obtain a cell. If the cell does not exist, create a new one.
Public static hssfcell ensurecellexist (hssfsheet sheet, int rowindex,
Short colindex ){
Hssfrow ROW = sheet. getrow (rowindex );
If (ROW = NULL ){
Row = sheet. createrow (rowindex );
}
Hssfcell cell = row. getcell (colindex );
If (cell = NULL ){
Cell = row. createcell (colindex );
}
Return cell;
}
// Copy the row rowindex1 format to row rowindex2
Public static void copysheetrowstyle (hssfsheet sheet, int rowindex1,
Int rowindex2 ){
// Check paras
If (rowindex1 = rowindex2 ){
Return;
}
Hssfrow ROW = sheet. getrow (rowindex1 );
If (ROW = NULL ){
Row = sheet. createrow (rowindex1 );
}
Short Cols = row. getlastcellnum ();
Hssfcellstyle style = NULL;
For (short colindex = 0; colindex <Cols; colindex ++ ){
Style = excelutil. getsheetcellstyle (sheet, rowindex1, colindex );
Excelutil. setsheetcellstyle (sheet, rowindex2, colindex, style );
}
}
}