Este artículo comparte principalmente un ejemplo de código completo de la implementación de Java que genera encabezados de tabla de árbol de Excel. No hay nada que explicar, así que solo eche un vistazo al proceso del código.
Formato de datos de origen:
String [] TargetNames = {"Nombre del indicador", "Unidad", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "mayo 2017_may Business ingreso_cumulativo", "mayo 2017_may empresarial Ingreso_Year año año año año año año año año año año año año año año año año año año año año año año año año año año año año año-año-año año año año año año año año año año año año año año año año-año año año-año año año año año año año año año año año año año año año año año año año año año año año año- año de año -MO año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año de año año año año año año-año año año año año año año año año año año año año año año año año año año año año el año año año año año año año año año año año año año año-año-y año y año y año "Junio 2017_Main Business Income_Year-Year", "Junio 2017_Main Business Income_This Mes", "Junio 2017_Main Business Income_moon-Month", "Junio 2017_Profit_Cumulative", "Junio 2017 2017 2015 año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año-año año año-año año año año año año año año año año año año año año año año el año año año año año año año año año año año año-año año año año año Y año Y año Y Aye año año año año año año año año año año año año año año año año año año año año año año año año de año año año año año-año año año año año año año año año año año año año año año año año año año año año año año año de año año año año año año año año año año año año año año año año año año año año año año año año año año de añoGenere el siguiente Excel:
La primera línea no pertenece al encabezado del árbol.
Código
SplitCell:
paquete com.zzj.excel; public class SplitCell {private String Key; private String ParentKey; private String Value; private int columnIndex; private int rowIndex; public splitcell () {} public SplitCell (Key de cadena, Key String Value) {this.Key = Key Key; this.value = value;} public PublicCell (Key de cadena, string ParentKey, string Value, int columna, int columnax) this.ParentKey = ParentKey; this.Value = value; this.ColumnIndex = columnIndex; this.rowindex = rowIndex;} public String getKey () {return key;} public void setKey (clave de cadena) {this.key = key;} public String getParentKey () {return ParentKey;} public void setParentKey (string string string) {this.parentkey = parentkey;} public string getValue () {return value;} public void setvalue (string value) {this.value = value;} public int getColumnIndex () {return columnIndex;} public void setColumnIndex (int columnIndex) {this.columnIndex = columnIndex;} public int getRowindex () {return RowIndex;} public Void SetRowindex (int toString () {return "cellContent [key =" + key + ", parentKey =" + parentkey + ", value =" + value + ", columnIndex =" + columnIndex + ", rowIndex =" + rowIndex + "]";}}Cell Fused:
paquete com.zzj.excel; public class MergedCell {private String Key; private String ParentKey; private String Value; private int inicio; private int endc; private int startr; private int endr; private boolean hoja = true; // nodo de hoja predeterminado public string getKey () {return key;} public void setKey (clave de cadena) setParentKey (String ParentKey) {this.ParentKey = ParentKey;} public String getValue () {return value;} public void setValue (string value) {this.value = valor;} public int getStartC () {return startC;} public void setStartC (int inicio) {this.startc = startc;} int getendc ()} void setendc (int endc) {this.endc = endc;}/*** Cell Merge End Column Index Self-Increment*/public void Incendc () {this.endc ++;} public int getStartr () {return startr;} public void setStartr (intentt int) 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 +", hoja = " + hoja +"] ";}}CellTransformer:
paquete com.zzj.excel; import java.util.linkedhashmap; import java.util.list; import java.util.map; public class CellTransformer {Private Final List <Spl SplitCell> CellContents; Private Final int FirStrowindex; Private intent Rowsize; Private Map <String, Celina> CellInfomap = New LinkEdHashmap, StringShmap; MergedCell> (); public cellTransformer (List <Splircell> CellContents, int firstrowindex, int rowsize) {this.cellContents = cellContents; this.firstrowindex = firStrowIndex; this.rowsize = showsize;} public CellContents) {mergedcell cellInfo = cellInfomap.get (cellContent.getKey ()); if (cellInfo == null) {cellInfo = convertTocellInfo (cellContent); cellInfomap.put (cellInfo.getKey (), cellInfo);} más */cellInfo.incendc (); // columna end Índice autoincriete (utilizado para fusión de columna) cellinfo.setleaf (false); // Siempre que aparezca repetidamente, es un nodo no hojado}} // fila para (fusedCell CellInfo: CellInFomap.Values () {if (CellInfo.islofic ())) Debe fusionarse con la última fila CellInfo.SetEdenR (FirStrowIndex + Rowsize - 1);}} return CellInfomap;} private convertocellinfo (SplitCell CellContent) {mergedCell CellInfo = nuevo MergedCell();cellInfo.setKey(cellContent.getKey());cellInfo.setParentKey(cellContent.getParentKey());cellInfo.setValue(cellContent.getValue());cellInfo.setStartC(cellContent.getColumnIndex());// The end index defaults to the start index CellInfo.SetEndc (CellContent.GetColumnIndex ()); CellInfo.SetStARTR (CellContent.GetRowIndex ()); // El índice final predeterminado al índice de inicio CellInfo.setendr (CellContent.getRowindex ()); regresa CellInfo;}}prueba
paquete 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.write.label; import jxl.writ.writeBlas jxl.write.writableSheet; import jxl.Write.WritableWorkbook; public class Main {private static string string separator = "_"; public static void main (string [] args) lanza la excepción {String [] targetNames = {"Nombre del indicador", "Unidad", "XX_YY1", "XX_YY2_ZZ1", "XX_YY2", "May", "Mayy", "Mayy", "Mayy," Mayy, "Mayy," Mayy "," Mayyy, "Mayy," Mayyy "," Mayy, "Mayy2". 2017_MAY Business Income_Cumulative "," May 2017_May Business Ingreso_Year año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año-año-año año año año año año año año año año año año año año año año-año año año año año-año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año- año -I A años año año año año año año año año año año año año año año año año año año año año año año de año año año año año año-año año año año año año año año año año año año año año año año año año año de año año año año año año año año año año año año año año año año "Mayo 2017_profit_this Month", "mayo 2017_profit_moq", "Junio 2017_Main Business Income_Cumulative", "Junio 2017_Main Business Income_Year-Year", "Junio de 2017_Main Business Ingreso_year año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año año- año AR año año año año año año año año año año año año año año año año año año año año año de año año año año año año año año año año año año-año año año año año año año año año-año año año año año año año año año-año año año año año año año año año año año año año año año año año año-año-y-y-y-Y Extranames.length; i ++) {Extranames [i] = "extra"+i;} final int firstTreeRowindex = 1; int rowsize = getRowsize (targetNames); list <ScillCell> CellContents = new ArrayList <> (); for (int i = 0; i <targetNams.length; i ++) {string [] Valores = valores de TargetNames [i].]. <valores.length; SplitCell CellContent = new SplitCell (Key, ParentKey, Value, I, J + FirstTreeRowIndex); CellContents.Add (CellContent);}} Writingworkbook Workbook = Workbook.CreateWorkBook (nuevo archivo ("f: //template.xls")); celular cellformat = getCellFormat (); writableSheet sheet = workbook.createSheetet ("plantplate", 0);//primera línea para (int i = 0; i <i <tronames.l longitud. {Etiqueta etiqueta = nueva etiqueta (i, 0, extraanames [i], cellformat); shead.addcell (etiqueta);} // encabezado de árbol celltransformer cellInfomanager = newTransformer (CellContents, FirstTreeRowIndex, Rowsize); MAP <String, MergedCell> MAP = CellInfomanager.transform (); para (MergedCell CellInfo: MAP.Values ()) {Label Label = New Label (CellInfo.getStartC (), CellInfo.getStartr (), CellInfo.getValue (), CellFormat); if (CellInfo.getStartC ()! = CellInfo.getendc () || CellInfo.getStartr () =) CellInfo.getEdenRR ()) {sheet.mergeCells (celleinfo.getStartC (), celleinfo.getstartr (), cellInfo.getEndC (), celleinfo.getInDr ());} sheet.addcell con éxito! ");} private static conformat getCellFormat () lanza la excepción {WritableFont font = new WritableFont (WRITITYFONT.ARIAL, 10, WATITYFONT.BOLD); WRITABLECELLFORMAT CELLFORMAT = NUEVO WritableCellFormat (); CellFormat.SetFont (font); CellFormat.SetAlignment (jxl.format.alignment.centre); cellformat.setvericalAlignment (jxl.format.vericalAlignment.centre); cellformat.setwrap (falso); return CellFormat;} int getrowsize (string [] TargetNames) (String t: TargetNames) {ROWSIZE = Math.max (Rowsize, T.Split (Separator) .Length);} return Rowsize;} private static string getKey (string [] valores, int index) {StringBuffer sb = new StringBuffer (); for (int i = 0; i <(INDEX +1); I ++) {Sb.append (valores (iValos) Separador);} sb.DeletElticeCharat (sb.length () - 1); return sb.ToString ();} cadena estática privada getParentKey (string [] valores, int index) {if (index == 0) {return null;} return getKey (valores, índice - 1);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}Resumir
Lo anterior es todo el contenido de este artículo sobre la implementación de Java que genera un ejemplo de código completo de encabezado de Excel Tree. Espero que sea útil para todos. Los amigos interesados pueden continuar referiéndose a otros temas relacionados en este sitio. Si hay alguna deficiencia, deje un mensaje para señalarlo. ¡Gracias amigos por su apoyo para este sitio!