【问题标题】:How to convert odt to docx with xdocreport?如何使用 xdocreport 将 odt 转换为 docx?
【发布时间】:2014-03-03 04:08:51
【问题描述】:

我使用 xdocreport 1.0.3,我想上传一个 odt 模板,对其进行处理,最后得到一个 docx 处理的文档。我该怎么做?

我试过了:

public void generateUserReport(long pcCaseId, long currentUserId) throws CMSException{
    try {
    InputStream is = new FileInputStream(CustomTemplate.TEMPLATES_PATH + "test.odt");
    IXDocReport report;
    report = XDocReportRegistry.getRegistry().loadReport(is,TemplateEngineKind.Velocity);
    IContext context = report.createContext();

    FieldsMetadata metadata = new FieldsMetadata();
    metadata.addFieldAsImage("signature");
    metadata.addFieldAsImage("logo");

     Date currentDate = new Date();
     SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");
     context.put("currentDate", df.format(currentDate));

    User user = this.userDAO.loadUser(currentUserId);
    byte[] signatureByteArr = user.getSignature();
    context.put("userName", user.getFullName());

    //TODO If exists signature, it will be added, in other case?
    if (signatureByteArr!=null){
        FileOutputStream fos = new FileOutputStream(CustomTemplate.TEMPLATES_PATH + "signature.jpg");
        report.setFieldsMetadata(metadata);
        fos.write(signatureByteArr);
        FileImageProvider signature = new FileImageProvider(new File(CustomTemplate.TEMPLATES_PATH,"signature.jpg"));
        context.put("signature", signature);
    }else{
        FileImageProvider noImage = new FileImageProvider(new File(CustomTemplate.TEMPLATES_PATH, "1px.gif"));
        context.put("signature", noImage);
    }

    FileImageProvider logo = new FileImageProvider(new File("TEMPLATES_PATH, logo.gif"));
    context.put("logo", logo);

    OutputStream out = new FileOutputStream(new File(CustomTemplate.TEMPLATES_PATH, "OutPut.docx"));
    report.process(context, out);
    System.out.println("Success");
    } catch (IOException e) {
    System.out.println("IO exception");
    } catch (XDocReportException e) {
        System.out.println("XDocException");
        e.printStackTrace();
    }
}

我得到一个 OutPut.docx,但“事实上”它是一个 odt 文档,Microsoft Office 无法在没有错误详细信息的情况下打开它。 OpenOffice 打开它没有任何问题。

【问题讨论】:

  • 你能告诉我们你的尝试吗?

标签: java docx odt xdocreport


【解决方案1】:

简而言之,XDocReport 不支持 odt->docx 转换器

你使用report.process表示没有转换(docx模板->docx报告,odt模板->odt报告)。在您的示例中,生成的报告是一个 odt(即使您设置了带有 docx 扩展名的文件名),这就是为什么 OpenOffice 可以打开它,尽管 MS Word 无法打开它。

如果您希望将报告转换为其他格式,例如 html、pdf,您必须使用 report.convert,但在您的情况下,您需要 XDocReport 不提供的 docx->odt 转换器。但由于 XDocReport 是模块化的,您可以开发自己的 docx->odt 转换器并将其插入 XDocReport。

【讨论】:

  • 谢谢你,安吉洛。但我不敢相信我是第一个尝试这个的人。
猜你喜欢
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2020-03-25
  • 2016-08-14
  • 2021-06-28
  • 2018-08-13
相关资源
最近更新 更多