有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带变量替换的docx4j中的java插入换行符

我有一个带有特定变量的模板,基本上我需要用数据库中的信息来替换它。 当我只想用一个词来替换时,这很容易,我的问题是当我需要像这样断线时:

这两个人的CPF应该来自我的变量,但这就是我得到的:

这就是我现在的代码,我试着把“\n”、“
”和“<;w:\br>;”,它们都不起作用,现在我把“\n”放在我想打破界限的地方:

private String criaIdentificacaoAssistenciaSocial(Lote lote) throws IOException {
    StringBuilder identificacaoAS = new StringBuilder();
    for (Pessoa pessoa : lote.getJsonBeneficiarios()) {
        PessoaFisica beneficiario = (PessoaFisica) pessoa;
        identificacaoAS.append(beneficiario.getNome()).append(", CPF ").append(beneficiario.getCpf());
        identificacaoAS.append("\n");
        if (beneficiario.getConjuge() != null) {
            identificacaoAS.append(beneficiario.getConjuge().getNome()).append(", CPF ").append(beneficiario.getConjuge().getCpf());
            identificacaoAS.append("\n");
        }
    }
    return identificacaoAS.toString();
}

我的返回值在mappingDocsVariable中,然后我用以下代码生成docx文件:

private File geraDocumento(HashMap<String, String> mappingDocVariables, String nomeDocx) throws Exception {
    String inputfilepath = System.getProperty("user.dir") + File.separator
            + "utils" + File.separator + nomeDocx + ".docx";

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    VariablePrepare.prepare(wordMLPackage);
    documentPart.variableReplace(mappingDocVariables);
    String tDir = System.getProperty("java.io.tmpdir");
    File temp = new File(tDir + nomeDocx + ".docx");
    Docx4J.save(wordMLPackage, temp);
    temp.deleteOnExit();
    return temp;
}

共 (1) 个答案