이 기사는 주로 Excel 트리 테이블 헤더를 생성하는 Java 구현의 완전한 코드 예제를 공유합니다. 설명 할 것이 없으므로 코드 프로세스를 살펴보십시오.
소스 데이터 형식 :
String [] targetNames = { "indicator name", "unit", "xx_yy1", "xx_yy2_zz1", "xx_yy2_zz2", "2017_ 5 월 비즈니스 소득 _cumulative", "2017_may Business" 소득 -연년년 기간년 기간 기간년 기간년 기간년 기간년 기간년 기간 기간 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년년년년년 기준 전년차 연도 연도 연도 연도 연도 연도 연도 Y "2017 년 6 월 _main 비즈니스 소득 _year 년", "2017 년 6 월 2017_main 비즈니스 소득 _this month", "2017_ 2017_Main 비즈니스 소득 _moon-month", "2017 년 6 월 _profit_cumulative", "6 월 2017_profit_year 연년년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간 전년차 연도 연도 연도 연도 연도 연도 연도 전년도 연도 연도 연도 연도 Y 고기 연년년 기간 전년임 기간년 기간 기간년 기간년 기간 기간 기간년 기간년 기간년 기간 기간년 기간년 기간 기간년 기간년 기간 기간 기간 기간 기간 기간 기간년 기간 기간년 기간 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년생 연도년 기간년년년년년 기간 전년차 연도 전년도 연도 연도 연도 연도 전년차 연도 연도 연도 연도 연도 연령다음 엑셀을 생성하십시오.
첫 번째 줄은 트리 헤더에 속하지 않습니다.
암호
SplitCell :
package com.zzj.excel; public class splitcell {private string key; private String parentkey; private string value; private int columnIndex; private int columnIndex; private int rowindex; public splitcell () {} public splitCell (문자열 키, 문자열 값) {this.key = key; value = value;} public splitCell (String key, string arvalkey, String value, int columnEdex) {int rowindex) this.parentkey = 부모 키; this.value = value; 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 (}} public void value;} = value;} public int getColumnIndex () {return columnIndex;} public void setcolumnIndex (int columnIndex) {this.columnIndex = columnIndex;} public int getRowindex () {return rowIndex;} public void setrowindex (int rowindex) {this. rowindex = rowindex; [key = " + key +", parentkey = " + parentkey +", value = " + value +", columnIndex = " + columnIndex +", rowIndex = " + rowIndex +"] ";}}mergedcell :
packet com.zzj.excel; public class mergedcell {private String key; private String parentkey; private string value; private int startc; private int startc; private int endc; private int endr; private boolean leaf = true; // 기본 리프 노드 공개 문자열 getkey () {return key;} public void setkey (string key) {thiskey = key;} public strey getparent key () {replate key;} setParentKey (string parentkey) {this.parentKey = parentKey;} public String getValue () {return value;} public void setValue (string value) {this.value = value;} public int getstartc (} public void setstartc (int startc) {this.startc = startc;} public int getendc;} void setendc (int endc) {this.endc = endc;}/*** 셀 병합 엔드 칼럼 인덱스 자체 인증*/public void incendc () {this.endc ++;} public int getstartr () {return startr;} public void setstartr (int startr) {this.startr;} public int getendr () {retud getendr;} setendr (int endr) {this.endr = endr;} public boolean isleaf () {return leaf;} public void setLeaf (boolean void setLeaf (boolean leaf) {this.Leaf = Leaf;}@Public String toString () {return "CellInfo [key =" + key + ", parentkey =" + atrate + ", value =" + value + ", value +" endc = " + endc +", startr = " + startr +", endr = " + endr +", leaf = " + leaf +"] ";}}CellTransformer :
package com.zzj.excel; import java.util.linkedhashmap; import java.util.list; import java.util.map; public class celltransformer {private final list <splitcell> cellcontents; private int firstrowindex; private int rowsize; private int rowsize; private int <string, mergedcell> cellinfomap = new linkedhaphmap <string. mergedCell> (); public celltransformer (list <spliteCell> cellContents, int firstrowIndex, int rowsize) {this.cellContents = this.firstrowIndex = firstrowIndex; rowsize;} public map <string, mergedcell () {splitecell (); cellContents) {mergedCell CellInfo = cellinfomap.get (cellContent.getKey ()); if (cellInfo == null) {cellInfo = convertTocellInfo (cellContent); cellinfomap.put (cellinfo.getKey (), CellInfo); */cellinfo.incendc (); // 열 끝 인덱스 인덱스 자체 증가 (열 병합에 사용) cellinfo.setLeaf (false); // 반복적으로 나타나는 한, 그것은 비 잎 노드}}}}}}}}}} // (mergedCell CellInfo : cellInfomap.valuess (if) {if (if and it it it) {) 마지막 행 Cellinfo.setendr (firstrowindex + rowsize -1);}} return cellinfomap;} private mergedcell convertocellinfo (splitcell cellcontent) {mergedcell cellinfo = new에 병합되어야합니다. mergedCell (); cellInfo.setKey (cellContent.getKey ()); cellInfo.setParentKey (cellContent.getParentKey ()); cellInfo.setValue (cellContent.getValue ()); CellInfo.setStartc (clortContent.getColumnIndex ()); CellInfo.SetEndc (CellContent.getColumnIndex ()); CellInfo.SetStartr (CellContent.getRowIndex ()); // 시작 인덱스 기본값은 시작 인덱스 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.writ.writebellformat; jxl.write. jxl.write.writableSheet; import jxl.write.writeworkbook; public class main {private static final string exparator = "_"; public static void main (String [] args)은 예외 {string [] targetnames = "indicator name", "xx_yy1", "xx_zz2" 2017_may 비즈니스 소득 _cumulative ","2017_ 5 월 비즈니스 소득 -연년년 기간년 기간 기간 기간 기간년 기간년 기간 기간년 기간년 기간 기간년 기간 기간년 기간 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년 기간년년년년년 기간 전년차 전년도 연도 연도 연도 연도 연도 연도 Y "2017_Profit_this Month", "2017 년 5 월 2017_profit_moq", "2017_ 6 월 2017_main 비즈니스 소득 _cumulative", "2017 년 6 월 2017_main 비즈니스 소득 _year 년", "2017_ 2017_Main 비즈니스 소득 AR AR 연도 연도 연도 연도 연도 연도 연도 기한다 엑스트라 이름. 길이; i ++) {extranames [i] = "extra"+i;} Final int firsterowindex = 1; int rowsize = getRowsize (targetNames); list <splighcell> cellContents = new arrayList <> (); for (int i = 0; i <targetnames.length; i ++) {string [] [I]; int j; <value.length; SplitCell CellContent = New SplitCell (키, 부모 키, 값, I, J + FirstEROWINDEX); CellContents.Add (CellContent);}} WritableWorkbook Workbook = Workbook.CreatworkBook (새 파일 ( "f : //template.xls"); cellformat cellformat = getCellFormat (); writableSheet Sheet = workseet ( "template", 0); // 첫 번째 줄. 새 레이블 (i, 0, extranames [i], cellformat); sheet.addcell (label);} // 트리 헤더 셀 트랜스포머 CellinFomanager = new CellTransformer (CellContents, FirstEroWindex, Rowsize); map <string, mergedcell> map = cellinfomanager.transform (); for (mergedCell CellInfo : map.values ()) {label label = new label = (cellinfo.getStartc (), cellinfo.getStartr (), cellinfo.getValue (), cellformat); if (cellinfo.getStartc ()! = cellinfo.getEndc () | cellinfo.getendr ()) {sheet.mergecells (cellinfo.getstartc (), cellinfo.getstartr (), cellinfo.getendc (), cellinfo.getendr ();} sheet.addcell (label);} workbook.write (); workbook.close (); System.out.out.out.out 성공적으로! ");} private static cellformat getCellformat ()는 예외를 {ritesablefont 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 (false); return cell format; retrow int getrows getrow getrow (문자열 t : targetNames) {rowsize = math.max (rowsize, t.split (분리기) .length);} return rowsize;} private static string getkey (string [] value, int index) {StringBuffer sb = new StringBuffer (); for (index +1); i ++) {sb.append (value [i +). 분리기);} sb.deletecharat (sb.length () -1); return sb.toString ();} private static string getParentKey (string [] value, int index) {if (index == 0) {return null;} retuck getKey (값, 색인 - 1);}}요약
위는 Excel Tree 헤더의 완전한 코드 예제를 생성하는 Java 구현에 대한이 기사의 모든 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!