Artikel ini terutama berbagi contoh kode lengkap dari implementasi Java yang menghasilkan header tabel pohon excel. Tidak ada yang perlu dijelaskan, jadi lihat saja proses kode.
Format Data Sumber:
String [] targetNames = {"nama indikator", "unit", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "Mei 2017_May Business Income_cumulative", "Mei 2017_May Bisnis 2017 Income_Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year -Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Y "Juni 2017_Main Business Income_year-year", "Juni 2017_Main Business Income_This Month", "Juni 2017_Main Business Income_moon-month", "Juni 2017_profit_cumulative", "Juni 2017_Profit_Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Y ear-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-YearMenghasilkan excel berikut:
Baris pertama bukan milik header pohon.
Kode
SplitCell:
paket com.zzj.excel; kelas publik splitCell {private string key; private string parentKey; nilai string pribadi; private int columnIndex; private int rowIndex; public splitCell () {} public splitCell (string key, nilai string) {this.key = this.value = value;} public splitCell (string Key, string value, this. this.parentkey = parentkey; this.value = nilai; this.columnIndex = ColumnIndex; this.rowIndex = rowIndex;} public string getKey () {return key;} public void setKey (tombol string) {this.key = key;} public string getParentKey () {return parentkey;} public void setParentKey (string ParentKey) {this.parentkey = ParentKey;} public string getValue () {this.parentkey = ParentKey;} public string getValue () {{this. = value;} public int getColumnIndex () {return columnIndex;} public void setColumnIndex (int columnIndex) {this.columnIndex = columnIndex;} public int getRowIndex () {returnindex;} public void setrowIndex (int rowindex) {this.rowindex = rowindex; [key = " + key +", parentKey = " + parentkey +", value = " + value +", columnIndex = " + columnIndex +", rowindex = " + rowindex +"] ";}}MergedCell:
Paket com.zzj.excel; kelas publik gabungancell {tombol string privat; private string parentkey; nilai string pribadi; private int startc; private int endc; private int startr; private int endr; private boolean leaf = true; // node leaf default public get getkey () {return key;} public void setKey (string string key) {this.key = key () {return key;} public void setKey setKey (string key) {this.key = key () {return key public void setKey public setKey (string key) {this.key = KEY () {return key; void publik setKey setKey (string key) {this. setParentKey (string parentKey) {this.parentkey = parentkey;} public string getValue () {value return;} public void setValue (nilai string) {this.value = value;} public int getStartc () {return startc;} public void setStartc (int startc) {startc. void setendc (int endc) {this.endc = endc;}/*** sel gabungan sel gabungan indeks self-increment*/public void incendc () {this.endc ++;} public int getStartr () {return startr;} public void setStartr (int startr) {this.startrer = startr;} public void {int startr) {this.startr = startr; startr; setendr (int endr) {this.endr = endr;} public boolean isleaf () {return leaf;} public void setleaf (boolean leaf) {this.leaf = leaf;}@override public string toString () {return "cellInfo [key =" + Key + ", Parentkey =" + Parentkey,) {cellInfo [KEY = " + KEY +", Parentkey = " + Parentkey,) {cellInfo [KEY =" + KEY + ", Parentkey =" + Parentkey, Nilai ", CellInfo [KEY =" + KEY + ", Parentkey =" + Parentkey ", Nilai =" Nilai + "Nilai +" Nilai + "Nilai +", Nilai + "Nilai +" Nilai + "Nilai +", Nilai + ", Nilai +", endc = " + endc +", startr = " + startr +", endr = " + endr +", Leaf = " + Leaf +"] ";}}CellTransformer:
package com.zzj.excel;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;public class CellTransformer {private final List<SplitCell> cellContents;private final int firstRowIndex;private final int rowSize;private Map<String, MergedCell> cellInfoMap = new LinkedHashMap<String, MergedCell> (); Public CellTransformer (Daftar <PlitTCell> CellContents, int firstrowIndex, int rowsize) {this.cellContents = cellContents; this.firstrowIndex = firstrowindex; this.rowsize = bilasan;} peta publik <string, gabscell> {this.rowsize = bilasan;} peta publik <string, gabungan gabungan> {) {) {) {{{) {{{) {{{{{{) {{{{{) cellContents) {gabungan cellinfo = cellinfomap.get (cellcontent.getKey ()); if (cellinfo == null) {cellinfo = convertTocellinfo (cellcontent); cellinfomap.put (cellinfo.getKey (), cellinfo); */cellInfo.incendc (); // Colom End Index-Increase Self-Increase (Digunakan Untuk Penggabungan Kolom) Cellinfo.setleaf (false); // Selama tampak berulang kali, itu adalah node non-daun}} // gabungan noode (cellinfo) (cellinfomap.valuees () {ifels) {ifelfo) (cellinfomap.values ()) {ifod () {ifodo) (cellinfo: cellinfomap.valuees () {ifod () {ifod (cellinfo) (cellinfomap.valueS () {) {api) {api) {cellinfo) (cellinfo (cellinfomap () {noode () {cellinfo) itu harus digabungkan ke baris terakhir cellinfo.setendr (firstrowindex + rowsize - 1);}} return cellinfomap;} private mergedcell convertTocellinfo (splitcell cellcontent) {mergedcell cellinfo = new (new cellcell) {mergedcell cellinfo = Gabungan (); cellinfo.setKey (cellContent.getKey ()); cellinfo.setParentKey (cellContent.getParentKey ()); cellinfo.setValue (cellcontent.getValue ()); cellinfo.setStartc (cellContent.getColumnindex ()); cellinfo.setStartc (cellContent.getColumnindex ()); cellinfo.setStartc (cellContent.getColumnindex ()); cellinfo. cellInfo.setendc (cellContent.getColumnIndex ()); cellinfo.setStartr (cellContent.getRowIndex ()); // default indeks akhir ke indeks start cellinfo.setendr (cellcontent.getRowIndex ()); return cellinfo;}}tes
Paket com.zzj.excel; impor java.io.file; impor java.util.arraylist; impor java.util.list; impor java.util.map; impor jxl.workbook; import jxl.format.cellformat; import jxl.write.label; import jxl.format.cellformat; jxl.write.writableSheet; import jxl.write.writableworkbook; kelas publik utama {private static final string pemisah = "_"; public static void main (string [] args) melempar pengecualian {string [] targetNames = {"nama indikator", "unit", "xx_yy1," "," "xxx", "XX_YY1," "," XX_YY1, "", "XX_YY1," XX_Y "," XX_YY1 "," XX_Y1, "XX_YY1," XX_YY1 "," XX_YY1 "," XX_YY1 "," "," 2017_May Business Income_cumulative "," Mei 2017_May Business Income_Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year -Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Y "Mei 2017_profit_This Month", "Mei 2017_profit_moq", "Juni 2017_Main Business Income_cumulative", "Juni 2017_Main Business Income_year-Year", "Juni 2017_Main Business Income_Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Ye ar-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Y extranames.length; i ++) {extranames [i] = "extra"+i;} final int firsttreerowIndex = 1; int rowsize = getRowSize (targetNames); Daftar <splitCell> cellContents = new ArrayList <> (); for (int i = 0; i <targetnames.length; i ++) {string); nilai = nilai = nilai = nilai = nilai (targetnames.length; <values.length; SplitCell cellContent = new splitCell (kunci, parentkey, value, i, j + firsttreerowIndex); cellContents.add (cellContent);}} workworkbook workbook = workbook.createworkBook (file baru ("f: //template.xls")); cellFormat cellFormat = getCellFormat (); writableSheet sheet = workbook.createsheet ("Template", 0);/pight (i i iN; {Label label = label baru (i, 0, extranames [i], cellFormat); sheet.addcell (label);} // tree header cellTransformer cellInfomanager = cellTransformer baru (cellContents, firsttreerowIndex, rowsize); peta <string, redcell> peta = cellinfomaner. untuk (cellinfo celledcell: map.values ()) {label label = label baru (cellinfo.getStartc (), cellinfo.getStartr (), cellinfo.getValue (), cellFormat); if (cellinfo.getStartc ()! = cellinfo.getFormat () || cellInfo.getEndR ()) {sheet.mergecells (cellinfo.getStartc (), cellinfo.getStartr (), cellinfo.getendc (), cellinfo.getendr ();} sheet.addcell (label);} workbook.write (); cleme.close) (label);} workbook.write (); clote.close (clape) (label);} workbook.write (); workbook.close. berhasil! ");} private static cellFormat getCellFormat () melempar Exception {writableFont font = new WritableFont (writableFont.arial, 10, writableFont.bold); writableCellFormat cellFormat = WritableCellFormat (); cellFormat.setFont (font); cellFormat.setAlignment (jxl.format.alignment.centre); cellFormat.setVerticalAnlignment (jxl.format.verticalAnlignment.centre); cellFormat.setwrap (false); cellformat return; {cellFormat; {{cellFormat); cellFormat.setwrap (false); cellFormat; {cellFormat; {{cellFormat; {cellFORCATIC); cellFormat.setWrap (false); cellFormat; {cellFormat; {} cellOnCeCatic); cellFormat 0; untuk (string t: targetNames) {rowsize = math.max (rowsize, t.split (pemisah) .length);} return rowsize;} string statis private getKey (string [] nilai, indeks int) {stringBuffer sb = new stringBuffer (); for (int i = 0; i <(indeks +1); s stringbuffer (); for (int i = 0; i <(ix; index +sb = new stringBuffer (); for (int i = 0; i <(ix; index +SB = new StringBuffer (); for (int i = 0; i <(ix; index +sb = new stringBuffer (); for (int i = i = i <(i <x; Pemisah);} sb.deleteCharat (sb.length () - 1); return sb.toString ();} private static string getParentKey (string [] nilai, indeks int) {if (index == 0) {return null;} return getkey (nilai, indeks - 1);}}Meringkaskan
Di atas adalah semua konten dari artikel ini tentang implementasi Java yang menghasilkan contoh kode lengkap header pohon excel. Saya harap ini akan membantu semua orang. Teman yang tertarik dapat terus merujuk ke topik terkait lainnya di situs ini. Jika ada kekurangan, silakan tinggalkan pesan untuk menunjukkannya. Terima kasih teman atas dukungan Anda untuk situs ini!