Zuhlek
Zuhlek

Reputation: 3

Writing äöü to Word using Apache POI

I am working on a Java application which creates .docx files using Apache POI. However, "äöü" is not correctly displayed in the word file when I open it manually from the Intelij navigation after it was created. Do you have an idea what I might want to check/amend?

public void generateProtocolTEST() throws IOException {
    if (!Paths.get("./protocols").toFile().exists()) Files.createDirectories(Paths.get("./protocols"));
    XWPFDocument document = new XWPFDocument();
    
    FileOutputStream out = new FileOutputStream("protocols/" + "test.docx");

    document.createParagraph().createRun().setText("äüö");

    document.write(out);
    out.close();
}

Result IntelliJ settings

Solution:

  1. Added "[compileJava, compileTestJava].options.encoding = 'UTF-8'" to build.gradle file.
  2. Build the project via the gradle navigation again.
  3. Restarted the IDE.

Upvotes: 0

Views: 229

Answers (1)

Mario Varchmin
Mario Varchmin

Reputation: 3782

If you didn't have UTF-8 set in IntelliJ before, you might have to recreate the .java file, which contains the code you show in your question (a restart of the IDE would probably also be a good idea), as changed encoding settings are effective for new files only.

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006974119-File-encoding-ignored-by-compiler

Upvotes: 1

Related Questions