Este artigo compartilha principalmente um exemplo de código completo da implementação de Java, gerando cabeçalhos de mesa do Excel Tree. Não há nada a explicar, então dê uma olhada no processo de código.
Formato de dados de origem:
String [] TargetNames = {"Nome do indicador", "unidade", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "maio de 2017_may Business Renda_cumulative", "May 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-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Year-Y "Junho de 2017_Main Business Renda_ear-ano", "Junho de 2017_Main Business Renda_ deste mês", "Junho de 2017_Main Business Renda_moon-Mont", "junho de 2017_profit_cumulative", "junho 2017_profit_ ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano passado, ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-ano passado-anoWUNY, ano-ano-ano-ano-ano-ano-ano-ano-ano-ano-anotro 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-YearGerar o seguinte Excel:
A primeira linha não pertence ao cabeçalho da árvore.
Código
SplitCell:
pacote com.zzjj.excel; public class splitcell {private string key; private string parentkey; private string value; private int colunindex; private int rowindex; public splitCell () {} public splitCell (chave de string, string) {this Key = key; this.parentKey = parentkey; this.value = value; this.columnindex = colunindex; this.RowIndex = rowIndex;} public String getKey () {return key;} public void setKey (string key) {this.key = key;} public string getParentKey () {return parentKey;} public string getParentKey (string parentkey) {this.ParentKey = publicKey; value;} public int getColumnIndex () {return columnIndex;} public void setColumnIndex (int columnIndex) {this.columnindex = columnindex;} public int getRowIndex () {return rowindex;} public void setRowindex (intIndex) {) {this.RowNowNowNex; "CellContent [key =" + key + ", parentkey =" + parentkey + ", value =" + value + ", colunindex =" + colunindex + ", rowindex =" + rowindex + "]";}}MergedCell:
pacote com.zzjj.excel; public class MergedCell {private string key; private string parentkey; valor de sequência privada; private int startc; private int endc; private int startr; private int endr; private boolean folha = true; // key padrão)} setParentKey (String parentKey) {this.parentKey = parentKey;} public String getValue () {return value;} public void SetValue (String value) {this.value = value;} public int getStartC () {return startc;} public setstartc (int startc) {this.startC =; setendc (int endc) {this.endc = endc;}/*** Mesclina de célula End Column Index auto-incremento*/public void incendc () {this.endc ++;} public int getStarTr () {return startr;} public void setSarTr (int starTr) {this.tarTr =; 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 +", startr = " + startr +", endr = " + endr +", leaf = " + folha +"] ";}}CellTransformer:
pacote com.zzjj.excel; importar java.util.linkedhashmap; importar java.util.list; importar java.util.map; classe pública CLELTRANSFORMER {Private Final List <SplitCell> célula de células; MergedCell> (); public CellTransFormer (List <SplitCell> CellContents, int FirstrowIndex, Int Rowsize) {this.CellContents = CellContents; this.firstrowIndex = FirstrowIndex; this.rowsize = lobsize;} mapa public <String, MergedCell> transformam () {this.Rowsize = Owsize;} public Map (String, Merged> transform> () {TowlinInin; CellContents) {MergedCell Cellinfo = CellInfomap.get (CellContent.getKey ()); if (Cellinfo == null) {Cellinfo = converttocellinfo (CellinContent); mais; */Cellinfo.inchendc (); // Índice de extremidade da coluna Auto-incrustação (usada para mesclagem de coluna) Cellinfo.setLeaf (false); //, desde que apareça repetidamente, é um nó não-folhedoso}} // fusão para (MergedCell Cellinfo: CellinFomap.Values () {Ift (se MergedCell Cellinfo: CellinFomap.Values () () (// ROW (IftCell Cellinfo: CellinFomap.Values () {se Nó, ele deve ser mesclado para a última linha Cellinfo.setEndr (FirstrowIndex + Rowsize - 1);}} Retornar Cellinfomap;} private MergedCell converttocellinfo (SplitCell CellContent) {MergedCell Cellinfo = novo MergedCell (); Cellinfo.SetKey (CellContent.getKey ()); Cellinfo.setParentKey (CellContent.getParentKey ()); Cellinfo.setValue (CellContent.getValue ()); Cellinfo.setEndc (CellContent.getColumnIndex ()); Cellinfo.setStartr (CellContent.GetRowIndex ()); // O índice final padrão do índice de início Cellinfo.setEndr (CellContent.GeROWIndex ()); retorno Cellinfo;}}}}teste
pacote com.zzjj.excel; importar java.io.file; importar java.util.ArrayList; importar java.util.list; importar java.util.map; importar jxl.workbook; import.nxl.format.cellformat; import jxl.write.label; importFl.ite.itLl.IrtFll.wrlBl.wrlBl.wrlBl.wrlBl.wrlBl.wrlBl.wrlBl.wrll.wrlBl; jxl.write.writableSleshet; importar jxl.write.writableworkbook; classe pública main {private static final string separator = "_"; public static void main (string [] args) lança exceção {string [] TargetNames = {"Nome do indicador", "unidade", "xx_yy1", "" xx_x_x_yy2, ",", "," string ", e", o nome do indicador, ", o nome do indicador,", o nome do indicador ", e"; 2017_may Business rendy_cumulative "," Maio de 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 -Ear ano-ano, ano-ano, ano-ano, ano-ano, ano-ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano-ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano, ano o ano "Maio de 2017_profit_this Mês", "Maio de 2017_profit_moq", "junho de 2017_main Business Renda_cumulative", "junho de 2017_main Business Renda_year-ano", "Junho de 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 ++) {extraRanames [i] = "extra"+i;} final int FirstTreeRowIndex = 1; int rowsize = getRowsize (TargetNames); list <SplitCell> CellContents = new ArrayList <> (); para (int i = 0; i <getNames.Length. j <valores.Length; SplitCell CellContent = new SplitCell (Key, Parentkey, Value, i, J + FirstTreeRowIndex); CellContents.Add (CellContent);}} WritableWorkbook Workbook = Workbook.CreateWorkBook (novo arquivo ("f: //template.xls")); CellFormat CellFormat = getCellFormat (); Writablheet Sheet = Workbook.Createsheet ("modelo", 0); etiqueta = novo rótulo (i, 0, extranames [i], célula); sheet.addcell (etiqueta);} // cabeçalho de árvore CellTransformer Cellinfomanager = new CellTransformer (Contenciadores CellinFomanGereReReReReWIndex, Rowsize); Map <StringCell> map = Cellinfomanager.TransformEx (); para (MergedCell Cellinfo: map.values ()) {etiqueta de etiqueta = new Label (Cellinfo.getStartc (), Cellinfo.getStartr (), Cellinfo.getValue (), CellFormat; if (Cellinfo.getStartC ()! {Sheet.MergeCells (Cellinfo.getStartc (), Cellinfo.getStartr (), Cellinfo.getEndc (), Cellinfo.getEndr ());} Sheet.addcell (LABEL);} WHELABY.WRITE (); Workbook.Close (); System.out.PRIRSLN (); getCellFormat () lança Exceção {writablefont font = new writablefont (writablefont.arial, 10, writablefont.bold); writableCellformat cellformat = novo WritableCellFormat (); CellFormat.SetFont (Font); CellFormat.Setalignment (jxl.format.alignment.centre); CellFormat.SetverticalAlignment (jxl.Format.verticalalignment.centre); (String t: TargetNames) {ROWSIZE = MATH.MAX (ROWSIZE, T.SPLIT (separador) .Length);} Retorno rowsize;} String estática privada GetKey (String [] Valores, int index) {StringBuffer sb = new StringBuffer (S for I = 0; i <(Index +1); Separador);} sb.deleteCharat (sb.length () - 1); retorna sb.toString ();} string estática privada getParentKey (string [] valores, int index) {if (index == 0) {return null;} retornar getKey (valores, index - 1);}}Resumir
O exposto acima é todo o conteúdo deste artigo sobre a implementação do Java, gerando um exemplo de código completo do cabeçalho do Excel Tree. Espero que seja útil para todos. Amigos interessados podem continuar se referindo a outros tópicos relacionados neste site. Se houver alguma falha, deixe uma mensagem para apontá -la. Obrigado amigos pelo seu apoio para este site!