Java Web project moved from Eclipse to tomact server, path changed, export EXCEL,JXL

Source: Internet
Author: User

I use JXL export Excel, relatively simple, the first I was fixed path can not be selected, very limited, and later changed, and fixed the path when the project on the server, the path is not OK.

The help of the great God on the Internet successfully set the response header, and optionally save the path.

1. Front End

Not with Ajax, like Ajax can't succeed
js$ ("#exportbaofei"). Click (function () { window.location.href= "${pagecontext.request.contextpath}/ Exportbaofeiservlet "; });//html<type= "button" value= "Export waste mode" ID = "Exportbaofei"/>

2.servlet

protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {log.info ("Exporting a servlet for a scrap model"); Mujuservice Mujuservice=NewMujuservice (); //Enquirylist<map<string, string>>list =Mujuservice.exportbaofei (); //Frame Selection PathWritableworkbook book =NULL;        Response.reset (); Response.setcharacterencoding ("UTF-8");//Set character sets//Create a workflowOutputStream OS =NULL; Try {            //Set Popup dialog boxResponse.setcontenttype ("Application/dowload"); Response.setcharacterencoding ("UTF-8"); //set the title of a worksheetResponse.setheader ("Content-disposition", "attachment; Filename=feimu_month.xls ");//sets the generated file name, cannot use Chinese characters, does not set Chinese character decodingOS =Response.getoutputstream (); //Initializing a worksheetBook =workbook.createworkbook (OS); } Catch(IOException E1) {Log.error ("An IO exception occurred in export Excel", E1); //throw new Serviceexception ("Export failed", E1);E1.printstacktrace (); }                //Excel format SettingsWritablefont font1=NewWritablefont (Writablefont.createfont ("_gb2312 in italics"), writablefont.no_bold);//format font format supported by Excel Writablefont font3=new Writablefont (Writablefont.createfont ("Italic _gb2312"), writablefont.no _bold);Writablecellformat format1=NewWritablecellformat (FONT1); //Assign horizontal alignment to center        Try{format1.setalignment (Jxl.format.Alignment.CENTRE); //Assign vertical alignment to centerformat1.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE); //set up line wrappingFormat1.setwrap (true); } Catch(WriteException E1) {//TODO auto-generated Catch blockLog.error (E1);        E1.printstacktrace (); }                //Writableworkbook book = null;            Try {                //Open File//Book = Workbook.createworkbook (new File (filename+ ". xls")); //generate a worksheet named "Student", parameter 0 indicates this is the first pageWritablesheet sheet = book.createsheet ("Scrap List of the month", 0); Sheet.addcell (NewLabel (0,0, "ordinal", FORMAT1));  for(inti = 1;i<12;i++) {Sheet.setcolumnview (I,15); } Sheet.addcell (NewLabel (1, 0, "Order Time", FORMAT1)); Sheet.addcell (NewLabel (2,0, "Return Time", FORMAT1)); Sheet.addcell (NewLabel (3,0, "repair number"), FORMAT1)); Sheet.addcell (NewLabel (4,0, "Product drawing number", FORMAT1)); Sheet.addcell (NewLabel (5,0, "Mold drawing number", FORMAT1)); Sheet.addcell (NewLabel (6,0, "Mold Specification", FORMAT1)); Sheet.addcell (NewLabel (7,0, "Repair module number"), FORMAT1)); Sheet.addcell (NewLabel (8,0, "Repair Quantity", FORMAT1)); Sheet.addcell (NewLabel (9,0, "Repair Reason", FORMAT1)); Sheet.addcell (NewLabel (10,0, "scrap modulo number"), FORMAT1)); Sheet.addcell (NewLabel (11,0, "Scrap Quantity", FORMAT1)); if(list!=NULL&&!List.isEmpty ()) {                    introws = 1;// Hang                    intcols = 0;//Lie                     for(Map<string, string>map:list) {Sheet.addcell (NewNumber (0, rows, rows)); Sheet.addcell (NewLabel (1,rows,map.get ("Wx_xdan_data") , FORMAT1)); Sheet.addcell (NewLabel (2,rows,map.get ("Wx_back_data") , FORMAT1)); Sheet.addcell (NewLabel (3,rows,map.get ("Wx_danhao") , FORMAT1)); Sheet.addcell (NewLabel (4,rows,map.get ("Wx_product_num") , FORMAT1)); Sheet.addcell (NewLabel (5,rows,map.get ("Wx_mj_num") , FORMAT1)); Sheet.addcell (NewLabel (6,rows,map.get ("Wx_mj_guige") , FORMAT1)); Sheet.addcell (NewLabel (7,rows,map.get ("Wx_muhao") , FORMAT1)); Sheet.addcell (NewLabel (8,rows,map.get ("Wx_count") , FORMAT1)); Sheet.addcell (NewLabel (9,rows,map.get ("Wx_reason") , FORMAT1)); Sheet.addcell (NewLabel (10,rows,map.get ("Wx_baofei_muhao") , FORMAT1)); Sheet.addcell (NewLabel (11,rows,map.get ("Wx_baofei_count") , FORMAT1)); Rows++;                            }} book.write (); } Catch(Exception e) {log.error (e);            System.out.println (e); }finally{                if(book!=NULL){                    Try{book.close (); } Catch(Exception e) {e.printstacktrace (); }                 }            }            }

Java Web project moved from Eclipse to tomact server, path changed, export EXCEL,JXL

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.