/*** 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