标签: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(); ///得到样式,以后可以根据正则表达式解析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(); } }
标签:http document value 绘画 table eal 水平对齐 正则表达 return
原文地址:http://www.cnblogs.com/jason2333/p/6815094.html