Java Export Excel

Source: Internet
Author: User

Straight into the topic, on the code.

Export Excel
@RequestMapping (value= "Excel")
public void Excel (string excelname,string title, @RequestParam ("District3") string district3, @RequestParam ("District4 ") string District4, @RequestParam (" PName ") string pname, @RequestParam (" ename ") string ename, OutputStream OS, HttpServletResponse response) throws exception{
Response.setcontenttype ("application/vnd.ms-excel");
Response.AddHeader ("Content-disposition", "attachment;filename=\", "+excelname+". Xls\ ");
String Excelname_=new string (excelname.getbytes ("GBK"), "iso8859-1");
Response.AddHeader ("Content-disposition", "attachment; Filename=\ ""
+ Excelname_ + ". Xls\" ");
Writableworkbook workbook = workbook.createworkbook (OS);
Writablesheet sheet = workbook.createsheet (excelname, 0); Generate a worksheet named Excelname, with a parameter of 0 indicating that this is the first page
Writablefont font1= New Writablefont (Writablefont.times,18,writablefont.bold);
Writablecellformat format1=new Writablecellformat (font1);
Format1.setalignment (Jxl.format.Alignment.CENTRE);//Set to center
Format1.setverticalalignment (Verticalalignment.centre); Set Vertical Center;
Format1.setborder (Jxl.format.border.right,jxl.format.borderlinestyle.thin,colour.black);
Format1.setborder (Jxl.format.border.bottom,jxl.format.borderlinestyle.none);
Sets the width for the column;
Sheet.setcolumnview (0,10);
Sheet.setcolumnview (1,35);
Sheet.setcolumnview (2, 35);
Sheet.setcolumnview (3, 15);
Sheet.setcolumnview (4, 15);
Sheet.setcolumnview (5, 15);
Sheet.setcolumnview (6, 35);
Sheet.setcolumnview (7, 35);
Sheet.setcolumnview (8, 20);
Sheet.setcolumnview (9, 35);

Sheet.mergecells (0,0,9,0);
Label Label=new label (0,0,TITLE,FORMAT1); Create a table header, named cell position in the constructor of the label object is the first column first row (0,0)
Sheet.addcell (label);
Sheet.setrowview (0,800);//Set the first row height

Set up a reporting unit
Sheet.mergecells (0,1,9,1);
font1= new Writablefont (Writablefont.times,14,writablefont.bold);
Format1=new Writablecellformat (FONT1);
Format1.setborder (Jxl.format.border.right,jxl.format.borderlinestyle.thin,colour.black);
Format1.setverticalalignment (Verticalalignment.centre); Set Vertical Center;
Label=new Label (0,1, "Filing unit: (stamped)", FORMAT1); Create a table header, named cell position in the constructor of the label object is the first column first row (0,0)
Sheet.addcell (label);
Sheet.setrowview (1,600);//Set the first row height

Sheet.setrowview (2,500);
Font1=new Writablefont (Writablefont.createfont ("_gb2312 in italics"), 12,writablefont.bold);
Writablecellformat format2=new Writablecellformat (font1);
Format2.setalignment (Jxl.format.Alignment.CENTRE);//Set to center
Format2.setverticalalignment (Verticalalignment.centre); Set Vertical Center;
Format2.set
Set border
Format2.setborder (Jxl.format.border.all,jxl.format.borderlinestyle.thin,colour.black);

int startindex=2;
Label=new Label (0,startindex, "serial number", FORMAT2);
Sheet.addcell (label);
Label=new Label (1,startindex, "Walkthrough organizational Unit", FORMAT2);
Sheet.addcell (label);
Label=new Label (2,startindex, "Walkthrough account Name", FORMAT2);
Sheet.addcell (label);
Label=new Label (3,startindex, "Walkthrough Type", FORMAT2);
Sheet.addcell (label);
Label=new Label (4,startindex, "Walkthrough content", FORMAT2);
Sheet.addcell (label);
Label=new Label (5,startindex, "Walkthrough Time", Format2);
Sheet.addcell (label);
Label=new Label (6,startindex, "Walkthrough Site", FORMAT2);
Sheet.addcell (label);
Label=new Label (7,startindex, "units and departments intending to participate in emergency drills", FORMAT2);
Sheet.addcell (label);
Label=new Label (8,startindex, "number of participants to participate in the walkthrough", FORMAT2);
Sheet.addcell (label);
Label=new Label (9,startindex, "remarks", FORMAT2);
Sheet.addcell (label);

/* Generate a cell that holds numbers
The full package path of number must be used, otherwise there is a syntax ambiguity
The cell position is the second column, the first row, and the value is 789.123
Jxl.write.Number number = new Jxl.write.Number (2,0,789.123);
Sheet.addcell (number); */
Jxl.write.Number Number=null;


Querying data from a database
Map<string, string> map=new hashmap<string, string> ();
Map<string, string> like=new hashmap<string, string> ();
if (Stringutils.notnull (DISTRICT3)) {
Map.put ("S.district3", district3);
}
if (Stringutils.notnull (DISTRICT3)) {
Map.put ("S.district4", DISTRICT4);
}
if (Stringutils.notnull (pname)) {
Map.put ("Plansdrillname", pname);
}
if (Stringutils.notnull (ename)) {
Like.put ("S.enterprisename", ename);
}
list<cpplansdrilldto> DTOs = cpplansdrillservice.getstatisticslist (map, like, 1,10000);
Font1=new Writablefont (Writablefont.createfont ("_gb2312 in italics"), 10,writablefont.no_bold);
Format2=new Writablecellformat (FONT1);
Format2.setalignment (Jxl.format.Alignment.CENTRE);//Set to center
Format2.setverticalalignment (Verticalalignment.centre); Set Vertical Center;
Format2.setwrap (TRUE);//Wrap Line
Format2.setborder (Jxl.format.border.all,jxl.format.borderlinestyle.thin,colour.black);
Data saving
for (int i = 0; I <dtos.size (); i++)
{
Cpplansdrilldto M=dtos.get (i);
String data= "";
if (M.getplansdrilldate ()!=null) {
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date date=new java.util.Date ();
Data=sdf.format (date);
}
int pcount=0;
if (Stringutils.notnull (M.getplansdrillcount ())) {
Pcount=integer.parseint (M.getplansdrillcount ());
}
String Deparmetn=stringutils.notnull (M.getplansdrilldepartment ())? M.getplansdrilldepartment (): "";
Number = new Jxl.write.Number (0,I+STARTINDEX+1,I+1,FORMAT2);
Sheet.addcell (number);
Label=new Label (1,i+startindex+1,m.getplansdrillunits (), FORMAT2);
Sheet.addcell (label);
Label=new Label (2,i+startindex+1,m.getplansdrillname (), FORMAT2);
Sheet.addcell (label);
Label=new Label (3,i+startindex+1,m.getplansdrilltype (). Equals ("1")? " On-site Walkthrough ":" Desktop Walkthrough ", FORMAT2);
Sheet.addcell (label);
Label=new Label (4,i+startindex+1,m.getplansdrillcontent (). Equals ("1")? " Single Walkthrough ":" Comprehensive Walkthrough ", FORMAT2);
Sheet.addcell (label);
Label=new Label (5,I+STARTINDEX+1,DATA,FORMAT2);
Sheet.addcell (label);
Label=new Label (6,i+startindex+1,m.getplansdrilladdress (), FORMAT2);
Sheet.addcell (label);
Label=new Label (7,I+STARTINDEX+1,DEPARMETN,FORMAT2);
Sheet.addcell (label);
Number=new Jxl.write.Number (8,I+STARTINDEX+1,PCOUNT,FORMAT2);
Sheet.addcell (number);
Label=new Label (9,i+startindex+1,m.getnote (), FORMAT2);
Sheet.addcell (label);
}

Workbook.write ();
Workbook.close ();
Os.close ();
Response.flushbuffer ();
}

Java Export Excel

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.