Use JSP to export Excel files and use poi and jxl

Source: Internet
Author: User
Tags dateformat

Guidance:
  Use JSP to export Excel files and use poi and jxl
Use poi
  
  
  
  
  
  
Response. setcontenttype ("application/OCTET-STREAM ");
  
  
Response. setheader ("content-disposition ",
  
  
"Attachment; filename =/" test.xls "+
  
  
  
  
"/"");
  
  
  
  
Hssfworkbook WB =
Newhssfworkbook ();
  
  
Hssfsheet sheet = WB. createsheet ("sheet1 ");
  
  
  
  
// Write the table header as follows
  
  
// The header is the first line
  
  
Hssfrow ROW = sheet. createrow (short) 0 );
  
  
  
  
Hssfcell cell1 = row. createcell (short) 0 );
  
  
Hssfcell cell2 = row. createcell (short) 1 );
  
  
Hssfcell cell3 = row. createcell (short) 2 );
  
  
  
  
Cell1.setencoding (short) 1 );
  
  
Cell1.setcelltype (1 );
  
  
Cell2.setencoding (short) 1 );
  
  
Cell2.setcelltype (1 );
  
  
Cell3.setencoding (short) 1 );
  
  
Cell3.setcelltype (1 );
  
  
  
  
// Define the content of the header
  
  
Cell1.setcellvalue ("test ");
  
  
Cell2.setcellvalue ("Test 2 ");
  
  
Cell3.setcellvalue ("Test 3 ");
  
  
For (INTI =
0i 4I ++)
  
{
// Define the data starting from the second row
Row = sheet. createrow (short) I +
1 );
Cell1 = row. createcell (short) 0 );
Cell2 = row. createcell (short) 1 );
Cell3 = row. createcell (short) 2 );
Cell1.setencoding (short) 1 );
Cell1.setcelltype (1 );
Cell2.setencoding (short) 1 );
Cell2.setcelltype (1 );
Cell3.setencoding (short) 1 );
Cell3.setcelltype (1 );
// Fill in the content
Cell1.setcellvalue ("ggg ");
Cell2.setcellvalue ("00000 ");
Cell3.setcellvalue ("adfasdf ");
}
  
  
WB. Write (response. getoutputstream ());
  
  
Response. getoutputstream (). Flush ();
  
  
Response. getoutputstream (). Close ();
  
  
%>
Poi Pack: http://www.apache.org/dyn/closer.cgi/jakarta/poi/
Use jexcelapi to dynamically generate Excel documents
First, go to http://www.andykhan.com/jexcelapi/index.htmlto download the Java Excel API. The homepage also provides a detailed introduction.
The latest version is 2.4.3,
You can also go.
After the download is complete, we need to add the jxl. jar in the file to your development classpath.
Is the Excel file to be produced now:
  
  
The Code is as follows:
File Excel = new file ("D:/aming.xls ");
If (! Excel. exists ()){
Excel. createnewfile ();
}
Writableworkbook WWB = Workbook. createworkbook (Excel );
Writablesheet Ws = WWB. createsheet ("testexcel", 0 );
Label lable = NULL;
  
// Excellent support for Chinese Characters
Lable = new label (0, 0, "My Chinese heart ");
WS. addcell (lable );
  
// You can define a template to format your cell.
Writablefont WF = new writablefont (writablefont. Arial, 10, writablefont. no_bold, false, underlinestyle. no_underline, colour. Black );
Writablecellformat WCF = new writablecellformat (WF );
WCF. setbackground (colour. White );
Lable = new label (0, 1, "fdsl", WCF );
WS. addcell (lable );
  
WF = new writablefont (writablefont. Times, 18, writablefont. Bold, true );
WCF = new writablecellformat (WF );
Lable = new label (0, 2, "aming", WCF );
WS. addcell (lable );
  
// The cell type can also be defined as a number.
Number NB = new number (21.4321321 );
WS. addcell (NB );
  
// Supports formatting your numeric string
Numberformat NF = new numberformat ("#.###");
WCF = new writablecellformat (NF );
NB = new number (21.43254354354354, WCF );
WS. addcell (NB );
// The cell type can be boolean.
Boolean BL = new Boolean (0, 5, true );
WS. addcell (BL );
// The cell type can also be date or time
Datetime dt = new datetime (0, 6, new date ());
WS. addcell (DT );
// Format your date.
Dateformat df = new dateformat ("mm dd yyyy hh: mm: SS ");
WCF = new writablecellformat (DF );
Dt = new datetime (0, 7, new date (), WCF );
WS. addcell (DT );
  
// Start writing the file
WWB. Write ();
WWB. Close ();
The above cannot be opened.
Download To http://prdownloads.sourceforge.net/jexcelapi

This article is transferred from
Http://www.loohost.com/thread-1311-1-1.html

Related Article

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.