Dieser Artikel hat hauptsächlich ein vollständiges Code -Beispiel für die Java -Implementierung, die Excel -Baum -Tabellenheader generiert. Es gibt nichts zu erklären, also schauen Sie sich einfach den Codeprozess an.
Quelldatenformat:
String [] targetnames = {"Anzeigename", "Einheit", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "Mai 2017_MAY Business Income_Cumulative", "May 2017_MAY Business Ertragsjahresjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr -Jahrjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr vor dem Vorjahres-Anmessen des Jahres Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahresjahr Vorjahresjahr Vorjahr Vorjahr "Juni 2017_Main Business Income_year-Jahr", "Juni 2017_Main Business Income_this Month", "Juni 2017_Main Business Income_moon Montag", "Juni 2017_Profit_cumulative", "June", "June" 2017_Profit_year-Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahresjahr Vorjahresjahres-20-Vorjahr Vorjahres-20-Wert Vorjahresjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahresjahr Vorjahresjahr Vorjahreszeit Hörjahresjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr VorjahrGenerieren Sie den folgenden Excel:
Die erste Zeile gehört nicht zum Baumkopf.
Code
Splitcell:
Paket com.zzj.excel; public class splitcell {private string key; private String parentKey; private String -Wert; private int columnIndex; private int rowIndex; public splitcell () {} public splitcell (String -Schlüssel, String -Wert) {this.key = key; this.ParentKey = parentKey; this.Value = Wert; this.columnIndex = columnIndex; this.rowIndex = rowIndex;} public String getKey () {return key;} public void setKey (String Key) {this.key = key;} public String getParentKey () {return parentKey;} public void void setParent (String Parentkey) {this.ParentKey; {this.Value = value;} public int getColumnIndex () {return columnIndex;} public void setColumnIndex (int columnIndex) {this.columnIndex = columnIndex;} public int get getrowIndex {) {return rowIndex;} öffentlicher void -void -void -set -@int [email protected]) {this.dowowdex) {this. this.rowinIndex) {this.the.trowIndex) {this.the.rowindex = rowIndex) {this.drow. {this.row. {this.row.drowIndex) {this. toString () {return "cellContent [key =" + taste + ", parentKey =" + parentKey + ", value =" + value + ", columnIndex =" + columnIndex + ", rowIndex =" + rowIndex + "]";}}Mergedcell:
package com.zzj.excel;public class MergedCell {private String key;private String parentKey;private String value;private int startC;private int endC;private int startR;private int endR;private Boolean leaf = true;// Default leaf node public String getKey() {return key;}public void setKey(String key) {this.key = key;}public String getParentKey() {return parentKey;}public void setParentKey (String parentKey) {this.parentKey = parentKey;} public String getValue () {return value;} public void setValue (String Value) {this.Value = value;} public int startc () {return startC;} public void setStartc (intartc) {this.startc = startC; void setendc (int endc) {this.endc = endc;}/*** Zelle Merge End Column Index Selbstversorgung 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 + ", value=" + value + ", startC=" + startC + ", endc = " + endc +", starter = " + starter +", endr = " + endr +", leaf = " + leaf +"] ";}}Celltransformator:
Paket com.zzj.excel; import Java.util.linkedHasMap; Import Java.util.List; import Java.util.map; öffentliche Klasse celltransformer {private endgültige Liste <splitcell> cellcontents; MergedCell> (); public celltransformer (list <Strichcell> cellContents, int firstrowIndex, int rowSize) {this.cellContents = cellContents; cellContents) {mergedCell cellInfo = cellInfomap.get (cellContent.getKey ()); if (cellinfo == null) {cellinfo = convertTocellInfo (cellContent); */cellInfo.incEndC();// Column end index self-increase (used for column merging) cellInfo.setLeaf(false);// As long as it appears repeatedly, it is a non-leaf node}}// Row merge for (MergedCell cellInfo: cellInfoMap.values()) {if (cellInfo.isLeaf()) {// If it is a leaf node, it Muss mit dem letzten Zeilen von CellInfo.setendr (firstrowIndex + rowSize - 1);}} zurückgeführt werden MergedCell (); cellInfo.setKey (cellContent.getKey ()); cellinfo.setParentKey (cellContent.getParentKey (); cellInfo.setendc (cellContent.getColumnIndex ());prüfen
Paket com.zzj.excel; import java.io.file; import Java.util.ArrayList; import Java.util.List; import Java.util.map; import Jxl.workbook; import Jxl.format.cellFormat; import Jxl.wrabel; jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class Main {private static final String SEPARATOR = "_";public static void main(String[] args) throws Exception {String[] targetNames = { "Indicator Name", "Unit", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "May 2017_may Business Income_Cumulative "," Mai 2017_MAY Business Einkommensjahresjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr -Jahrjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahresjahr des Jahres Vorjahresjahr-Jährige Vorjahr "Mai 2017_Profit_This Monat", "Mai 2017_Profit_moq", "Juni 2017_main Business Income_Cumulative", "Juni 2017_Main Business Income_year-Jahr", Juni 2017_main Business Einkommensjahresjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahresjahr Vorjahres-Jee-Jee-Jee-Jee-Jee-Jee-Jee-JED AR-Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr Vorjahr im Vorjahr extranames.length; i ++) {extranames [i] = "extra"+i;} endgültig intreerowIndex = 1; int rowSize = getRowSize (targetNames); list <Slastcell> cellContents = new ArrayList <> (); für (int i = 0; i <targetnames.length; i ++) {] -Ston [] -Ston [] -Vote []. Werte.Length; Splitcell cellContent = new Splitcell (Schlüssel, Parentkey, Value, I, J + FirstreeRowIndex); CellContents.Add (CellContent);}} WORTABLEWORKBOOK WORKBOOK = WORKBOOK.CreateWorkbook (neue Datei ("f: //template.xls"); = neues Label (i, 0, extranames [i], cellFormat); Sheet.Addcell (Label);} // Baumhader als Celltransformer cellinfomanager = new CellTransformer (CellContents, FirstReerowIndex, RowSize); MAP <string, mergedCell> MAP = cellinfanfanager.transformager (); für (mergedCell cellInfo: map.Values ()) {Label label = new label (cellinfo.getStartc (), cellinfo.getStartr (), cellinfo.getValue (), cellformat); cellInfo.getendr ()) {Sheet.MergeCells (cellinfo.getStartc (), cellinfo.getStartr (), cellinfo.getendc (), cellinfo.getendr ()); erfolgreich! ");} private static cellFormat getCellFormat () löst Ausnahme aus {WritableFont font = new WritableFont (withableFont.arial, 10, schriftlich WritableCellFormat (); cellFormat.setfont (font); cellFormat.SetAlignment (jxl.format.Alignment.centre); . Separator);} sb.deletecharat (sb.length () - 1); return sb.toString ();} private statische String getParentKey (String [] Werte, intindex) {if (index == 0) {return null;} return getKey (Werte, Index - 1);}}}Zusammenfassen
Das obige ist der gesamte Inhalt dieses Artikels über die Java -Implementierung, die ein vollständiges Code -Beispiel für Excel -Baumkopf generiert. Ich hoffe, es wird für alle hilfreich sein. Interessierte Freunde können weiterhin auf andere verwandte Themen auf dieser Website verweisen. Wenn es Mängel gibt, hinterlassen Sie bitte eine Nachricht, um darauf hinzuweisen. Vielen Dank an Freunde für Ihre Unterstützung für diese Seite!