Jxl exports excel as a download (JSP call)

Source: Internet
Author: User

----------------------- Collectcorrectexp. jsp -------------------------------------------

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "Java. Io. outputstream" %>
<% @ Page import = "com. datacollect. datacollectcorrectexp" %>
<%
Outputstream OS = response. getoutputstream (); // gets the output stream
Response. Reset (); // clear the output stream

// If filename cannot be set in Chinese, an error occurs.
Response. setheader ("content-disposition", "attachment; filename=student.xls"); // set the output file header
Response. setcontenttype ("application/MSExcel"); // defines the output type.
 
Datacollectcorrectexp dccexp = new datacollectcorrectexp ();
Dccexp. createxls (OS );
%>

 

----------------------- Collectcorrectexp. jsp -------------------------------------------

 

Package com. datacollect;

Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. Text. simpledateformat;
Import java. util. arraylist;
Import java. util. date;
Import java. util. List;

Import jxl .*;
Import jxl. format. alignment;
Import jxl. format. border;
Import jxl. format. borderlinestyle;
Import jxl. format. cellformat;
Import jxl. Write. boolean;
Import jxl. Write. label;
Import jxl. Write. number;
Import jxl. Write. writablecellformat;
Import jxl. Write. writablefont;
Import jxl. Write. writablesheet;
Import jxl. Write. writableworkbook;

/**
* Export the summary data offset Template
* @ Author yangning
*
*/
Public class datacollectcorrectexp {

Public void createxls (outputstream OS ){
// Prepare to set the title of the Excel worksheet
String [] Title = {"no.", "Product Name", "product price", "product quantity", "production date", "Origin", "Export "};
Try {
// Obtain the start time
Long start = system. currenttimemillis ();
// Excel output path
// String filepath = "C:/test.xls ";
// Create an Excel worksheet
Writableworkbook WWB;
// Create a new jxlfile to generate test.xls under the C drive
// Outputstream OS = new fileoutputstream (filepath );
WWB = Workbook. createworkbook (OS );
// Add the first worksheet and set the name of the first sheet
Writablesheet sheet = WWB. createsheet ("product list", 0 );
Label label;
For (INT I = 0; I <title. length; I ++ ){
// Label (x, y, z) Where X represents the column x + 1 of the cell, row y + 1, and the content of the cell is Y
// Specify the cell position and content in the sub-object of the label object
Label = new label (I, 0, title [I]);

// Add the defined cells to the worksheet
Sheet. addcell (Label );
}
// Fill the data below
/*
* To save the number to a cell, use jxl. Write. Number.
* The complete path must be used; otherwise, an error may occur.
**/
// Fill in the product number
Jxl. Write. Number = new jxl. Write. Number (0,1, 20071001 );
Sheet. addcell (number );
// Fill in the product name
Label = new label (, "golden pigeon melon seeds ");
Sheet. addcell (Label );
/*
* Defines the public format of the displayed amount
* Jxl automatically performs rounding.
* For example, 2.456 is formatted as 2.46, and 2.454 is formatted as 2.45.
**/
Jxl. Write. numberformat NF = new jxl. Write. numberformat (".");
Jxl. Write. writablecellformat WCF = new jxl. Write. writablecellformat (NF );
// Fill the product price
Jxl. Write. Number NB = new jxl. Write. Number (2.45, WCF );
Sheet. addcell (NB );
// Fill in the number of products
Jxl. Write. Number numb = new jxl. Write. Number (200 );
Sheet. addcell (numb );
/*
* Defines the public format of the display date.
* Example: yyyy-mm-dd hh: mm
**/
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
String newdate = SDF. Format (new date ());
// Fill in the production date
Label = new label (4,1, newdate );
Sheet. addcell (Label );
// Filling Origin
Label = new label (5, 1, "Xi'an, Shaanxi ");
Sheet. addcell (Label );
/*
* Display a Boolean Value
**/
Jxl. Write. boolean bool = new jxl. Write. boolean (6, 1, true );
Sheet. addcell (bool );
/*
* Merge Cells
* Implemented through writablesheet. mergecells (int x, int y, int M, int N );
* Merge rows from column x + 1, row y + 1 to column M + 1, and row n + 1
*
**/
Sheet. mergecells (0, 3, 2, 3 );
Label = new label (, "three cells are merged ");
Sheet. addcell (Label );
/*
*
* Define public font formats
* Obtain a font style as a template
* First obtain the first sheet through web. getsheet (0 ).
* Then obtain the second column of the first sheet. The first line is the font of the "product name ".
**/
Cellformat cf = WWB. getsheet (0). getcell (1, 0). getcellformat ();
Writablecellformat WC = new writablecellformat ();
// Set Center
WC. setalignment (alignment. centre );
// Set the border line
WC. setborder (border. All, borderlinestyle. Thin );
// Set the background color of the cell.
WC. setbackground (jxl. format. colour. Red );
Label = new label (1, 5, "font", WC );
Sheet. addcell (Label );

// Set the font
Jxl. Write. writablefont wfont = new jxl. Write. writablefont (writablefont. createfont (""), 20 );
Writablecellformat font = new writablecellformat (wfont );
Label = new label (, "", font );
Sheet. addcell (Label );

// Write data
WWB. Write ();
// Close the file
WWB. Close ();
Long end = system. currenttimemillis ();
System. Out. println ("---- the time for completing this operation is:" + (end-Start)/1000 );
} Catch (exception e ){
System. Out. println ("--- exception ---");
E. printstacktrace ();
}
}
 
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

}

}

 

========================================================== ======================================

 

Struts action version

Http://blog.163.com/ycyu05@126/blog/static/917133612009731111054968/

 

 

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.