当前位置:Gxlcms > html代码 > Excel转成HtmlExcel预览_html/css_WEB-ITnose

Excel转成HtmlExcel预览_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:36人阅读

基于Poi demo做的,效果还不错,这里给大家分享下,先看看效果




效果基本上一致,这里说下如何做的

1.合并单元格的处理

/**

* 根据合并输出内容,处理合并单元格事情

* @param formatAsString

*/

private void handerMergedString(String formatAsString) {

String[] strArr = formatAsString.split( ":" );

int startCol = strArr[0].charAt(0) - 65;

int startRol = Integer.valueOf(strArr[0].substring(1));

int endCol = strArr[1].charAt(0) - 65;

int endRol = Integer.valueOf(strArr[1].substring(1));

mergedCache.put(startRol + "_" + startCol, new Integer[] { endRol - startRol + 1,

endCol - startCol + 1 });

for (int i = startRol; i <= endRol; i++) {

for (int j = startCol; j <= endCol; j++) {

notNeedCread.add(i + "_" + j);

}

}

notNeedCread.remove(startRol + "_" + startCol);

}

把整个sheet的合并单元格,存放起来,然后便利的时候判断下,给td写写 rowspan和colsopan 就可以了,相对比较简单


2.CSS的设置

private void styleContents(CellStyle style) {

if (style.getAlignment() != 2) {

styleOut( "text-align" , style.getAlignment(), ALIGN);

styleOut( "vertical-align" , style.getAlignment(), VERTICAL_ALIGN);

}

helper.colorStyles(style, out);

}

private void fontStyle(Font font) {

if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD)

out.format( " font-weight: bold;%n" );

if (font.getItalic())

out.format( " font-style: italic;%n" );

out.format( " font-family: %s;%n" , font.getFontName());

int fontheight = font.getFontHeightInPoints();

if (fontheight == 9) {

fontheight = 10;

}

out.format( " font-size: %dpt;%n" , fontheight);

helper.styleColor(out, "color" , getColor(font));

}


从cellStyle 里面吧值获取出来,然后填写到css当中,已经font的css,这个理font和style要区分因为后面复杂的单元格要迭代设置的font,另外

03和07版本很多不同的东西,需要分开设置


3.值的获取--比较重要和复杂的

public String getHtmlValue(Cell cell) {

if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()

|| Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {

cell.setCellType(Cell.CELL_TYPE_STRING);

return cell.getStringCellValue();

} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {

if (cell.getRichStringCellValue().numFormattingRuns() == 0) {

return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue());

} else if (is07) {

return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue());

} else {

return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue());

}

}

return "" ;

}


判断cell类型,然后设置值,如果是String,需要判断是不是复杂的text,如果是,需要按照03,和07分别处理,迭代列类型列表然后输出


总的来说比自己想想的容易的多


使用方法---只能一个sheet一个sheet的输出

/**

* Excel 变成界面

* @author JueYue

* @date 2015年5月10日 上午11:51:48

*/

public final class ExcelToHtmlUtil {

private ExcelToHtmlUtil() {

}

/**

* 转换成为Table

* @param wb Excel

* @return

*/

public static String toTableHtml(Workbook wb) {

return new ExcelToHtmlServer(wb, false , 0).printPage();

}

/**

* 转换成为Table

* @param wb Excel

* @param sheetNum sheetNum

* @return

*/

public static String toTableHtml(Workbook wb, int sheetNum) {

return new ExcelToHtmlServer(wb, false , sheetNum).printPage();

}

/**

* 转换成为完整界面

* @param wb Excel

* @param sheetNum sheetNum

* @return

*/

public static String toAllHtml(Workbook wb) {

return new ExcelToHtmlServer(wb, true , 0).printPage();

}

/**

* 转换成为完整界面

* @param wb Excel

* @param sheetNum sheetNum

* @return

*/

public static String toAllHtml(Workbook wb, int sheetNum) {

return new ExcelToHtmlServer(wb, true , sheetNum).printPage();

}

}



源码地址EasyPoi

人气教程排行