package com.chinahrt.report.export; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Workbook; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; /**根据table的html代码生成excel * @param args * zyn * 2012-12-19 上午11:35:30 */ public class TableToExcelUtil { /** * * @param sheetName * @param html * @param headNum表头的行数 * @throws FileNotFoundException * zyn * 2012-12-21 下午1:44:02 */ public void createExcelFormTable(String sheetName,String html,int headNum) throws FileNotFoundException{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(sheetName); CellStyle headStyle = this.createHeadStyle(wb); CellStyle bodyStyle = this.createBodyStyle(wb); FileOutputStream os = new FileOutputStream("c:\\table.xls"); SAXBuilder sb = new SAXBuilder(); ByteArrayInputStream is = new ByteArrayInputStream(html.getBytes()); try { org.jdom.Document document = sb.build(is); //获取table节点 Element root = document.getRootElement(); //获取tr的list List<Element> trList = root.getChildren("tr"); int[][] area = getCellArea(trList); //循环创建行 for(int i=0;i<trList.size();i++){ HSSFRow row = sheet.createRow(i); List<Element> tdList = trList.get(i).getChildren("td"); //该行td的序号 int tdIndex = 0; for(int ii=0;ii<area[i].length;ii++){ row.createCell(ii); HSSFCell cell = row.getCell(ii); //判断是否为表头,使用对应的excel格式 if(i<headNum){ cell.setCellStyle(headStyle); }else{ cell.setCellStyle(bodyStyle); } //如果对应的矩阵数字为1,则和横向前一单元格合并 if(area[i][ii]==1){ sheet.addMergedRegion(new CellRangeAddress(i,i,ii-1,ii)); }else if(area[i][ii]==2){//如果对应的矩阵数字为2,则和纵向的前一单元格合并 sheet.addMergedRegion(new CellRangeAddress(i-1,i,ii,ii)); }else{//如果为0,显示td中对应的文字,td序号加1 cell.setCellValue(this.getInnerText(tdList.get(tdIndex))); tdIndex ++; } } } wb.write(os); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 导出excel表格二维数组:0为文字占用格,1为横向被合并格,2为纵向合并格 * @param trList * @return * zyn * 2012-12-21 下午1:35:40 */ private int[][] getCellArea(List<Element> trList){ //获取table单元格矩阵 Element headtr = trList.get(0); List<Element> headTdList = headtr.getChildren("td"); //每行的未经合并的单元格个数 int cols = 0; for(Element e:headTdList){ int colspan = Integer.valueOf(null==e.getAttributeValue("colspan")?"0":e.getAttributeValue("colspan")); if(colspan==0){ colspan =1; } cols += colspan; } //初始化单元格矩阵 int[][] area = new int[trList.size()][cols]; for(int i=0;i<trList.size();i++){ Element tr = trList.get(i); List<Element> tdList = tr.getChildren("td"); //该行到ii个单元格为止被合并的单元格个数 int rowColspan = 0; for(int ii=0;ii<tdList.size();ii++){ //本单元格跨度计算前的td数 int oldIndex = ii+rowColspan; Element td = tdList.get(ii); int colspan = Integer.valueOf(null==td.getAttributeValue("colspan")?"0":td.getAttributeValue("colspan")); //colspan为0或者1证明未合并 colspan = colspan>1?colspan:1; rowColspan += colspan-1; //单元格需要被横向合并声明为1 for(int m=1;m<colspan;m++){ area[i][oldIndex+m]=1; } int rowspan = Integer.valueOf(null==td.getAttributeValue("rowspan")?"0":td.getAttributeValue("rowspan")); rowspan = rowspan>1?rowspan:1; //单元格需要被纵向向合并声明为2 for(int m=1;m<rowspan;m++){ area[m+i][oldIndex] = 2; } } } /*for(int a=0;a<area.length;a++){ for(int b =0;b<area[0].length;b++){ System.out.print(area[a][b]); } System.out.println(""); }*/ return area; } /**- * 设置表头样式 * @param wb * @return */ private CellStyle createHeadStyle(Workbook wb){ CellStyle style = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(headerFont); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); return style; } /**- * 设置表单记录样式 * @param wb * @return */ private CellStyle createBodyStyle(Workbook wb){ CellStyle style = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_NORMAL); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(headerFont); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); return style; } private String getInnerText(Element td){ String txt = ""; if(td.getText()==null || td.getText().equals("")){ if(null != td.getChildren()){ for(int i=0;i<td.getChildren().size();i++){ Element e = (Element)td.getChildren().get(i); txt += getInnerText(e); } } }else{ txt = td.getText(); } return txt; } public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub TableToExcelUtil tu = new TableToExcelUtil(); // System.out.println(tu.getInnerHtml(\"<td><a>1</a></td>\")); tu.createExcelFormTable("缴费统计", "<table><tr class=\"titlebg\"><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">序号</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">计划</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">部门名称</td><td align=\"center\" nowrap=\"nowrap\" colspan=\"6\">线上缴费</td><td align=\"center\" nowrap=\"nowrap\" colspan=\"3\">线下缴费</td><td align=\"center\" nowrap=\"nowrap\" rowspan=\"2\" colspan=\"1\">总计</td></tr><tr class=\"titlebg\"><td align=\"center\" nowrap=\"nowrap\">线上总计</td><td align=\"center\" nowrap=\"nowrap\">快钱</td><td align=\"center\" nowrap=\"nowrap\">支付宝</td><td align=\"center\" nowrap=\"nowrap\">平台余款</td><td align=\"center\" nowrap=\"nowrap\">激活卡</td><td align=\"center\" nowrap=\"nowrap\">其他</td><td align=\"center\" nowrap=\"nowrap\">线下总计</td><td align=\"center\" nowrap=\"nowrap\">本地缴费</td><td align=\"center\" nowrap=\"nowrap\">中心收费</td></tr><tr class=\"whbg\" orgPath=\"01.25.01.\" planId=\"9e508516-5409-4b5d-a0d6-f86ba77eb79f\" ><td align=\"center\" nowrap=\"nowrap\">1</td><td align=\"center\" nowrap=\"nowrap\">盐城2013年培训计划</td><td align=\"center\" nowrap=\"nowrap\">盐城市</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\"><a id=\"0-12\" href=\"javascript:showDetail('0-12');\">3</a></td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\">0</td><td align=\"center\" nowrap=\"nowrap\"><a id=\"0-15\" href=\"javascript:showDetail('0-15');\">3</a></td></tr><tr class=\"whbg\" orgPath=\"all\" planId=\"all\"><td align=\"center\" nowrap=\"nowrap\" colspan=\"3\" >总计</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" ><a id=\"4-6\" href=\"javascript:showDetail('4-6');\">3</a></td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" >0</td><td align=\"center\" nowrap=\"nowrap\" ><a id=\"4-9\" href=\"javascript:showDetail('4-9');\">3</a></td></tr></table>", 2); } }
用jdom解析table的html结构,用poi生成excel,html效果如下:
导出的excel效果如下:
原getCellArea有bug,不能满足同一个单元格既跨行又跨列的情况,修复如下:
private static int[][] getCellArea1(List<Element> trList){ //获取table单元格矩阵 Element headtr = trList.get(0); List<Element> headTdList = headtr.getChildren("td"); //每行的未经合并的单元格个数 int cols = 0; for(Element e:headTdList){ System.out.println("#"+e.getText()); int colspan = Integer.valueOf(null==e.getAttributeValue("colspan")?"0":e.getAttributeValue("colspan")); if(colspan==0){ colspan =1; } cols += colspan; } //初始化单元格矩阵 int[][] area = new int[trList.size()][cols]; for(int i=0;i<trList.size();i++){ Element tr = trList.get(i); List<Element> tdList = tr.getChildren("td"); for(int ii=0,tdIndex=0;ii<cols;ii++){ //如果大于0,表明已经处理过,不需再处理 if(area[i][ii]>0){ continue; } Element td = tdList.get(tdIndex); int colspan = Integer.valueOf(null==td.getAttributeValue("colspan")?"0":td.getAttributeValue("colspan")); colspan = colspan>1?colspan:1; //单元格需要被横向合并声明为1 for(int m=1;m<colspan;m++){ area[i][ii+m]=1; } int rowspan = Integer.valueOf(null==td.getAttributeValue("rowspan")?"0":td.getAttributeValue("rowspan")); rowspan = rowspan>1?rowspan:1; //单元格需要被纵向向合并声明为2 for(int m=1;m<rowspan;m++){ area[m+i][ii] = 2; } //列和行都有跨度的区域,把第一行,第一列外的区域置为2 if(colspan>1 && rowspan>1){ for(int j=1;j<rowspan;j++){ for(int k=1;k<colspan;k++){ area[i+j][ii+k]=2; } } } tdIndex ++; } } return area; }
相关推荐
table表格导出为excel,方便快捷,非常实用,可以私信我我给你私信发送
table表格把数据导出到excel中到本地显示
用Javascript实现的将网页table中的数据导出到excel表格中!JSP ASP.NET 静态页面都可以使用!代码简单实用!
纯JS将table表格导出到excel的方法 html <div > ('tableExcel','myDiv')">IE导出Excel方法</button> ('tableExcel')">Chrome导出Excel</button> </div> <div id="myDiv"> <table id="tableExcel" ...
把table列表导出为excel格式,可自定义表格宽高,表名字,等等.
layui数据表格导出Excel插件 博客地址: https://www.cnblogs.com/YunRuiSoftWare/p/13346396.html
工作之余整理的,可用于实现在前端页面导出table表格中的数据到excel,支持生成json、excel等格式。
javascript代码可以将HTML的table表格转换成excel表格。var table = document.getElementById("table")[removed]; export2Excel(table, '导出.xls');
jquery插件将html表格导出为excel格式文件tableexport
tableExport_html表格导出excel等多种格式,只需要导入一个js就可以简单易用,支持多种格式
jquery-table2excel是一款可以将HTML表格的内容导出到微软Excel电子表格中的jQuery插件。该插件可以根据你的需要导出表格中的内容,不需要的行可以不导出。它文件体积小,使用非常方便。
Ligerui Table 导出excel表格 (基于jquery) 1.支持原样式导出 2.支持带图片导出 3.支持固定行固定列导出
亲测 vue el-table前端使用file-saver组件,导出excel ,可导出分页数据,里面有详细步骤
该插件可以将Html的表格导出成为 JSON, XML, CSV, TSV, TXT, SQL, Word, Excel, PNG and PDF格式,支持的导出格式还是挺多的,基本能满足自己的需求。 因为github上作者给出的插件的使用描述已经很详细了,我就不在...
将页面中的HtmlTable,导出为Execl 支持合并单位元、合并行等操作。续之前的优化了计算算法,升级了代码。
改文件中有基于bootstrap的demo,和用于其他前端框架的js文件,文件夹中有相应的说明,其他的我也不知道有什么好说的了
由于项目需要,关于js将table转为excel,纠结了好久,在网上找了很多资料,前面也上传了两个版本,后来发现问题很多,到目前为止这个版本是兼容性最好。可以自定义文件名,下载表格有边框,有标题,欢迎下载;
table表格导出excel插件修改版,解决IE以及兼容模式下导出{table[0]}的问题
自从上次用bootstrap table就一直没有解决导出excel的关题,网上找到的那更改tableExport.js和jquery.base64.js的方法虽然可以正常导出,但中文依旧乱码... 解决方法终归是有的....现将文件分享给大家. 里面有table...
echarts图表导出到excel中的解决方法,做个备忘录,以防忘记