Java 如何在DOCX中使用POI从单元格顶部写入
我正在读取两个docx文件,并用第二个文件中的文本替换第一个文件表格单元格中的文本。要替换的单元格中有一个标记([#指示器])。 文本将写入表的单元格中,标记确实存在。我的问题是,在写入单元格后,单元格的开头总是有一些空白。图片中的单词“Rechnerische Ergebnisse:”上方。 如何在单元格的开头写入或删除空白? 代码如下:Java 如何在DOCX中使用POI从单元格顶部写入,java,apache-poi,docx,Java,Apache Poi,Docx,我正在读取两个docx文件,并用第二个文件中的文本替换第一个文件表格单元格中的文本。要替换的单元格中有一个标记([#指示器])。 文本将写入表的单元格中,标记确实存在。我的问题是,在写入单元格后,单元格的开头总是有一些空白。图片中的单词“Rechnerische Ergebnisse:”上方。 如何在单元格的开头写入或删除空白? 代码如下: private void insertText(final XWPFDocument docxErsetzt) throws FileNotFoundExc
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();
}
private void insertText(最终XWPFDocument docxErsetzt)抛出FileNotFoundException、IOException{
FileInputStream fis=新的FileInputStream(“src\\main\\resources\\WordTemplate\\text.docx”);
XWPFDocument textInhalt=新的XWPFDocument(fis);
列出paragraphsInhat=textInhalt.getparaphs();
List table=docxErsetzt.getTables();
用于(XWPFTable XWPFTable:table){
List row=xwpfTable.getRows();
对于(XWPFTableRow XWPFTableRow:row){
列表单元格=xwpfTableRow.getTableCells();
用于(XWPFTableCell c:单元){
if(c.getText()包含(“[#指示符#]”){
//移除[#指示器#]
c、 删除第(0)款;
对于(XWPF段落:段落内容){
XWPFRun newRun=c.addparagration().createRun();
newRun.removeBreak();
newRun.removeCarriageReturn();
newRun.setText(para.getText(),0);
对于(XWPFRun:para.getRuns()){
字符串textInRun=run.getText(0);
if(textInRun==null | | textInRun.isEmpty()){
继续;
}
newRun.setFontSize(run.getFontSize());
newRun.setFontFamily(run.getFontFamily());
newRun.setBold(run.isBold());
newRun.setItalic(run.isItalic());
newRun.setStriketThrough(run.isStriketThrough());
newRun.setUnderline(run.getUnderline());
newRun.setColor(run.getColor());
}
}
}
}
}
}
write(新文件OutputStream(OUTPUTPATH+“textreplaced.docx”);
docxErsetzt.close();
textInhalt.close();
}
一个XWPFParagraph
由一个或多个XWPFRun
实例组成,不要删除该段落,尝试以以下方式更改其内容:
public void changeText(XWPFParagraph p, String newText) {
List<XWPFRun> runs = p.getRuns();
for(int i = runs.size() - 1; i > 0; i--) {
p.removeRun(i);
}
XWPFRun run = runs.get(0);
run.setText(newText, 0);
}
public void changeText(XWPFParagraph p,String newText){
List runs=p.getRuns();
对于(int i=runs.size()-1;i>0;i--){
p、 联合国(一);
}
XWPFRun=runs.get(0);
run.setText(newText,0);
}
一个XWPFParagraph
由一个或多个XWPFRun
实例组成,不要删除该段落,尝试以以下方式更改其内容:
public void changeText(XWPFParagraph p, String newText) {
List<XWPFRun> runs = p.getRuns();
for(int i = runs.size() - 1; i > 0; i--) {
p.removeRun(i);
}
XWPFRun run = runs.get(0);
run.setText(newText, 0);
}
public void changeText(XWPFParagraph p,String newText){
List runs=p.getRuns();
对于(int i=runs.size()-1;i>0;i--){
p、 联合国(一);
}
XWPFRun=runs.get(0);
run.setText(newText,0);
}
删除段落不也会删除段落的行吗?@Iman段落有很多样式信息删除它会影响您的视图不删除段落也会删除它的行吗?@Iman段落有很多样式信息删除它会影响您的视图