首页 > 解决方案 > 将 HTML 页面模板与用户数据一起存储在 DOCX 文件中后的外观

问题描述

我开发了一个 HTML 和 CSS 网页模板,如下所示:

这里 一旦用户在此网页上输入数据并单击发送按钮。我的 java 应用程序将触发一封电子邮件以及 docx 文件作为附件,其中该 docx 文件应包含与上图中完全相同的网页模板以及用户输入的数据。

我通过使用库 JSOUP jar 实现了这一点:

File i1 = new File("C:/Users/RPUNEET/Desktop/Puneeth/java/DaimlerLoDATemplate_Demo/WebContent/Template_demo_new.html");
Document doc = Jsoup.parse(i1, "UTF-8");

// For the project name
Element eleprojectname= doc.getElementById("projectname");
eleprojectname.attr("value",iv.getProjectname());

// For the name field
Element elename = doc.getElementById("Name1");
elename.attr("value", iv.getName());

// For the e-mail field
Element eleEmail = doc.getElementById("email");
eleEmail.attr("value", iv.getEmail());

我正在将我从 servlet 请求中获取的对象数据设置为引用的 html-file: 中特定元素 ID 的值Template_demo_new.html

设置所有值后,此“文档”将被转换为字符串以存储在特定文件中:

String html = doc.html();

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, false);
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(html.getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
//HeaderPart hp=new HeaderPart

// the bit in the document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();

ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);

// content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
wordMLPackage.save(new java.io.File("C:/Users/RPUNEET/Desktop/RajPuneeth/LoDA_BRD_Template_v1.0.docx"));

该代码将 Web 模板与用户数据一起存储。但是文档 docx 文件的外观与 html 网页中未应用大小和背景颜色的网页模板并不完全相同,如下所示:

这里

请让我知道我必须做什么才能在电子邮件版本中获得与 html 模板中相同的外观和感觉?

标签: javahtmlcssjsoup

解决方案


推荐阅读