java - 将 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 模板中相同的外观和感觉?
解决方案
推荐阅读
- ios - How to pass variable data in view controllers embedded in a navigation stack?
- excel - 将所有 IF 公式 (TRUE) 转换为除包含 IF 公式 (FALSE) 的行之外的值
- python - Python AttributeError:“NoneType”对象没有使用 BeautifulSoup 的“查找全部”属性
- sql - PostgreSQL 可以在 ORDER BY 中使用 FOR LOOP 吗?
- markdown - R Markdown / Bookdown:如何自定义数字书章节?
- .net - 如何在 Azure Pipeline 期间创建包含 build.* 变量的文件
- sql - 在 Postgresql 中将日期转换为 YYYY-MM-DDThh:mm:ss.sssZ 格式
- javascript - 如何遍历包含数组、对象和字符串的对象
- javascript - 在没有互联网连接的情况下玩用 HTML5、CSS 和 JavaScript 开发的游戏
- linux - 如何创建一个程序,将操作系统更改的更新日志实时提取到镜像文件