/** * Shanghai Automotive Group Finance Co., Ltd. * Copyright (c) 1994-2014 all rightsreserved. */package Com.saicfc.pmpf.internal.manage.utils;import Java.io.file;import Java.io.ioexception;import Java.util.iterator;import java.util.linkedhashmap;import java.util.list;import JXL. Workbook;import Jxl.write.label;import Jxl.write.writablesheet;import Jxl.write.writableworkbook;import Jxl.write.writeexception;import jxl.write.biff.rowsexceededexception;/** * Excel File Generator * @author Lizhiyong * @version $ Id:ExcelUtils.java, v 0.12014 September 11 pm 5:27:18 Exp $ */public class Excelutils {/** * generate an Excel file JXL * @para M FilePath the path of the Excel file to be generated * @param titlelist the header information of the Excel file to be generated * @param dataList details of the Excel file to be generated * @param fil Ename the Excel file name to generate * @param sheetname the sheet file name to generate * @return Excelfile */@SuppressWarnings ("Rawtypes") public static File Writeexcel (string filePath, List titlelist, List dataList, string fileName, String sheetname) { Writableworkbook WWB = null; File excelfile = null; try {File File = new file (FilePath); if (!file.exists ()) {File.mkdir (); System.out.println ("folder created"); }//New Create a JXL file//define file name format and create Excelfile = file.createtempfile (filename, ". xls", new file (FilePath)); The first thing to do is to use the factory method of the Workbook class to create a writable workbook (Workbook) Object WWB = Workbook.createworkbook (excelfile); if (WWB! = null) {//Create a writable worksheet//workbook The Createsheet method has two parameters, the first is the name of the worksheet, and the second is the position of the worksheet in the workbook Writablesheet ws = Wwb.createsheet (sheetname, 0); Add header information int row = 0; for (Iterator Iterator = Titlelist.iterator (); Iterator.hasnext ();) {Linkedhashmap Titlemap = (linkedhashmap) iterator.next (); int cell = 0; for (Iterator titleiterator = Titlemap.entryset (). Iterator(); Titleiterator. Hasnext ();) {Java.util.Map.Entry titleentry = (java.util.Map.Entry) titleiterator.next (); It is important to note that in Excel, the first parameter represents the column, the second represents the row Ws.addcell (new Jxl.write.Label (cell, row, (String) titleent Ry.getvalue ())); cell++; } row++; }//below start adding cell information int rows = Titlelist.size (); for (Iterator Iterator = Datalist.iterator (); Iterator.hasnext ();) {Linkedhashmap DataMap = (linkedhashmap) iterator.next (); int cells = 0; for (Iterator dataiterator = Datamap.entryset (). Iterator (); dataiterator. Hasnext ();) {Java.util.Map.Entry dataEntry = (java.util.Map.Entry) dataiterator.next (); It is important to note that in Excel, the first parameter represents a column, the second represents a row//adds the resulting cell to the workTable Ws.addcell (new Jxl.write.Label (cells, rows, (String) Dataentry.getvalue ())); cells++; } rows++; }//writes the file from Memory Wwb.write (); Close resources, free memory wwb.close (); }} catch (IOException e) {e.printstacktrace (); } catch (Rowsexceededexception e) {e.printstacktrace (); } catch (WriteException e) {e.printstacktrace (); } return excelfile; }}
Here is the call:
String fileName = "Citic Bank (CITIC) refund data"; String sheetname = "Citic Bank (CITIC) refund data"; List titlelist = new ArrayList (); Linkedhashmap Titlemap = new Linkedhashmap (); Titlemap.put ("FileType", "File type:"); Titlemap.put ("FileType1", "Ftb2cbatchpayback"); Titlelist.add (0, Titlemap); Linkedhashmap titleMap2 = new Linkedhashmap (); Titlemap2.put ("Filetitle", "title:"); Titlemap2.put ("FileTitle1", "refund batch"); Titlelist.add (1, TITLEMAP2); Titlelist.add (2, New Linkedhashmap ()); Titlelist.add (3, New Linkedhashmap ()); Linkedhashmap titleMap3 = new Linkedhashmap (); Titlemap3.put ("Title1", "Original payment order number"); Titlemap3.put ("Title2", "Original payment order Date"); Titlemap3.put ("Title3", "refund Amount"); Titlemap3.put ("Title4", "abstract"); Titlelist.add (4, TITLEMAP3); Create Files File = Excelutils.writeexcel (FilePath, Titlelist, ExportData, filename, SheetName); Download file DownLoadFile (response, FilePath, file);
/** * Download file * @param response * @param filePath file path * @param files file * @throws ioexception< c8/>*/public void DownLoadFile (httpservletresponse response, String filePath, file file) throws IOException { String fileName = File.getname (); Download file filemanageutils.exportfile (response, FilePath + filename, filename); Delete a single file filemanageutils.deletefile (FilePath, fileName); }
Package Com.saicfc.pmpf.internal.manage.utils;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream; Import javax.servlet.http.httpservletresponse;/** * File Management * @author Lizhiyong * @version $Id: Filemanageutils.java, v 0.120 September 11, 14 morning 9:37:47 EXP $ */public class Filemanageutils {/** * Download file * @param response * @param csvfilepath * File path * @param filename * file name * @throws IOException */public static V OID Exportfile (httpservletresponse response, String Csvfilepath, String fileName) Throws IOException {Response.setcontenttype ("Applicatio N/CSV;CHARSET=GBK "); Response.setheader ("Content-disposition", "attachment; Filename= "+ New String (Filename.getbytes (" GBK ")," iso8859-1 ")); UrlenCoder.encode (FileName, "GBK") inputstream in = null; try {in = new FileInputStream (Csvfilepath); int len = 0; byte[] buffer = new byte[1024]; Response.setcharacterencoding ("GBK"); OutputStream out = Response.getoutputstream (); while (len = in.read (buffer)) > 0) {//out.write (new byte[] {(byte) 0xEF, (Byte) 0xBB, (byte) 0xBF}); Out.write (buffer, 0, Len); }} catch (FileNotFoundException e) {System.out.println (e); } finally {if (in! = null) {try {in.close (); } catch (Exception e) {throw new RuntimeException (e); }}}}/** * Delete all files under this directory FilePath * @param filePath * File directory path */Publ IC static void Deletefiles (String filePath) {File File = new file (FilePath); if (File.exists ()){file[] files = file.listfiles (); for (int i = 0; i < files.length; i++) {if (Files[i].isfile ()) {files[i].delete (); }}}}/** * Delete individual files * @param filePath * file directory path * @param fi Lename * File name */public static void DeleteFile (String filePath, String fileName) {File File = N EW File (FilePath); if (file.exists ()) {file[] files = file.listfiles (); for (int i = 0; i < files.length; i++) {if (Files[i].isfile ()) {if (Files[i].getnam E (). Equals (FileName) {files[i].delete (); Return } } } } }}
Java generate Ecxel file and download (public)