The Java implementation assigns the Excel template, writes the data in the new file, and downloads

Source: Internet
Author: User

    /*** Generate Excel and download*/     Public voidExportexcel () {File newFile=CreateNewFile (); //file NewFile = new file ("D:/ss.xls"); //new file to write data and download *****************************************************InputStream is =NULL; Hssfworkbook Workbook=NULL; Hssfsheet sheet=NULL; Try{ is=NewFileInputStream (NewFile); Workbook=NewHssfworkbook (IS); //get the first sheetSheet = workbook.getsheetat (0); } Catch(Exception E1) {e1.printstacktrace (); }                if(Sheet! =NULL){            Try {                //Write DataFileOutputStream fos =NewFileOutputStream (NewFile); Hssfrow Row= Sheet.getrow (4); Hssfcell Cell= Row.getcell (1);                System.out.println (Cell.getstringcellvalue ()); Cell.setcellvalue ("Ssssssssssssssssssssssssssssssssssssssssssss");                Workbook.write (FOS);                Fos.flush ();                                Fos.close (); //DownloadInputStream FIS =NewBufferedinputstream (NewFileInputStream (newFile)); HttpServletResponse Response= This. GetResponse (); byte[] buffer =New byte[Fis.available ()];               Fis.read (buffer);               Fis.close ();               Response.reset (); Response.setcontenttype ("Text/html;charset=utf-8"); OutputStream toclient=NewBufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/x-msdownload"); String NewName= Urlencoder.encode ("Purchase Contract" +system.currenttimemillis () + ". xls", "UTF-8"); Response.AddHeader ("Content-disposition", "attachment;filename=\" "+ newName +" \ ""); Response.AddHeader ("Content-length", "" "+newfile.length ());               Toclient.write (buffer);            Toclient.flush (); }            Catch(Exception e) {e.printstacktrace (); }finally {                Try {                    if(NULL!=is )                    {Is.close (); }                } Catch(Exception e) {e.printstacktrace (); }            }        }        //Delete a new file created//This.deletefile (newFile);    }    /*** Copy Files * *@paramS * source file *@paramT * Copy to the new file*/     Public voidfilechannelcopy (file s, file t) {Try{InputStream in=NULL; OutputStream out=NULL; Try{ in=NewBufferedinputstream (NewFileInputStream (s), 1024); out=NewBufferedoutputstream (NewFileOutputStream (t), 1024); byte[] buffer =New byte[1024]; intLen;  while(Len=in.read (buffer))!=-1) {out.write (buffer,0, Len); }            } finally {                if(NULL!=In )                {In.close (); }                if(NULL!=Out )                {Out.close (); }            }        } Catch(Exception e) {e.printstacktrace (); }    }        /*** Read the Excel template and copy it to the new file for writing and downloading *@return     */     PublicFile CreateNewFile () {//read the template and assign a value to the new file ************************************************************//File Template pathString Path = This. Getrequest (). Getrealpath (Systemconfig.filetemplate); String FileName= "Purchasecontract.xls"; File File=NewFile (path+ "/" +fileName); //the path to the saved fileString Realpath =Servletactioncontext.getservletcontext (). Getrealpath (Systemconfig.upload_file_dir); //the new file nameString newfilename = "Purchase Contract" +system.currenttimemillis () + ". xls"; //determine if a path existsFile dir =NewFile (Realpath); if(!dir.exists ())        {Dir.mkdirs (); }        //write to the new ExcelFile NewFile =NewFile (Realpath, NewFileName); Try{newfile.createnewfile (); //copy a template to a new filefilechannelcopy (file, newFile); } Catch(Exception e) {e.printstacktrace (); }        returnNewFile; }    /*** Delete after successful download * *@paramFiles*/    Private voidDeleteFile (File ... files) { for(File file:files) {if(File.exists ()) {file.delete (); }        }    }

The Java implementation assigns the Excel template, writes the data in the new file, and downloads

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.