简体   繁体   English

如何使用 Java 中的 Apache POI 将文本添加到 a.docx 文件

[英]How to add text to a .docx file using Apache POI in Java

I am new to Java and want to add data to an already existing.docx file and so far i've been unsucessful.我是 Java 的新手,想将数据添加到已经存在的 .docx 文件中,但到目前为止我一直没有成功。 I've been looking for an answer to this for 3 days now and i decided to come to stackoverflow and ask how i could add text to an existing file using apache poi.我一直在寻找这个问题的答案 3 天了,我决定来到 stackoverflow 并询问如何使用 apache poi 将文本添加到现有文件中。

My program is supposed to generate a bunch of random data and store it in a file.我的程序应该生成一堆随机数据并将其存储在一个文件中。 Before Apache, i was using.write() to log my data and it was working well but the file didnt have formatting which was fine in the moment but now i need formatting to be able to get a good look at the thousands of data entries.在 Apache 之前,我使用.write() 来记录我的数据,它运行良好,但文件没有格式化,目前还可以,但现在我需要格式化才能很好地查看成千上万的数据条目.

Here is the code i was using to log the data before:这是我之前用来记录数据的代码:

FileWriter fwriteFinal = new FileWriter("obf_reg.docx", true);
fwriteFinal.write(lbrk);
fwriteFinal.write("\n");
fwriteFinal.write("Your Engine is called: " + engine_Name);
fwriteFinal.write("\n");
fwriteFinal.write("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your Propellant is: " + propellant_List_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Tank repressurisation would be done " + tank_Repressurisation_Chosen);
fwriteFinal.write("\n");
fwriteFinal.close();

and this is what im trying to use:这就是我试图使用的:

XWPFDocument docx = new XWPFDocument();
XWPFParagraph par = docx.createParagraph();
XWPFRun run = par.createRun();
run.setText(lbrk + "\n" + "Your Engine is called: " + engine_Name + "\n" +
            "\n" + "Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen +
            "\n" + "Your Propellant is: " + propellant_List_Chosen +
            "\n" + "Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen +
            "\n" + "Your engine's nozzle type should be : " + nozzle_Type_List_Chosen +            
            "\n" + "Tank repressurisation would be done " + tank_Repressurisation_Chosen);
FileOutputStream out = new FileOutputStream("C:\\Users\\sfsga\\OneDrive\\Desktop\\obf_reg.docx");
docx.write(out);
out.close();

its prolly a dumb mistake but im still gonna ask.这是一个愚蠢的错误,但我还是要问。

The string literal "\n" does not have the usual special meaning in Microsoft Word. 字符串文字"\n"在 Microsoft Word 中没有通常的特殊含义。 There are special settings for line breaks in Word. Word 中的换行符有特殊设置。

You could use XWPFRun.addBreak to add a line break into one text run.您可以使用XWPFRun.addBreak在一个文本运行中添加换行符。 To do so each "\n" in your code would must be replaced by run.addBreak() .为此,您的代码中的每个"\n"都必须替换为run.addBreak()

Something like:就像是:

...
  run.setText(lbrk);
  run.addBreak();
  run.setText("Your Engine is called: " + engine_Name);
  run.addBreak();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  run.addBreak();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  run.addBreak();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  run.addBreak();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  run.addBreak();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);
...

But more Word-lke is to use an additional paragraph for each time you would press Enter in Word's GUI.但更多的 Word-lke 是每次在 Word 的 GUI 中按Enter时使用一个附加段落。 Word also adds a new paragraph when the user press Enter in Word's GUI.当用户在 Word 的 GUI 中按Enter 键时,Word 还会添加一个新段落。

Using different single XWPFRun per text run is also better for formatting later.每个文本运行使用不同的单个XWPFRun也更好地用于稍后的格式化。 All needed different text formatting needs their own XWPFRun .所有需要的不同文本格式都需要自己的XWPFRun

If the need is to append data into the same file each time the code runs, then we need check whether the file already exists and, if so, create the XWPFDocument from that file.如果需要在每次代码运行时将 append 数据放入同一个文件中,那么我们需要检查该文件是否已经存在,如果存在,则从该文件创建XWPFDocument To do so constructor public XWPFDocument(java.io.InputStream is) can be used.为此,可以使用构造函数public XWPFDocument(java.io.InputStream is)

That would look like follows in code:这在代码中如下所示:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

public class CreateWord {

 public static void main(String[] args) throws Exception {
     
  String lbrk = "Sample lbrk";
  String engine_Name = "Sample engine_Name";
  String nuclear_Cycle_Chosen = "Sample nuclear_Cycle_Chosen";
  String engine_Cycle_Chosen = "Sample engine_Cycle_Chosen";
  String propellant_List_Chosen = "Sample propellant_List_Chosen";
  String reactor_Fuel_Chosen = "Sample reactor_Fuel_Chosen";
  String nozzle_Type_List_Chosen = "Sample nozzle_Type_List_Chosen";
  String tank_Repressurisation_Chosen = "Sample tank_Repressurisation_Chosen";
  
  File file = new File("./obf_reg.docx");
  XWPFDocument docx = new XWPFDocument();
  if (file.exists()) {
   docx = new  XWPFDocument(new FileInputStream(file));
  } 

  XWPFParagraph par = docx.createParagraph();
  XWPFRun run = par.createRun();

/*
  run.setText(lbrk);
  run.addBreak();
  run.setText("Your Engine is called: " + engine_Name);
  run.addBreak();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  run.addBreak();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  run.addBreak();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  run.addBreak();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  run.addBreak();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);

  par = docx.createParagraph();
*/

  par = docx.createParagraph(); run = par.createRun();
  run.setText(lbrk);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Engine is called: " + engine_Name);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);

  FileOutputStream out = new FileOutputStream(file);
  docx.write(out);
  out.close();
  docx.close();

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM