1. Get file or character set drawing table character encoding
// get document and set encoding format public Static throws ioexception{ file MyFile=new file (fileName); Document doc= jsoup.parse (myFile, "UTF-8", "" ") ; return doc; }
2. Draw on the parsed table
public Static voidMergecolrow (Elements trs,elements Tdcol,writablesheet Sheet)throwsrowsexceededexception, writeexception{int[] rowhb=New int[trs.size ()][tdcol.size ()]; for(intI=0;i<trs.size (); i++) {Element TR=Trs.get (i); Elements TDs=tr.getelementsbytag ("td"); intRealcolnum=0; for(intJ=0;j<tds.size (); J + +) {Element TD=Tds.get (j); if(rowhb[i][realcolnum]!=0) {realcolnum=Getrealcolnum (rowhb,i,realcolnum); } intRowspan=1; intColspan=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; } } } ///this method is used to draw cells based on styles and merge cells according to Rowpan and colspan public Static voidDrawmegercell (intRowSpanintColspan,writablesheet sheet,intrealcolnum,intRealrownum,string text,int[] Rowhb)throwsrowsexceededexception, writeexception{ for(inti=0;i<rowspan;i++){ for(intj=0;j<colspan;j++){ if(i!=0| | J!=0) {text=""; } Label label=NewLabel (realcolnum+j,realrownum+i,text); Writablefont countents=NewWritablefont (writablefont.createfont ("microsoft Jas black"), 10);//Set cell contents, font sizeWritablecellformat CELLF =NewWritablecellformat (countents); Cellf.setalignment (jxl.format.Alignment.CENTRE);//Assign horizontal alignment to centerCellf.setverticalalignment (jxl.format.VerticalAlignment.CENTRE);//Specify the vertical alignment as the homeLabel.setcellformat (cellf); Sheet.addcell (label); Rowhb[realrownum+i][realcolnum+j]=1; }} sheet.mergecells (realcolnum,realrownum, realcolnum+colspan-1,realrownum+rowspan-1); }
public Static intGetrealcolnum (int[] rowhb,intIintRealcolnum) { while(rowhb[i][realcolnum]!=0) {realcolnum++; } returnrealcolnum; }
///set The column width of the table according to Colgroups public Static voidsetcolwidth (Elements colgroups,writablesheet sheet) {if(colgroups.size () >0) {Element Colgroup=colgroups.get (0); Elements cols=colgroup.getelementsbytag ("col"); for(intI=0;i<cols.size (); i++) {Element Col=Cols.get (i); String STRWD=col.attr ("width"); if(col.attr ("width")! = ""){ intWd=Integer.parseint (strwd); Sheet.setcolumnview (I,WD/8); } } } }
3. Generate Excel based on a drawing table
public voidToexcel (string fileName, string excelname, httpservletrequest Request)throwsioexception{
By parsing the character Document Doc=Jsoup.parse (fileName); Parsing based on address file //Document Doc=getdoc (fileName);String title =Doc.title (); ///get style, later can be based on regular expression parsing css, not found CssparseElements style= Doc.getelementsbytag ("style"); ///get Table,demo Just enter a table and you can enter all the table in a loop through the Tables collection laterElements tables= Doc.getelementsbytag ("TABLE"); if(tables.size () ==0){ return; } Try { //Save the file to the Classpath directoryString Path =request.getsession (). getservletcontext (). Getrealpath ("/resource/download/"); Path+ = Excelname + ". xls"; Writableworkbook book= Workbook.createworkbook (NewFile (path)); for(inti = 0; I < tables.size (); i++){
//Get TableElement table =Tables.get (i); String name= Table.attr ("value"); Writablesheet sheet=Book.createsheet (name, i); //Get all RowsElements TRS = Table.getelementsbytag ("tr"); Elements Tdcol=trs.get (0). Getelementsbytag ("td"); ///get The column width collectionElements 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 (); } }
Drawing tables generate execl files by using Jsoup parsing HTML