0
2つのdocxファイルを読み込み、最初のセルのテキストを2番目のテキストのテキストに置き換えます。置換されるべき細胞はそれらの中にマーカーを有する([#指示番号])。 テキストは、マーカーが存在するテーブルのセルに書き込まれます。私の問題は、セルに書いた後にセルの先頭に空きスペースがあるということです。画像に「Rechnerische Ergebnisse:」という単語があります。 セルの先頭に書き込む方法、または空のスペースを削除する方法はありますか?その内容を変更しようと、このよう段落を削除しないXWPFParagraph
は、1つまたは複数のXWPFRun
インスタンスで構成されてDOCXでPOIを使用してセルの上から書き込む方法
private void insertText(final XWPFDocument docxErsetzt) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("src\\main\\resources\\WordTemplate\\text.docx");
XWPFDocument textInhalt = new XWPFDocument(fis);
List<XWPFParagraph> paragraphsInhat = textInhalt.getParagraphs();
List<XWPFTable> table = docxErsetzt.getTables();
for (XWPFTable xwpfTable : table) {
List<XWPFTableRow> row = xwpfTable.getRows();
for (XWPFTableRow xwpfTableRow : row) {
List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
for (XWPFTableCell c : cell) {
if (c.getText().contains("[#indicator#]")) {
// remove [#indicator#]
c.removeParagraph(0);
for (XWPFParagraph para : paragraphsInhat) {
XWPFRun newRun = c.addParagraph().createRun();
newRun.removeBreak();
newRun.removeCarriageReturn();
newRun.setText(para.getText(), 0);
for (XWPFRun run : para.getRuns()) {
String textInRun = run.getText(0);
if (textInRun == null || textInRun.isEmpty()) {
continue;
}
newRun.setFontSize(run.getFontSize());
newRun.setFontFamily(run.getFontFamily());
newRun.setBold(run.isBold());
newRun.setItalic(run.isItalic());
newRun.setStrikeThrough(run.isStrikeThrough());
newRun.setUnderline(run.getUnderline());
newRun.setColor(run.getColor());
}
}
}
}
}
}
docxErsetzt.write(new FileOutputStream(OUTPUTPATH + "textreplaced.docx"));
docxErsetzt.close();
textInhalt.close();
}
を削除しません。ここ コードです段落もその実行を削除しますか? – Iman
@Iman段落には多くのスタイル情報が含まれています。削除するとビューに影響します –