Cet article partage principalement un exemple de code complet de la mise en œuvre de Java générant des en-têtes de table d'arborescence Excel. Il n'y a rien à expliquer, alors jetez un œil au processus de code.
Format de données source:
String [] cibleNames = {"Nom de l'indicateur", "unité", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "mai 2017_may business revenu_cumulative", "mai 2017_may Business Revenu_year-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année Y-An-Année Y-An-Année Un an -Rey-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-y-yean an-y-yo-an-y-yo yean ano-y-yeo ano-y-yo-ano-y-yo ano-ano anan anoanoanoanoanoanoanoanvano-anoan anoan anoan anoano-ano ano-y -o -o-anoan anofning anofingo anoan anoan anoanoanoanananoananandanoanoananoandanoanoananoandanowowayoo-y -o -odayo-y -o-y -odf "Juin 2017_Main Business Income_year-Year", "Juin 2017_Main Business Income_This Mois", "Juin 2017_Main Business Income_moon-Month", "juin 2017_profit_cumulative", "juin 2017_profit_year-an-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-y-yean an-y-yeo yearse-y-yearse d'année an-y-yean yearsear-y-y-yeo yearsear-y-y-yeo yearsen yearsear yearsear yearsear yearsear yearseri Ear-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-y-yean an-y-yo-Yandi-An-y -o-ano-y-yo anosfowne d'année ye d'année d'année-année-année-année-année-année-année-année-année-année-anGénérez l'Excel suivant:
La première ligne n'appartient pas à l'en-tête d'arbre.
Code
SplitCell:
package com.zzj.excel; public class Slipcell {private String Key; private String ParentKey; private String Value; private int colmnindex; private int rowIndex; public splitCell () {} public SlitCell (string key, string value) {this.key = key; this.value = valeur;} public splitcell (string key, string ParentKey, string value, int chlumnIndex, int rowIndex) {this. this.parentKey = parentKey; this.value = valeur; 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 SetParentKey (String ParentKey) {this.parentKey = ParentKey;} public String GetValue () {return value;} public SetValue (String Value) {this.value = value;} public int getColumnIndex () {return colonnedIndex;} public void SetColumnIndex (int colnnIndex) {this.columnindex = columnIndex;} public int getrowindex () {return rowindex;} public vider setRowIndex (int rowindex) {this.RowIndEx = Rowindex; toString () {return "CellContent [key =" + key + ", parentKey =" + parentKey + ", value =" + value + ", colonnedIndex =" + ColumnIndex + ", rowIndex =" + RowIndex + "]";}}MergedCell:
Package com.zzj.excel; classe publique MergedCell {Key Private String Key; Private String ParentKey; Private String Valent; private int startc; private int Endc; private int Startr; private int Endr;} private boolean leaf = true; // string nœud gnory getkey ()} public String GetPaRent Setkey (STRING key) {this.key = key;} public String GetParente GetpaRentkey setParentKey (String ParentKey) {this.parentkey = parentKey;} public String getValue () {return Value;} public void setValue (String Value) {this.value = valeur;} public int getStartc () {return startc;} public void SetStartc (int startc) {this.startc = startc; void setentenc (int endc) {this.endc = endc;} / ** * mouluer find colonne index self-increment * / public void incendc () {this.endc ++;} public int getStarttr () {return startr;} public void setStarttr (int startr) {this.startr = startr;} public int GetEndr () {return end; setendr (int endr) {this.endr = endr;} public boolean isleaf () {return leaf;} public void Setleaf (booléen leaf) {this.leaf = leaf;} @ override String public toString () {return "CellInfo [key =" + key + ", parentkey =" + Parentkey + ", value =" + value + ", startc =" endc = "+ endc +", startr = "+ startr +", endr = "+ endr +", leaf = "+ leaf +"] ";}}CellTransformateur:
package com.zzj.excel; importer java.util.linkedhashmap; import java.util.list; import java.util.map; public class CellTransformateur {private final list <splitcell> cellContents; private final int Firstrowindex; private final int rowSize; map <string, MergedCell> CellIlinfo MergedCell> (); public CellTransformateur (List <SplitCell> CellContents, int FirStrowindex, int Rowsize) {this.cellContents = CellContents; this.firStrowindex = FirStrowIndex; this.RowSize = Rowsize;} public map <string, mergell> transform () {ceLlinfoMAP.Clap.Clap.Clap.Clour CellContents) {MergedCell CelluLinfo = CelluLinfoMap.get (CellContent.getKey ()); if (CellInfo == Null) {CellInfo = ConvertTocelLinfo (CellContent); CellInfoMap.put (CeLlinfo.getKe * / CellIlinfo.incnendc (); // Colonne Index de fin INDEX Self augmentation (utilisé pour la fusion de colonnes) Cellinfo.setLeaf (false); // Tant qu'il apparaît à plusieurs reprises, il s'agit d'un nœud non-feuil Node, il doit être fusionné dans la dernière ligne Celelinfo.Settendr (FIRSTROWINDEX + ROWSIZE - 1);}} return CelelinfoMap;} private MergedCell ConvertToceLlinfo (SplitCell CellContent) {MergedCell Cellufo = new MergedCell (); CelluLinfo.SetKey (CellContent.GetKey ()); CelluLinfo.SetParentKey (CellContent.GetParentKey ()); CelluLinfo.SetStArtc (CellContent.getValue ()); // L'indice de démarrage par défaut par défaut sur l'indice de démarrage par défaut pour l'indice de démarrage pour le défaut de l'indice de démarrage pour le pour départ de l'indice de démarrage sur le fait que l'indice de démarrage sur le surin CelluLinfo.SetEtendC (CellContent.getColumnIndex ()); CelluLinfo.SetStarttr (CellContent.getRowIndex ()); // L'indice final est par défaut vers l'index de démarrage CelluLinfo.setendr (CellContent.getRowIndex ()); Return CellInfo;}}}test
Package com.zzj.excel; import java.io.file; import java.util.arraylist; import java.util.list; import java.util.map; import jxl.workbook; import jxl.write.writableChelformat; import jxl.write.writewwrite.writewwritable; JXL.WRITE.WRITABLESHEET; Importer Jxl.Write.WitableWorkBook; public class Main {private static final string séparateur = "_"; public static void main (String [] args) lève une exception {String [] TargetNames = {"Nom d'indicateur", "unité", "xx_yy1", "xx_yy2_zz1", "Xx_yy1", "May 2017_may Business Income_cumulative "," Mai 2017_may Business Revenu_year année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année d'année -Rey-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-an "Mai 2017_profit_this Mois", "May 2017_profit_moq", "Juin 2017_Main Business Income_cumulative", "Juin 2017_Main Business Income_year-Year", "Juin 2017_Main Business Revenu_year-yeardi-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-ye d'année ye d'année ye d'année years d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année ye d'année-là AR-Année année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-année-y-y-yean yerdi yearse-y-y-yeo yearso yerdi yearse-y-y-yeo yearso-y-yo-an-y-yo-Yandi-Yan-Y-y-yosifing yerdi yerdi yerdi yearso yerge-y-yement an-y-year yearsear yearsear yearsear yearsear years-y-yean yearso yerdi year-y-y-y-yeil extranames.length; i ++) {extranames [i] = "extra" + i;} final int premierTreerowIndex = 1; int RowSize = getrowSize (cibleNames); list <splitCell> CellContents = new ArrayList <> (); for (int i = 0; i <cibleNames.length; i ++) {String [] valeurs = ciblenames [i]. valeurs.length; j ++) {String value = valeurs [j]; string key = getKey (valeurs, j); string parentKey = getParentKey (valeurs, j); SplitCell CellContent = new SplitCell (Key, ParentKey, Value, I, J + FirstTreerOWIndex); CellContents.Add (CellContent);}} WitheableworkBook Workbook = Workbook.Createworkbook (nouveau fichier ("f: //template.xls")); CellFormat CellFormat = getCellFormat (); stewhesheet sheet = workbook.createSheet ("template", 0); // la première ligne pour (int i = 0; i; = new Label (i, 0, extranames [i], CellFormat); sheet.addcell (label);} // en-tête d'arbre CellTransformateur CellInfomanager = new CellTransformateur (CellContents, FirstTreerowIndex, Rowsize); Map <String, MergedCelll> Map = CellInFomanAper.Transform (); pour (MergedCell CelluLinfo: map.values ()) {label Label = new Label (CeLlinfo.getStartc (), CellInfo.getStarttr (), CelluLinfo.getValue (), CellFormat); if (CelluLinfo.getStartc ()! = CeLlinfo.gendc () || Celinfo.getStarTr ()! = CelluLinfo.geTenDr ()) {sheet.mergecells (ceLlinfo.getStartc (), CelluLinfo.getStarttr (), CelluLinfo.getendc (), CelluLinfo.getendR ());} sheet.addCell (label);} workbook.write (); workbook.close (); System.out.PrintLn ("ExportEd (); Close (); avec succès! ");} Private Static CellFormat GetCellFormat () lève une exception {WitheableFont font = new WriteableFont (WitheableFont.Arial, 10, WriteableFont.Bold); WritableCellFormat CellFormat = new WriteableCellFormat (); CellFormat.Setfont (Font); CellFormat.Setalignment (jxl.format.alignment.centre); CellFormat.SetVerterAldignment (jxl.format.verticalAlignment.Centre) (String t: TargetNames) {Rowsize = math.max (RowSize, t.split (séparateur) .length);} return RowSize;} private static getKey (String [] valeurs, int index) {StringBuffer SB = new StringBuffer (); pour (int i = 0; i <(index + 1); i ++) {sb.Apprend (valeurs [i] Séparateur);} sb.deletecharat (sb.length () - 1); return sb.toString ();} chaîne statique privée getParentKey (String [] valeurs, int index) {if (index == 0) {return null;} return getKey (valeurs, index - 1);}}Résumer
Ce qui précède est tout le contenu de cet article sur l'implémentation Java générant un exemple de code complet de l'en-tête d'arborescence Excel. J'espère que ce sera utile à tout le monde. Les amis intéressés peuvent continuer à se référer à d'autres sujets connexes sur ce site. S'il y a des lacunes, veuillez laisser un message pour le signaler. Merci vos amis pour votre soutien pour ce site!