1. We define these three classes first
The DataItem class, which represents a number within a cell
Package Com.tntxia.pem.entity;public class DataItem {private string value;private string Datatype;private string Cellstyle= "";p ublic String Getcellstyle () {return cellstyle;} public void Setcellstyle (String cellstyle) {this.cellstyle = CellStyle;} /** * Empty constructor */public DataItem () {}/** * string parameter constructor, default parameter as this DataItem value */public DataItem (string value) {this.value = value; }public String Getdatatype () {return dataType;} public void Setdatatype (String dataType) {this.datatype = DataType;} Public String GetValue () {return value;} public void SetValue (String value) {this.value = value;}}
DataRow class, which represents a row in Excel, consisting of multiple DataItem
Package Com.tntxia.pem.entity;import Java.util.arraylist;public class DataRow {private arraylist<dataitem> Dataitems=new arraylist<dataitem> ();p ublic arraylist<dataitem> Getdataitems () {return dataItems;} public void Setdataitems (arraylist<dataitem> dataitems) {this.dataitems = Dataitems;} public void Add (DataItem item) {This.dataItems.add (item);} public void Add (String value) {This.dataItems.add (new DataItem (value));} public int size () {return This.getdataitems (). Size ();}}
The DataList class, the data for the entire table, consists of multiple DataRow.
Package Com.tntxia.pem.entity;import Java.util.arraylist;public class DataList {private arraylist<datarow> rows = new Arraylist<datarow> ();p ublic arraylist<datarow> getRows () {return rows;} public void Setrows (arraylist<datarow> rows) {this.rows = rows;} public void Add (DataRow row) {rows.add (row);} public void Remove (int index) {rows.remove (index);} Inject another set of data into the current data set. public void inject (DataList DataList) {for (DataRow row:dataList.getRows ()) {this.rows.add (row);}} Public String toString () {String res = ' {'; for (int i=0;i<rows.size (); i++) {DataRow row = Rows.get (i), res+= "{"; for (int k =0;k<row.size (); k++) {res+= row.getdataitems (). Get (k). GetValue () + ",";} res+= "},";} res+= "}"; return res;} Public string[][] Totwodarray () {arraylist<datarow> DataRows = This.getrows (); String[][] result = new String[datarows.size ()][];int most = 0;for (int i=0;i<datarows.size (); i++) {DataRow row = Dataro Ws.get (i); arraylist<dataitem> items = Row.getdataitems (); if (most<items. Size ()) {most = Items.size ();} result[i]= new string[most];for (int j=0;j<most;j++) {if (J<items.size ()) {DataItem item = Items.get (j); result[i][ J]=item.getvalue ();} Else{result[i][j]= "[null]";}}} return result;}}
2. Write a util class for Excel
Package Com.tntxia.pem;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.util.arraylist;import Java.util.locale;import Org.apache.poi.hssf.usermodel.hssfcell;import Org.apache.poi.hssf.usermodel.hssfrow;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.poifs.filesystem.poifsfilesystem;import Com.tntxia.pem.entity.dataitem;import Com.tntxia.pem.entity.datalist;import Com.tntxia.pem.entity.datarow;import Jxl. Cell;import JXL. Celltype;import JXL. Sheet;import JXL. Workbook;import JXL. Workbooksettings;import Jxl.format.border;import Jxl.format.borderlinestyle;import Jxl.format.Colour;import Jxl.write.label;import Jxl.write.writablecell;import Jxl.write.writablecellformat;import Jxl.write.WritableFont; Import jxl.write.writablesheet;import jxl.write.writableworkbook;import jxl.write.writeexception;/** * Excel Tool class * * @ Author CHENSX * */public class Excelutil {/** Not center bold, no Border inside, black on white, top border */private static Writablecellformat WCF_MBWB = null;/** Center bold, white black word */private static Writablecellformat getwcfmbwb () {if (WCF _MBWB = = null) {Writablefont Wcf_mbwb_font = new Writablefont (Writablefont.createfont ("Arial"), ten, writablefont.bold); try {Wcf_mbwb_font.setcolour (colour.black); WCF_MBWB = new Writablecellformat (Wcf_mbwb_font); Wcf_mbwb.setalignment ( Jxl.format.Alignment.CENTRE); wcf_mbwb.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE); wcf_ Mbwb.setbackground (Colour.white); Wcf_mbwb.setborder (Border.all, borderlinestyle.thin,colour.black); wcf_ Mbwb.setwrap (TRUE);} catch (WriteException e) {e.printstacktrace ();}} return WCF_MBWB;} /** * Create a blank Excel file, if the file already exists, in order to avoid the trouble of overwriting the existing file, directly return */public static void Createexcelfile (String filePath) {File File = new Fil E (FilePath), if (File.exists ()) {return;} else {try {file.createnewfile ();} catch (Exception e) {e.printstacktrace (); return;}} Workbooksettings ws = new Workbooksettings () Ws.setlocale (New Locale ("en", "en"); try {Writableworkbook workbook = Null;workbook = Workbook.createworkbook (file, WS), if (Workbook.getsheets (). Length = = 0) {workbook.createsheet ("1", 0) ;} Workbook.write ();} catch (Exception e) {e.printstacktrace ();}} /** * generates an Excel table */public static void Createexcelfile (String filePath, DataList DataList) DataList the data inside { Writableworkbook workbook = null; File File = new file (FilePath); try {workbook = workbook.createworkbook (file); Writablesheet sheet = null;if (workbook.getsheets (). Length = = 0) {sheet = workbook.createsheet ("sheet", 0);} else {sheet = Workbook.getsheet (0);} arraylist<datarow> rows = Datalist.getrows (); for (int i = 0; i < rows.size (); i++) {DataRow row = Rows.get (i); arraylist<dataitem> items = Row.getdataitems (); for (int j = 0; J < Items.size (), j + +) {DataItem item = Items.get ( j); Label Value_label = null;//If the item has a style set inside, now only the style 1if (Item.getcellstyle (). Equals ("1")) {Value_label = new label (j, I, item . GetValue (), GETWCFMBWB ());} else {Value_label = new label (j, I, Item.getvalue ());} Sheet.adDcell (Value_label);}} Workbook.write ();} catch (Exception e) {e.printstacktrace ();} finally {try {workbook.close ();} catch (Exception e) {e.printstacktrace ();}}} /** * Copy, modify, where File1 is the Copy object, File2 is the object created after the modification * The original formatting of the cell is not removed, we can still add the new cell decoration, so that the contents of the cell will behave in different form * * @param file1 * @param file2 */public static void Modifyexcel (file file1, file file2) {try {Workbook RWB = Workbook.getworkbook (file1); W Ritableworkbook WWB = Workbook.createworkbook (file2, RWB);//Copyint Sheetcount = Wwb.getnumberofsheets (); for (int i = 0; i < Sheetcount; i++) {Writablesheet ws = Wwb.getsheet (i); int rows = Ws.getrows (); for (int k = 0; k < rows; k++) {cell[] Rowcells = WS.G Etrow (k); for (int j = 0; J < Rowcells.length; J + +) {Writablecell WC = Ws.getwritablecell (j, k);//Determine the type of cell, make the appropriate conversion if (W C.gettype () = = Celltype.label) {Label label = WC;IF (Label.getstring (). Equals ("Beijing North Beijing Technology Co., Ltd.") { Label.setstring ("Beijing Yu Xin Yi Cheng Technology Co., Ltd.");}}} Wwb.write (); Wwb.close (); Rwb.close ();}} catch (Exception e) {E.printstacKtrace ();}} public static DataList readdatafromfile (String file, int startIndex) {Poifsfilesystem fs = null; Hssfworkbook WB = null;datalist DataList = new DataList (); try {fs = new Poifsfilesystem (new FileInputStream (file)); WB = NE W Hssfworkbook (FS);} catch (IOException e) {e.printstacktrace (); return null;} Hssfsheet sheet = wb.getsheetat (0); Hssfrow row = null; Hssfcell cell = Null;int Rownum;rownum = Sheet.getlastrownum (); for (int i = startIndex; I <= rowNum; i++) {row = sheet. GetRow (i); if (row = = null) Continue;datarow DataRow = new DataRow (), int cellnum = Row.getlastcellnum (); for (int k = 0; K &L T Cellnum; k++) {cell = Row.getcell ((short) k), if (cell = = null) {Datarow.add (new DataItem (null));} else {if (cell.getcelltype () = = H Ssfcell.cell_type_numeric) {Datarow.add (string.valueof ((int) cell.getnumericcellvalue ()));} else if ( Cell.getcelltype () = = hssfcell.cell_type_string) {Datarow.add (Cell.getstringcellvalue ());}}} Datalist.add (DataRow);} return dataList;} public static DataList ReaddaTafromfiles (int startIndex) {file root = new File ("f:\\exel\\ five-period export record 2"); file[] files = root.listfiles ();D atalist DataList = new DataList (); for (File file:files) {DataList dl = Readdatafromfile (File.getabsolutepath (), StartIndex);d atalist.inject (DL);} return dataList;} public static void Append (file modelfile, file destfile, int modelstart,int pastefilestart) {DataList DataList = Readdataf Romfiles (Pastefilestart); System.out.println (Datalist.getrows (). Size ()), try {Workbook RWB = Workbook.getworkbook (modelfile); Writableworkbook WWB = Workbook.createworkbook (DestFile, RWB); Writablesheet ws = Wwb.getsheet (0); for (int i = 0; i < datalist.getrows (). Size (); i++) {DataRow row = Datalist.getrows ( ). get (i); arraylist<dataitem> dataitems = Row.getdataitems (); for (int k = 0; k < dataitems.size (); k++) {DataItem item = DA Taitems.get (k); Label Itemlabel = new label (k, i + Modelstart, Item.getvalue ()); Ws.addcell (Itemlabel);}} Wwb.write (); Wwb.close (); Rwb.close ();} catch (Exception e) {E.printstaCktrace ();}} public static DataList Readexcel (String filePath) {DataList list = new DataList (); try {fileinputstream in = new fileinputs Tream (FilePath); Workbook WB = Workbook.getworkbook (in); Sheet Sheet = wb.getsheet (0); for (int i = 0; i < sheet.getrows (); i++) {DataRow datarow = new DataRow (); List.add (Dataro W); Cell[] cells = Sheet.getrow (i); for (int k = 0; k < cells.length; k++) {DataItem item = new DataItem (cells[k].getcontent S ());d Atarow.add (item);}}} catch (Exception e) {e.printstacktrace ();} return list;} public static void Main (string[] args) {}}
Java code for generating Excel tables