在Ruby中创建Microsoft Word(.docx)文档

编程入门 行业动态 更新时间:2024-10-17 05:34:20
本文介绍了在Ruby中创建Microsoft Word(.docx)文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有在Ruby应用程序中创建Word文档(.docx)的简便方法?实际上,就我而言,这是一个从Linux服务器提供服务的Rails应用程序.

Is there an easy way to create Word documents (.docx) in a Ruby application? Actually, in my case it's a Rails application served from a Linux server.

类似于大虾的宝石,但 DOCX 而不是PDF很棒!

A gem similar to Prawn but for DOCX instead of PDF would be great!

推荐答案

如前所述,似乎没有任何库可以在Ruby中操作Open XML文档,但是 OpenXML Developer 具有有关Open XML文档格式的完整文档.

As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developer has complete documentation on the format of Open XML documents.

如果您要发送的是为每个用户定制的标准文档(如套用信函)的副本,则应该很简单,因为DOCX是一个ZIP文件,其中包含目录层次结构中的各个部分.有一个DOCX模板",其中包含要发送给all用户的所有部分和树结构(不包含实际内容),然后只需创建新的(或修改现有的)包含所需的用户特定内容的部分并将其注入ZIP(DOCX文件)中,然后再发送给用户.

If what you want is to send a copy of a standard document (like a form letter) customized for each user, it should be fairly simple given that a DOCX is a ZIP file that contains various parts in a directory hierarchy. Have a DOCX "template" that contains all the parts and tree structure that you want to send to all users (with no real content), then simply create new (or modify existing) pieces that contain the user-specific content you want and inject it into the ZIP (DOCX file) before sending it to the user.

例如:您可能具有包含尊敬的[USER-PLACEHOLDER]:的document-template.xml.当用户请求文档时,用用户名替换[USER-PLACEHOLDER],然后将生成的document.xml添加到your-template.docx ZIP文件(该文件将包含Word文档中所需的所有图像和其他部分)并发送生成的文档发送给用户.

For example: You could have document-template.xml that contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER] with the user's name, then add the resulting document.xml to the your-template.docx ZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.

请注意,如果将.docx文件重命名为.zip,则探究内部零件的结构和格式很简单.您可以使用任何ZIP操纵工具或通过编程方式非常轻松地删除或替换图像或其他零件.

Note that if you rename a .docx file to .zip it is trivial to explore the structure and format of the parts inside. You can remove or replace images or other parts very easily with any ZIP manipulation tools or programmatically with code.

如果不使用API​​来简化工作,则很难从原始XML生成具有完全自定义内容的全新Word文档.如果确实需要这样做,则可以考虑安装 Mono ,然后使用VB.NET,C#或 IronRuby 使用打开XML格式SDK 1.0 .由于您将只使用 Microsoft.Office. DocumentFormat.OpenXml.Packaging命名空间来操作Open XML文档,它应该可以在Mono中正常工作,Mono似乎可以支持SDK所需的所有功能.

Generating a brand new Word document with completely custom content from raw XML would be very difficult without access to an API to make the job easier. If you really need to do that, you might consider installing Mono, then use VB.NET, C# or IronRuby to create your Open XML documents using the Open XML Format SDK 1.0. Since you would just be using the Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace to manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.

更多推荐

在Ruby中创建Microsoft Word(.docx)文档

本文发布于:2023-11-16 14:36:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   Microsoft   Ruby   docx   Word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!