Export to excel (LP) based on the template)

Source: Internet
Author: User

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

 

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.