Package com.bailin.framework.util.excel;
Import java.util.ArrayList;
Import java.util.List;
/** * * * @author CJ * * */public class Row {
Private list<column> columnlist = new arraylist<column> (); Private String ColumnName;
Public list<column> getcolumnlist () {return columnlist; }
public void Setcolumnlist (list<column> columnlist) {this.columnlist = columnlist; }
public void appendcolumn (column column) {columnlist.add (column); }
Public String getColumnName () {return columnName; }
public void Setcolumnname (String columnName) {this.columnname = ColumnName; }
}
Package com.bailin.framework.util.excel;
/** * Column * @author CJ * */
public class Column {
private int columnnum;
Private String ColumnLabel;
public int Getcolumnnum () {
return columnnum; }
public void Setcolumnnum (int columnnum) {this.columnnum = Columnnum; }
Public String Getcolumnlabel () {return columnlabel; }
public void Setcolumnlabel (String columnlabel) {This.columnlabel = ColumnLabel; }
}
Package com.bailin.framework.util.excel;
Import java.util.ArrayList; Import java.util.List;
/** * Page * @author CJ * */
public class Sheet {
Public Sheet () {
}
Public Sheet (list<row> listrow,string name) {
This.rowlist = ListRow; THIS.name = name;
} private List<row> rowlist = new arraylist<row> ();
private String name;
Public list<row> getrowlist () {
return rowlist; }
public void Setrowlist (list<row> rowlist) {
This.rowlist = rowlist; }
Public String GetName () {
return name; }
public void SetName (String name) {
THIS.name = name; }
public void Appendrow (row row) {
Rowlist.add (row); }
}
Package com.bailin.framework.util.excel;
Import Java.io.File; Import java.util.ArrayList; Import java.util.List;
Import Javax.swing.JFileChooser;
Import Org.zkoss.zul.Listbox; Import Org.zkoss.zul.Listhead; Import Org.zkoss.zul.Listheader;
/** * Add head * * @author CJ * */public class Exportexcel { /** * set head * @param ListBox page ListBox * @return Line */ public static row Addpendtitle (ListBox listbox) {&NBSP;&NB Sp Row row = new row (); Row List<Column> columnlist = new arraylist<column> ();//Save all columns listhead s = Listbox.getlisthead (); List<Listheader> j = S.getchildren ();//Get to head child node for (int i = 0; i < j.size (); i++) {//Traversal columns column column = new column ();//Column column.setcolumnlabel (J.get (i). Getlabel ( )); column.setcolumnnum (i); columnlist.add (column);//Add to column } row.setcolumnlist (Columnlist); row.setcolumnname ("table header"); return Row; }
/** * Pop-up Select File Save location * @return File Save location */public static string filename () {string filename = "d://export Excel";//Jfilec Hooser dialog = new JFileChooser (); Dialog.setcurrentdirectory (New File ("e://"));//Set default open Path//Dialog.setdialogtitle ("store report in"); Dialog.setfileselectionmode (jfilechooser.files_only); Dialog.setdialogtype (Jfilechooser.save_dialog); int result = Dialog.showsavedialog (dialog); if (result = = Jfilechooser.approve_option) {//File File = Dialog.getselectedfile ();//FileName = File.getabsolute Path (); Get the full name of the file//} return filename; } }
Package com.bailin.framework.util.excel;
Import Java.io.File; Import Java.io.FileOutputStream; Import Java.util.Iterator; Import java.util.List;
Import org.zkoss.zul.Filedownload;
Import JXL. Workbook; Import Jxl.format.UnderlineStyle; Import Jxl.write.WritableFont; /** * Output Excel format * @author CJ * */public class Excel { public static Boolean write (List<s heet>sheetlist,string filename,int width) { jxl.write.writableworkbook wwb; Jxl.write.WritableCellFormat wff1=new Jxl.write.WritableCellFormat (); try { FileOutputStream OS = new FileOutputStream (filename+ ". xls"); File output location wff1.setalignment (Jxl.format.Alignment.CENTRE); Set the cell in the play WWB = Workbook.createworkbook (OS); int i = 0; for (Iterator<sheet>itr = SheetlIst.iterator () Itr.hasnext ();) { Sheet Sheet = Itr.next (); int RowNum =0; jxl.write.WritableSheet ws = Wwb.createsheet (Sheet.getname (), i); for (Iterator<row >sheetitr = Sheet.getrowlist (). iterator (); Sheetitr.hasnext ();) { Row row = Sheetitr.next (); int colunnum = 0; for (Iterator<column>rowiterator=row.getcolumnlist (). Iterator (); Rowiterator.hasnext ();) { ws.setcolumnview (colunnum, width);//Setting Column width Column column = Rowiterator.next (); if (null! = Row.getcolumnname () && row.getcolumnname (). Equals ("Table header")) { jxl.write.writablefont wf2=new Jxl.write.WritableFont (writablefont.arial,10, writablefont.bold,false, underlinestyle.no_underline,jxl.format.colour.black); Set font style jxl.write.writablecellformat wf1=new Jxl.write.WritableCellFormat (WF2); wf1.setalignment (Jxl.format.Alignment.CENTRE); Set up a cell in the play ws.addcell (New Jxl.write.Label (Colunnum,rownum, Column.getcolumnlabel (), WF1); }else{ ws.addcell (New Jxl.write.Label (Colunnum,rownum,column.getcolumnlabel (), wff1)); } colunNum++; } rownum++; } i++; } try { wwb.write (); wwb.close (); os.close (); file file = new file (filename+ ". xls"); filedownload.save (file , "application/vnd.ms-excel"); return true; } catch (Exception e) { e.printstacktrace (); return false; } } catch (Exception e) { e.printstacktrace (); return false; } }
}
Package com.bailin.framework.util.excel;
Import Java.io.File; Import java.util.ArrayList;
Import Javax.swing.filechooser.FileFilter; /** * Filter File * @author CJ * */public class Textfilefilter extends FileFilter { private Arrayl ist<string> extensions = new arraylist<string> (); private arraylist<string> descriptions = new arraylist<string> (); public Textfilefilter () { super (); } public textfilefilter (String extension , String description) { super (); this.extensions.add (extension); This.descriptions.add (description); } @Override public Boolean accept (File pathname) { if (pathname! = null) { &nbs P; if (Pathname.isdirectory ()) { return true; } String Extension = getextension (pathname); for (int i=0; i<extensions.size (); i++) { if (Extensions.get (i). toLowerCase (). EndsWith (Extension.tolowercase ())) { return true; } } } return false; }
Private String getextension (File pathname) {if (pathname! = null) {String filename = pathname.getname (); int i = Filename.lastindexof ('. '); if (i > 0 && i < filename.length ()-1) {return filename.substring (i). toLowerCase (); }} return null; }
@Override public String getdescription () {return descriptions.get (Descriptions.size ()-1); } }
ZK implementation Export Excel report