この記事では、主にExcelツリーテーブルヘッダーを生成するJava実装の完全なコード例を共有しています。説明するものは何もないので、コードプロセスを見てください。
ソースデータ形式:
string [] argetnames = {"indicator name"、 "ung"、 "xx_yy1"、 "xx_yy2_zz1"、 "xx_yy2_zz2"、 "xx_yy2_zz1"、 "収入_YEAR YEEAR年年年年度年年年年年年年年年年年年年年年年年年年年年間年年年間年年年間年年年間年年年間年間年年年年間年年年間年年年間年年年間年年年間年、年年年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年-YEAR-YEER-YEAR年年年年年年度年年年年年年年年年年間年年年度年年年年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年年間年年年間年年年間年、年年年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年「2017年6月_Main Business Income_Year Year "、「2017年6月_Main Business Income_This Month"、 "2017年6月_Main Business Income_Moon-Month"、「2017年6月) 2017_Profit_YEAR年年年年年年年度年年度年年年度年間年年年年年年年年年年間年年年間年年年間年年年間年年年年間年年年間年年年間年年年間年年年間年年年間年、年間年年年間年年年年間年年年間年年年間年年年間年年年間年年年間年耳年年年年年度年年年年年年年年年年年年年年年度年年年度年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年年間年、年年年年年間年年年間年年年間年年年間年年年間年年年間年年年間年次のExcelを生成します。
最初の行はツリーヘッダーに属していません。
コード
splitcell:
package 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(String key, String value) {this.key = key;this.value = value;}public SplitCell(String key, String parentKey, String value, int columnIndex, int rowIndex) {this.key = key; this.parentkey = parentkey; this.value = value; this.columnIndex = columnIndex; this.rowindex = rowindex;} public string getKey(){return key;} public void setkey(this.key = key;} public string getParentKey(){return parentkey;} public void setParentkey(string parentkey){this.parentkey = parentkey;} public getValue(){return value;} value;} public int getColumnIndex(){return columnindex;} public void setColumnIndex(int columunnindex){this.columnindex = columnindex;} public int getRowIndex(){return rowindex;} public void setRowIndex(int rowindex){recont = rowindex;} "CellContent [key =" + key + "、parentkey =" + parentkey + "、value =" + value + "、columnindex =" + columnindex + "、rowindex =" + rowindex + "];}}合併:
パッケージcom.zzj.excel; public class mergedcell {private string key; private string key; private string parentkey; private int endc; private int startr; private int endr; private boolean leaf = true; //デフォルトリーフノードパブリックストリングgetKey(){returnキー; 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 void setstartc(int startc){this.startc = startc;} startc;} startc;} setEndc(int endc){this.endc = endc;}/*** cell merge end column index selfincrement*/public void incendc(){this.endc ++;} public int getstartr(){return startr;} public void setstartr(int startr){this.startr = startr = startrtr;} public getendr;} public getendr;} 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 + " "、endc =" + endc + "、startr =" + startr + "、endr =" + endr + "、leaf =" + leaf + "]";}}CellTransformer:
パッケージcom.zzj.excel;インポートjava.util.linkedhashmap;インポート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>(); public celltransformer(list <splitcell> cellcontents、int firstrowindex、int rowsize){this.cellcontents = cellcontents; this.firstrowindex; this.rowsize = rowsize;} public map <slutedcell> transform> for() cellcontents){mergedcell cellinfo = cellininfomap.get(cellcontent.getkey()); if(cellinfo == null){cellinfo = converttocellinfo(cellcontent); cellinfomap.put(cellinfo.getkey()、cellinfo); */cellinfo.incendc(); //列の終了インデックスの自己障害(列の合併に使用)cellinfo.setLeaf(false); //繰り返し見える限り、それは非葉のノード}}}}ノード、それは最後の行にマージする必要がありますcellinfo.setendr(firstrowindex + rowsize -1);}} return cellinfomap;} private mergedcell convertocellinfo(splitecell cellcontent){mergedcell cellinfo = new mergedcell(); cellinfo.setkey(cellcontent.getKey()); cellinfo.setParentKey(cellcontent.getParentKey()); cellinfo.setValue(cellcontent.getValue()); cellinfo.setstartc(cellcontent.getColumnindex(); celliNfo.setEndc(cellcontent.getColumnIndex()); cellInfo.setStartr(cellcontent.getRowIndex()); // end indexはdefault index cellinfo.setendr(cellcontent.getrowindex(); return cellinfo;}}テスト
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.format.CellFormat;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableFont;import jxl.write.writablesheet; Import jxl.write.writableworkbook; public class main {private static final string separator = "_"; public static void main(string [] args)throws {string [] targetnames = {"indicator name"、 "unit"、 "XX_YY1"、 2017_May Business Income_Cumulative "、" 2017年5月_May Business収入_YEAR YEEAR年年年年度年年年年年年年年年年年年年年年年年年年年年間年年年間年年年間年年年間年年年間年年年年間年年年間年年年間年年年間年年年間年年年間年、年年年年年年間年年年間年年年間年年年間年年年間年年年間年-YEAR-YEAR年年年年年年度年年年年年度年年年度年年年度年年度年年年年間年年年間年年年間年年年間年年年間年年年年年年間年年年年間年年年年間年年年間年、年年年年年年年年年年間年年年間年年年間年年年間年年年年間年「2017年5月_PROFIT_TIS MONTH "、" 2017_Profit_moq "、" 2017年6月_Main Business Income_Cumulative "、" 2017年6月収入_YEAR YEEAR年年年年年年年年度年年年度年年年度年年度年年年度年間年年年年年年年年年間年年年間年年年間年年年間年年年年間年年年年間年年年間年年年間年、年年年年年間年年年間年年年間年年年間年年年間年年年間年年年間年年間年AR年年年年度年年年年年年年年年年年年度年年年度年年度年年年年年間年年年間年年年間年年年間年年年間年年年間年年年年間年年年間年年年間年年年間年、年間年年年間年年年間年年年間年年年年間年年年年間年年年間年年年間年間年年年間年年extranames.length; i ++){extranames [i] = "extra"+i;} final int firsttreerowindex = 1; int rowsize = getRowsize(ターゲットネーム);リスト<splitcell> cellcontents = new arraylist <>(); <values.length; j ++){string values [j]; string key = getKey(values、j); string parentkey = getParentKey(values、j); splitcell cellcontent = new splitcell(key、parentkey、value、i、j + firsttreerowindex); CellContents.Add(CellContent);}} WritableWorkbook workbook = workbook.createworkbook(createworkbook( "f://template.xls")); cellformat cellformat = getCellformat(); writablesheet sheet = workbook.createsheet( "template"); {label label = new label(i、0、extranames [i]、cellformat); sheet.addcell(label);} // Tree Header Celltransformer celliNfomanager = new CellTransformer(CellContents、FirstTreerowIndex、rowsize); for(mergedcell cellinfo:map.values()){label label = new label(cellinfo.getStartc()、cellinfo.getStartr()、cellinfo.getValue()、cellformat); {sheet.mergecells(cellinfo.getStartc()、cellinfo.getStartr()、cellinfo.getendc()、cellinfo.getendr();} sheet.addcell(label);} workbook.write(); workbook.close(); System.out.Outln() getCellFormat()スロー例外{writablefont font = new writablefont(writablefont.arial、10、writablefont.bold); writablecellformat cellformat = new writablecellformat(); cellformat.setfont(font); cellformat.setalignment(jxl.format.alignment.centre); cellformat.setverticalalignment(jxl.format.verticalalignment.centre); cellformat.setwrap(fals (String T:TargetNames){rowsize = math.max(rowsize、t.split(separator).length);} private static string getKey(string [] values、int index){stringbuffer sb = new Stringbuffer();セパレータ);} sb.deletecharat(sb.length() - 1); return sb.tostring();} private static string getParentKey(string [] values、int index){if(index == 0){return null;} return getKey(values、index -1);}}}}}}要約します
上記は、Excelツリーヘッダーの完全なコード例を生成するJava実装に関するこの記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!