Apache POI:使用 XLSX 文件中的格式化文本创建 DOCX
Apache POI : Using formatted text from XLSX file to create DOCX
使用 Apache POI,我需要使用从 .xlsx 电子表格中提取的文本创建一个 .docx 文件。简单的文本和数据一切顺利。
问题是某些单元格具有格式化文本,我可以在其中轻松检索 XSSFRichTextString
对象。我需要在输出 .docx 文档中保留此格式。
这里的代码对于 post 来说太长太复杂,但基本上我最终得到了 XWPFParagraph
对象和必须进入其中的 XSSFRichTextString
对象。据我了解,文本必须通过 XWPFRun
对象添加到 XWPFParagraph
对象。
另一个问题是我卡在 Apache POI 3.16 上。
这可以做到吗?
将此作为答案发布,以便我可以添加代码,但解决方案基于 How to display the Excel Cell content along with its styling in XHTML page?,正如 Axel Richter 在评论中所建议的那样。
// from caller
XSSFRichTextString richTextString;
XSSFCellStyle cellStyle;
String textString = richTextString.getString();
for (int i = 0; i < richTextString.numFormattingRuns(); i++) {
int nTextPos = richTextString.getIndexOfFormattingRun(i);
String textPart = textString.substring(nTextPos, nTextPos + richTextString.getLengthOfFormattingRun(i));
Font font = richTextString.getFontOfFormattingRun(i);
boolean withLineBreak = false;
do {
XWPFRun _run = p.createRun();
String sThisPart = textPart;
// break into multiple strings
int nNewLinePos = textPart.indexOf("\n");
if (nNewLinePos != -1) {
sThisPart = textPart.substring(0, nNewLinePos);
textPart = textPart.substring(nNewLinePos + 1);
}
else {
textPart = "";
}
if (withLineBreak) {
_run.addBreak();
}
_run.setText(sThisPart);
if (font != null) {
// from richtextstring
_run.setFontFamily(font.getFontName());
_run.setBold(font.getBold());
_run.setItalic(font.getItalic());
}
else if (cellStyle != null) {
// from cell style
_run.setFontFamily(cellStyle.getFont().getFontName());
_run.setBold(cellStyle.getFont().getBold());
_run.setItalic(cellStyle.getFont().getItalic());
}
p.addRun(_run);
withLineBreak = true;
}
while (!textPart.isEmpty());
}
请注意,Font
对象的某些属性必须转换为文本 运行 中的可用数据,例如字体大小。
可以设置从富文本到文档的 mote 文本属性 运行。对我来说,它像这样工作得很好。
使用 Apache POI,我需要使用从 .xlsx 电子表格中提取的文本创建一个 .docx 文件。简单的文本和数据一切顺利。
问题是某些单元格具有格式化文本,我可以在其中轻松检索 XSSFRichTextString
对象。我需要在输出 .docx 文档中保留此格式。
这里的代码对于 post 来说太长太复杂,但基本上我最终得到了 XWPFParagraph
对象和必须进入其中的 XSSFRichTextString
对象。据我了解,文本必须通过 XWPFRun
对象添加到 XWPFParagraph
对象。
另一个问题是我卡在 Apache POI 3.16 上。
这可以做到吗?
将此作为答案发布,以便我可以添加代码,但解决方案基于 How to display the Excel Cell content along with its styling in XHTML page?,正如 Axel Richter 在评论中所建议的那样。
// from caller
XSSFRichTextString richTextString;
XSSFCellStyle cellStyle;
String textString = richTextString.getString();
for (int i = 0; i < richTextString.numFormattingRuns(); i++) {
int nTextPos = richTextString.getIndexOfFormattingRun(i);
String textPart = textString.substring(nTextPos, nTextPos + richTextString.getLengthOfFormattingRun(i));
Font font = richTextString.getFontOfFormattingRun(i);
boolean withLineBreak = false;
do {
XWPFRun _run = p.createRun();
String sThisPart = textPart;
// break into multiple strings
int nNewLinePos = textPart.indexOf("\n");
if (nNewLinePos != -1) {
sThisPart = textPart.substring(0, nNewLinePos);
textPart = textPart.substring(nNewLinePos + 1);
}
else {
textPart = "";
}
if (withLineBreak) {
_run.addBreak();
}
_run.setText(sThisPart);
if (font != null) {
// from richtextstring
_run.setFontFamily(font.getFontName());
_run.setBold(font.getBold());
_run.setItalic(font.getItalic());
}
else if (cellStyle != null) {
// from cell style
_run.setFontFamily(cellStyle.getFont().getFontName());
_run.setBold(cellStyle.getFont().getBold());
_run.setItalic(cellStyle.getFont().getItalic());
}
p.addRun(_run);
withLineBreak = true;
}
while (!textPart.isEmpty());
}
请注意,Font
对象的某些属性必须转换为文本 运行 中的可用数据,例如字体大小。
可以设置从富文本到文档的 mote 文本属性 运行。对我来说,它像这样工作得很好。