poi 修改html样式,Apache POI - 将格式输出格式设置为HTML

基于my version for DocX,这里是HSSF改编版本。与其他版本一样,您必须调试并扩展各种CSS样式的循环。

更新:我昨天已经忽略了,你想有一个流XSSF解决方案,所以我摆弄周围,如果有可能,只是使用的usermodel类(不是真的,当涉及到字体颜色),而且我不知道为什么SXSSF没有使用任何我的字体设置,直到我发现,通过设计的目前(见错误52484)

import java.awt.Color;

import java.io.FileOutputStream;

import java.lang.reflect.Field;

import java.util.Enumeration;

import javax.swing.text.*;

import javax.swing.text.html.*;

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

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

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

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

public class StyledTextXls {

public static void main(String[] args) throws Exception {

HTMLEditorKit kit = new HTMLEditorKit();

HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();

kit.insertHTML(doc, doc.getLength(), "

paragraph 1

", 0, 0, null);

kit.insertHTML(doc, doc.getLength(), "

paragraph 2

", 0, 0, null);

Workbook wb = new XSSFWorkbook();

// Workbook wb = new HSSFWorkbook();

// Workbook wb = new SXSSFWorkbook(100); // doesn't work yet - see Bug 52484

Sheet sheet = wb.createSheet();

Row row = sheet.createRow(0);

Cell cell = row.createCell(0);

StringBuffer sb = new StringBuffer();

for (int lines=0, lastPos=-1; lastPos < doc.getLength(); lines++) {

if (lines > 0) sb.append("\n");

Element line = doc.getParagraphElement(lastPos+1);

lastPos = line.getEndOffset();

for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) {

final Element frag = line.getElement(elIdx);

String subtext = doc.getText(frag.getStartOffset(), frag.getEndOffset()-frag.getStartOffset());

sb.append(subtext);

}

}

CreationHelper ch = wb.getCreationHelper();

RichTextString rt = ch.createRichTextString(sb.toString());

for (int lines=0, lastPos=-1; lastPos < doc.getLength(); lines++) {

Element line = doc.getParagraphElement(lastPos+1);

lastPos = line.getEndOffset();

for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) {

final Element frag = line.getElement(elIdx);

Font font = getFontFromFragment(wb, frag);

rt.applyFont(frag.getStartOffset()+lines, frag.getEndOffset()+lines, font);

}

}

cell.setCellValue(rt);

cell.getCellStyle().setWrapText(true);

row.setHeightInPoints((6*sheet.getDefaultRowHeightInPoints()));

sheet.autoSizeColumn((short)0);

FileOutputStream fos = new FileOutputStream("richtext"+(wb instanceof HSSFWorkbook ? ".xls" : ".xlsx"));

wb.write(fos);

fos.close();

}

static Font getFontFromFragment(Workbook wb, Element frag) {

// creating a font on each is call is not very efficient

// but should be ok for this exercise ...

Font font = wb.createFont();

final AttributeSet as = frag.getAttributes();

final Enumeration> ae = as.getAttributeNames();

while (ae.hasMoreElements()) {

final Object attrib = ae.nextElement();

try {

if (CSS.Attribute.COLOR.equals(attrib)) {

// I don't know how to really work with the CSS-swing class ...

Field f = as.getAttribute(attrib).getClass().getDeclaredField("c");

f.setAccessible(true);

Color c = (Color)f.get(as.getAttribute(attrib));

if (font instanceof XSSFFont) {

((XSSFFont)font).setColor(new XSSFColor(c));

} else if (font instanceof HSSFFont && wb instanceof HSSFWorkbook) {

HSSFPalette pal = ((HSSFWorkbook)wb).getCustomPalette();

HSSFColor col = pal.findSimilarColor(c.getRed(), c.getGreen(), c.getBlue());

((HSSFFont)font).setColor(col.getIndex());

}

} else if (CSS.Attribute.FONT_WEIGHT.equals(attrib)) {

if ("bold".equals(as.getAttribute(attrib).toString())) {

font.setBoldweight(Font.BOLDWEIGHT_BOLD);

}

}

} catch (Exception e) {

System.out.println(attrib.getClass().getCanonicalName()+" can't be handled.");

}

}

return font;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值