通過使用jsoup解析html,繪畫表格生成execl文件
阿新 • • 發佈:2017-05-06
num group wid 字符 for format 格式 colspan tables
1.獲取文件或者字符設置繪畫表格字符編碼
//得到Document並且設置編碼格式 public static Document getDoc(String fileName) throws IOException{ File myFile=new File(fileName); Document doc= Jsoup.parse(myFile, "UTF-8",""); return doc; }
2.根據解析出來的table進行繪畫
public static void mergeColRow(Elements trs,Elements tdcol,WritableSheet sheet) throwsRowsExceededException, WriteException{ int[][] rowhb=new int[trs.size()][tdcol.size()]; for(int i=0;i<trs.size();i++){ Element tr=trs.get(i); Elements tds=tr.getElementsByTag("td"); int realColNum=0; for(int j=0;j<tds.size();j++){ Element td=tds.get(j); if(rowhb[i][realColNum]!=0){ realColNum=getRealColNum(rowhb,i,realColNum); } 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(rowspan,colspan,sheet,realColNum,i,text,rowhb); realColNum=realColNum+colspan; } } } ///這個方法用於根據樣式畫出單元格,並且根據rowpan和colspan合並單元格 public static void drawMegerCell(int rowspan,int colspan,WritableSheet sheet,int realColNum,int realRowNum,String text,int[][] rowhb) throws RowsExceededException, WriteException{ for(int i=0;i<rowspan;i++){ for(int j=0;j<colspan;j++){ if(i!=0||j!=0){ text=""; } Label label = new Label(realColNum+j,realRowNum+i,text); WritableFont countents = new WritableFont(WritableFont.createFont("微軟雅黑"),10); // 設置單元格內容,字號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(realColNum,realRowNum, realColNum+colspan-1,realRowNum+rowspan-1); }
public static int getRealColNum(int[][] rowhb,int i,int realColNum){ while(rowhb[i][realColNum]!=0){ realColNum++; } return realColNum; }
///根據colgroups設置表格的列寬 public static void setColWidth(Elements colgroups,WritableSheet sheet){ if(colgroups.size()>0){ Element colgroup=colgroups.get(0); Elements cols=colgroup.getElementsByTag("col"); for(int i=0;i<cols.size();i++){ Element col=cols.get(i); String strwd=col.attr("width"); if(col.attr("width")!=""){ int wd=Integer.parseInt(strwd); sheet.setColumnView(i,wd/8); } } } }
3.生成根據繪畫的表格生成Excel
public void toExcel(String fileName, String excelName, HttpServletRequest request)throws IOException{
//通過解析字符 Document doc = Jsoup.parse(fileName); //根據地址文件進行解析 // Document doc=getDoc(fileName); String title = doc.title(); ///得到樣式,以後可以根據正則表達式解析css,暫且沒有找到cssparse Elements style= doc.getElementsByTag("style"); ///得到Table,demo只演示輸入一個table,以後可以用循環遍歷tables集合輸入所有table Elements tables= doc.getElementsByTag("TABLE"); if(tables.size()==0){ return; } try { //文件保存到classpath目錄下面 String path = request.getSession().getServletContext() .getRealPath("/resource/download/"); path += excelName + ".xls"; WritableWorkbook book = Workbook.createWorkbook(new File(path)); for(int i = 0; i < tables.size();i++){
//獲取table Element table = tables.get(i); String name = table.attr("value"); WritableSheet sheet = book.createSheet( name, i); //得到所有行 Elements trs = table.getElementsByTag("tr"); Elements tdcol=trs.get(0).getElementsByTag("td"); ///得到列寬集合 Elements colgroups = table.getElementsByTag("colgroup"); setColWidth(colgroups, sheet); mergeColRow(trs, tdcol, sheet); } book.write(); book.close(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } }
通過使用jsoup解析html,繪畫表格生成execl文件