-
大小: 910KB文件類型: .zip金幣: 2下載: 0 次發布日期: 2021-05-21
- 語言: Java
- 標簽: excelJsoup??
資源簡介
要求能夠實現給出任意帶table表格的html文件,生成與表格相同內容的excel文件,附件可以作為測試文件,提供給定的roster.html文件,通過java代碼,實現生成與html頁面的table相同樣式的roster.xls文件。

代碼片段和文件信息
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.io.IOException;
import?jxl.Workbook;
import?jxl.write.Label;
import?jxl.write.WritableCellFormat;
import?jxl.write.WritableFont;
import?jxl.write.WritableSheet;
import?jxl.write.WritableWorkbook;
import?jxl.write.WriteException;
import?jxl.write.biff.RowsExceededException;
import?org.jsoup.Jsoup;
import?org.jsoup.nodes.Document;
import?org.jsoup.nodes.Element;
import?org.jsoup.select.Elements;
///COPY?BY??安卓無憂
public?class?HTMLTOExcel?{
public?static?void?main(String?args[])?throws?IOException{
///讀取classpath目錄下面的路徑
String?path=HTMLTOExcel.class.getResource(“/“).getPath();
path+=“roster.html“;
toExcel(path“roster“); ??? ???
????}
????//得到Document并且設置編碼格式
public?static?Document?getDoc(String?fileName)?throws?IOException{
??????File?myFile=new?File(fileName);
??????Document?doc=?Jsoup.parse(myFile?“GBK“““);
??????return?doc;
}
///這個方法用于根據trs行數和sheet畫出整個表格
public?static?void?mergeColRow(Elements?trsWritableSheet?sheet)?throws?RowsExceededException?WriteException{
int[][]?rowhb=new?int[300][50];
for(int?i=0;i ?Element?tr=trs.get(i);
?Elements?tds=tr.getElementsByTag(“td“);
?
?int?realColNum=0;
?for(int?j=0;j ??Element?td=tds.get(j); ??
??if(rowhb[i][realColNum]!=0){
??realColNum=getRealColNum(rowhbirealColNum);
??}
??int?rowspan=1;
??int?colspan=1;
??if(td.attr(“rowspan“)!=““){
??rowspan?=?Integer.parseInt(td.attr(“rowspan“));
??}
??if(td.attr(“colspan“)!=““){
??colspan?=?Integer.parseInt(td.attr(“colspan“));
??}
??String?text=td.text();
??drawMegerCell(rowspancolspansheetrealColNumitextrowhb);
??realColNum=realColNum+colspan;
?}
?
}
}
///這個方法用于根據樣式畫出單元格,并且根據rowpan和colspan合并單元格
public?static?void?drawMegerCell(int?rowspanint?colspanWritableSheet?sheetint?realColNumint?realRowNumString?textint[][]?rowhb)?throws?RowsExceededException?WriteException{
??for(int?i=0;i ??for(int?j=0;j ??if(i!=0||j!=0){
?text=““;
??}
??Label?label?=?new?Label(realColNum+jrealRowNum+itext);
???WritableFont?countents?=?new?WritableFont(WritableFont.TIMES10);?//?設置單元格內容,字號12??
???WritableCellFormat?cellf?=?new?WritableCellFormat(countents?);?
???cellf.setAlignment(jxl.format.Alignment.CENTRE);//把水平對齊方式指定為居中
???cellf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//把垂直對齊方式指定為居
???label.setCellFormat(cellf);
???sheet.addCell(label);
???rowhb[realRowNum+i][realColNum+j]=1;
??}
??}
??sheet.mergeCells(realColNumrealRowNum?realColNum+colspan-1realRowNum+rowspan-1);
}
public?static?int?getRealColNum(int[][]?rowhbint?iint?realColNum){
while(rowhb[i][realColNum]!=0){
realColNum++;
}
return?realColNum;
}
///根據colgroup
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-09-23?12:14??HtmlToExcel\
?????文件?????????409??2014-11-12?10:57??HtmlToExcel\.classpath
?????文件?????????387??2014-11-12?10:51??HtmlToExcel\.project
?????目錄???????????0??2016-09-23?12:14??HtmlToExcel\.settings\
?????文件?????????629??2014-11-12?10:51??HtmlToExcel\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2016-09-23?12:14??HtmlToExcel\bin\
?????文件????????5743??2016-09-23?12:12??HtmlToExcel\bin\HTMLTOExcel.class
?????文件???????17644??2016-09-23?11:53??HtmlToExcel\bin\roster.html
?????文件???????14848??2016-09-23?12:06??HtmlToExcel\bin\roster.xls
?????目錄???????????0??2016-09-23?12:14??HtmlToExcel\lib\
?????文件??????300844??2014-11-12?10:57??HtmlToExcel\lib\jsoup-1.8.1.jar
?????文件??????704543??2014-11-12?10:54??HtmlToExcel\lib\jxl.jar
?????目錄???????????0??2016-09-23?12:14??HtmlToExcel\src\
?????文件????????4874??2016-09-23?12:12??HtmlToExcel\src\HTMLTOExcel.java
?????文件???????17644??2016-09-23?11:53??HtmlToExcel\src\roster.html
?????文件?????????177??2016-09-23?12:12??HtmlToExcel\請先閱讀我.txt
評論
共有 條評論