tableToExcel工具类,此工具类指定格式的表格进行转Excel
格式:其中不能带有thead,tbody和th标签
<table> <tr> <td>表头1</td> <td>表头2</td> <td>表头3</td> <td>表头4</td> </tr> <tr> <td>数据1</td> <td>数据2</td> <td>数据3</td> <td>数据4</td> </tr> <tr> <td>数据1</td> <td>数据2</td> <td>数据3</td> <td>数据4</td> </tr> </table>
工具类:
package com.zx.erjiqualitydata; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.springframework.util.ClassUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; public class TableToExcel { /** * @author lusong * @param sheetName * @param html * @param headNum表头的行数 * @throws FileNotFoundException * zyn * 2012-12-21 下午1:44:02 */ public static String tableToExcel(String sheetName,String html,int headNum) throws FileNotFoundException{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(sheetName); CellStyle headStyle = createHeadStyle(wb); CellStyle bodyStyle = createBodyStyle(wb); HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String filePath = request.getSession().getServletContext().getRealPath("/static/document"); String path = filePath+"\"+sheetName+".xlsx"; FileOutputStream os = new FileOutputStream(path); SAXBuilder sb = new SAXBuilder(); try { ByteArrayInputStream is = new ByteArrayInputStream(html.getBytes("UTF-8")); 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(getInnerText(tdList.get(tdIndex))); tdIndex ++; } } } wb.write(os); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "static/document/"+sheetName+".xlsx"; } /** * 导出excel表格二维数组:0为文字占用格,1为横向被合并格,2为纵向合并格 * @param trList * @return * zyn * 2012-12-21 下午1:35:40 */ private static 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 static 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 static 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 static 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>")); String tableHtmlString = "<table class="zx_total_table dataTable no-footer" style=" 100%; margin: 0px auto;" id="rf_table" role="grid"><thead><tr role="row" style="height: 0px;"><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 241px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">医疗机构名称</div></th><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 162px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">级别</div></th><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 83px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">类别</div></th><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 83px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">属性</div></th><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 163px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">所有制</div></th><th class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 441px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;">所在地</div></th></tr></thead><tbody><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-长安区</td></tr><tr role="row" class="even"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>阿斯蒂芬</td><td>北京-北京市-东城区</td></tr></tbody>"; //tableToExcel("缴费统计", "<table ><tr role="row" style="height: 0px;"><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 174px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 174px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 89px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 90px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 174px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 472px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;"><div class="dataTables_sizing" style="height:0;overflow:hidden;"></div></td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-长安区</td></tr><tr role="row" class="even"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>阿斯蒂芬</td><td>北京-北京市-东城区</td></tr></table>", 1); tableToExcel("缴费统计", "<table class="zx_total_table dataTable no-footer" style=" 100%; margin: 0px auto;" id="rf_table" role="grid"><tr role="row" style="height: 0px;"><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 241px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">医疗机构名称</td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 162px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">级别</td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 83px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">类别</td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 83px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">属性</td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 163px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">所有制</td><td class="sorting" aria-controls="rf_table" rowspan="1" colspan="1" style=" 441px; padding-top: 0px; padding-bottom: 0px; border-top- 0px; border-bottom- 0px; height: 0px;">所在地</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">河北二院</td><td>三级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="even"><td class="sorting_1">河北二院</td><td>二级医院</td><td>综合</td><td>公立</td><td>私有制</td><td>河北省-石家庄市-裕华区</td></tr><tr role="row" class="odd"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>公有制</td><td>河北省-石家庄市-长安区</td></tr><tr role="row" class="even"><td class="sorting_1">省三院</td><td>二级医院</td><td>综合</td><td>公立</td><td>阿斯蒂芬</td><td>北京-北京市-东城区</td></tr></table>", 1); } }