poi怎么将rtf转成html,java - Convert a POI RichTextString to HTML or RTF - Stack Overflow

Thanks to Gagravarr who pointed me to the ToHTML example.

The examples only did cell wide markup - they did not take into account formatting with in a cell.

Calling formatCell() will convert the cell to html.

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.ss.format.CellFormat;

import org.apache.poi.ss.format.CellFormatResult;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.CellStyle;

import static org.apache.poi.ss.usermodel.CellStyle.*;

import org.apache.poi.ss.usermodel.Font;

import org.apache.poi.ss.usermodel.Sheet;

/**

*

* @author Anthony Symons

*/

public class RichTextStringToHtml {

private final HSSFWorkbook wb;

private final HSSFPalette colors;

private StringBuilder sb;

private Cell cell;

private Sheet sheet;

private static final Map ALIGN = mapFor(ALIGN_LEFT, "left",

ALIGN_CENTER, "center", ALIGN_RIGHT, "right", ALIGN_FILL, "left",

ALIGN_JUSTIFY, "left", ALIGN_CENTER_SELECTION, "center");

private static final Map VERTICAL_ALIGN = mapFor(

VERTICAL_BOTTOM, "bottom", VERTICAL_CENTER, "middle", VERTICAL_TOP,

"top");

private static final Map BORDER = mapFor(BORDER_DASH_DOT,

"dashed 1pt", BORDER_DASH_DOT_DOT, "dashed 1pt", BORDER_DASHED,

"dashed 1pt", BORDER_DOTTED, "dotted 1pt", BORDER_DOUBLE,

"double 3pt", BORDER_HAIR, "solid 1px", BORDER_MEDIUM, "solid 2pt",

BORDER_MEDIUM_DASH_DOT, "dashed 2pt", BORDER_MEDIUM_DASH_DOT_DOT,

"dashed 2pt", BORDER_MEDIUM_DASHED, "dashed 2pt", BORDER_NONE,

"none", BORDER_SLANTED_DASH_DOT, "dashed 2pt", BORDER_THICK,

"solid 3pt", BORDER_THIN, "dashed 1pt");

private static final HSSFColor HSSF_AUTO = new HSSFColor.AUTOMATIC();

public RichTextStringToHtml(Cell c, StringBuilder sb) {

cell = c;

sheet = cell.getSheet();

wb = (HSSFWorkbook) sheet.getWorkbook();

colors = wb.getCustomPalette();

this.sb = sb;

}

private static Map mapFor(Object... mapping) {

Map map = new HashMap();

for (int i = 0; i < mapping.length; i += 2) {

map.put((K) mapping[i], (V) mapping[i + 1]);

}

return map;

}

public void colorStyles(CellStyle style) {

HSSFCellStyle cs = (HSSFCellStyle) style;

styleColor("background-color", cs.getFillForegroundColor());

styleColor("color", cs.getFont(wb).getColor());

}

private void styleColor(String attr, short index) {

HSSFColor color = colors.getColor(index);

if (index == HSSF_AUTO.getIndex() || color == null) {

} else {

short[] rgb = color.getTriplet();

sb.append(String.format(" %s: #%02x%02x%02x; /* index = %d */", attr, rgb[0],

rgb[1], rgb[2], index));

}

}

private void styleOut(String attr, K key, Map mapping) {

String value = mapping.get(key);

if (value != null) {

sb.append(" ").append(attr).append(": ").append(value);

}

}

private void fontStyle(short fontIndex) {

Font font = wb.getFontAt(fontIndex);

if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_NORMAL) {

sb.append(" font-weight: bold;");

}

if (font.getItalic()) {

sb.append(" font-style: italic;");

}

int fontheight = font.getFontHeightInPoints();

if (fontheight == 9) {

//fix for stupid ol Windows

fontheight = 10;

}

sb.append(" font-size: ").append(fontheight).append("pt;");

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

// Font color is handled with the other colors

}

private void getHtmlFromHss(HSSFRichTextString richTextString) {

//List formattingRuns = new ArrayList();

int numFormattingRuns = richTextString.numFormattingRuns();

String baseString = richTextString.getString();

CellStyle cs = cell.getCellStyle();

short csFont = cs.getFontIndex();

sb.append("

fontStyle(csFont);

sb.append('"').append(">");

if (numFormattingRuns <= 0) {

// no formatting so just copy in the string

sb.append(StringEscapeUtils.escapeHtml4(baseString));

sb.append("

");

return;

}

int firstIndex = richTextString.getIndexOfFormattingRun(0);

int currOffset = 0;

while (currOffset < firstIndex) {

sb.append(escapeHtml(baseString.charAt(currOffset)));

currOffset++;

}

for (int fmtIdx = 0; fmtIdx < numFormattingRuns; fmtIdx++) {

int begin = richTextString.getIndexOfFormattingRun(fmtIdx);

short fontIndex = richTextString.getFontOfFormattingRun(fmtIdx);

// apply the font at this point

// Walk the string to determine the length of the formatting run.

sb.append("

fontStyle(fontIndex);

sb.append('"').append(">");

for (int j = begin; j < richTextString.length(); j++) {

short currFontIndex = richTextString.getFontAtIndex(j);

if (currFontIndex == fontIndex) {

sb.append(escapeHtml(baseString.charAt(currOffset)));

currOffset++;

} else {

break;

}

}

//formattingRuns.add(new FormattingRun(begin, length, fontIndex));

}

sb.append("

");

}

public String escapeHtml(char in) {

switch (in) {

case '\n':

case '\r':

return "
";

}

return StringEscapeUtils.escapeHtml4("" + in);

}

public void formatCell() {

String content = null;

String attrs = "";

CellStyle style = null;

style = cell.getCellStyle();

attrs = tagStyle(cell, style);

//Set the value that is rendered for the cell

//also applies the format

CellFormat cf = CellFormat.getInstance(

style.getDataFormatString());

CellFormatResult result = cf.apply(cell);

try {

getHtmlFromHss((HSSFRichTextString) cell.getRichStringCellValue());

} catch (java.lang.IllegalStateException ex) {

}

}

private String tagStyle(Cell cell, CellStyle style) {

if (style.getAlignment() == ALIGN_GENERAL) {

switch (ultimateCellType(cell)) {

case HSSFCell.CELL_TYPE_STRING:

return "style=\"text-align: left;\"";

case HSSFCell.CELL_TYPE_BOOLEAN:

case HSSFCell.CELL_TYPE_ERROR:

return "style=\"text-align: center;\"";

case HSSFCell.CELL_TYPE_NUMERIC:

default:

// "right" is the default

break;

}

}

return "";

}

private static int ultimateCellType(Cell c) {

int type = c.getCellType();

if (type == Cell.CELL_TYPE_FORMULA) {

type = c.getCachedFormulaResultType();

}

return type;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值