通過使用jsoup解析html,繪畫表格產生execl檔案

來源:互聯網
上載者:User

標籤:http   document   value   繪畫   table   eal   水平對齊   正則表達   return   

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) throws RowsExceededException, 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(); ///得到樣式,以後可以根據Regex解析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檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.