是否可以向Word文档中添加一些数据?

发布于 2024-09-06 18:15:09 字数 151 浏览 4 评论 0原文

是否可以添加(通过 MS Word 对象模型?或通过编辑文件)一些自定义数据(可能是另一个文件?)

在 MS Word 中打开、更改和保存文档后会保留该数据吗?

(在某些报告导出过程中,我需要将一些跟踪数据放入docx中,导入过程中将再次使用自定义数据)

Is it possible to add (thru the MS Word object model? or by editing file) some custom data (another file maybe?)

Will be that data preserved after opening, changing and saving document in MS Word ?

(I need to put some tracking data in docx during some report export, custom data will be used again during import)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2024-09-13 18:15:09

是的。这相当简单,但据我所知,没有记录(根本没有或很好)。假设您想要将 .ini 文件添加到您的 Office 文档(.docx、.xslx、.pptx)中,以便与您的文档一起“随身携带”(即您发送 .docx 通过电子邮件发送给某人,并希望您添加的 yourfile.iniyourdoc.docx< /strong> 即使打开 DOCX 的人进行了一些更改,然后保存并将其发送回给您)。

  1. 将文档的扩展名重命名为
    .zip。解压它。
  2. 在根文件夹中,找到
    [Content_Types].xml 文件并打开它。
  3. 就在 根目录下
    元素,添加 (注意 -
    对于另一个文件,只需确保
    扩展名与您的文件匹配
    将添加和属性
    Extension 与此匹配。对于
    ContentType 属性,可以是
    任何东西)。
  4. 现在将您的文件名.ini文件添加到
    包裹。您可以将其添加到任何地方 -
    根,/word 路径,一个新的
    文件夹,例如 /word/MyIniFiles
  5. 接下来,建立之间的关系
    您的文件和文档。如果你
    不,该文件不会随
    文档。导航至
    \word_rels\document.xml.rels 文件,
    并在 下添加
    类似于 其中
    属性 Id 是您的 Type 中的唯一名称(最好坚持使用 rIDX 或类似名称),Type > 反映一个 URI(
    几乎任何东西)和 Target
    您的文件的相对路径
    额外。我将 abcpy.ini 添加到
    位于同一文件夹中的 /word 文件夹
    root 作为 document.xml,所以我的
    相对路径只是文件名。
    我是否将其添加到以下文件夹中
    /word 名为 myIniFiles
    目标将是
    myIniFiles/abcpy.ini
  6. 保存所有内容。从根目录重新压缩文件,并将文档重新命名为您的原始名称和
    扩展回.docx。复制/粘贴原始文件。完毕。

如果您有 Open XML 包编辑器(VSTO Power Tools 适用于 VS2008 或最近发布的 独立 for VS 2010),您可以使用 VS 为您完成一些此类工作,但本质上是相同的。尽管有不同的术语,例如 Target 等于 Name 等。

请注意,这不是某种 hack。 Microsoft 自己在 Office 2010 中使用同样的技术来处理使用新的“艺术效果”修改的图像 - 原始图像文件被移植到 HDPhoto 类型和修改后的图像保存为 png 或 jpg(这些是在客户端中显示的图像)。 hdphoto(带有 .wdp 扩展名)随文档一起传输,但从未实际用于在 Word、PowerPoint 等中显示。

Yes. It's rather straightforward, but as far as I know, not documented (at all or very well). Say you want to add an .ini file to your your Office doc (.docx, .xslx, .pptx) to be "carried around" with your document (i.e. you send your .docx to someone over email and want the yourfile.ini you've added to stay with the yourdoc.docx even if the person opening the DOCX makes some changes and then saves it and sends it back to you).

  1. Rename your document's extention to
    .zip. Unzip it.
  2. In the root folder, find the
    [Content_Types].xml file and open it.
  3. Right under the <Type/> root
    element, add <Default
    Extension="ini"
    ContentType="custom/ini"/>
    (note -
    for another file, just make sure the
    extention matches the file you're
    going to add and the attribute
    Extention matches that. For the
    ContentType attribute, that can be
    anything at all).
  4. Now add your filename.ini file to the
    package. You can add it anywhere -
    the root, the /word path, a new
    folder like /word/MyIniFiles.
  5. Next, set up a relationship between
    your file and the document. If you
    don't, the file won't carry with the
    document. Navigate to the
    \word_rels\document.xml.rels file,
    and add under <Relationships/>
    something like <Relationship
    Id="rd1"
    Type="http://schemas.microsoft.com/office/2007/relationships/ini"
    Target="abcpy.ini" />
    where the
    attribute Id is a unique name in your Type (best to stick with rIDX or something like that), Type reflects a URI (of
    just about anything) and Target is
    the relative path of the file you
    added. I added abcpy.ini to the
    /word folder which is in the same
    root as document.xml, so my
    relative path is just the file name.
    Had I added it to a folder under
    /word that was called myIniFiles,
    the Target would be
    myIniFiles/abcpy.ini
  6. Save everything. Rezip your file from the root and name the document back to your original name and the
    extention back to .docx. Copy/paste over the original file. Done.

If you have the Open XML Package Editor (part of VSTO Power Tools for VS2008 or recently released stand-alone for VS 2010), you can use VS to do some of this work for you, but it's essentially the same. Different terminology though like Target equals Name, etc.

Note that this isn't some kind of hack. Microsoft themselves use this very same technique in Office 2010 for images modified with the new "Artistic Effects" - the original image file is ported to an HDPhoto type and modified images are saved as png or jpg (and those are the ones that are displayed in the client). The hdphoto (with a .wdp extension) travels with the document, but is never actually used in display in Word, PowerPoint, etc.

萌面超妹 2024-09-13 18:15:09

是的,这是可能的。 Word 具有允许您将任意 XML 文档嵌入到 Word 文档中的功能。此功能称为“自定义 XML”。

编辑文档时会保留嵌入文档,并且还可以使用 VBA 以编程方式或使用 Word 2007 中引入的内容控件从文档内部访问嵌入文档。

请注意,Word 要求嵌入内容为 XML,但事实并非如此。不限制您只能使用纯 XML 数据,因为您可以使用 base64 编码将二进制内容嵌入到 XML 中。

MSDN 有一个 C# 示例,向您展示如何将自定义 XML 部分添加到 .docx 文件:

如何:在不启动 Microsoft 的情况下将自定义 XML 部分添加到文档中办公室

Yes, this is possible. Word has a feature that allows you to embed an arbitrary XML document into your Word document. This feature is called Custom XML.

The embedded document is retained when the document is edited and can also be accessed from within the document, either programmatically using VBA, or using the Content Controls introduced with Word 2007.

Note that Word requires the embedded content to be XML, but that doesn't restrict you to pure XML data as you can use base64 encoding to embed binary content into XML.

MSDN has a sample in C# that shows you how to add a Custom XML part to a .docx file:

How to: Add Custom XML Parts to Documents Without Starting Microsoft Office

神仙妹妹 2024-09-13 18:15:09

不,如果您向该 zip 文件添加任何内容,Word 将开始抱怨文件损坏。
但不要重新发明轮子。 Word 文档可以具有多个不同的文档属性。就用那个吧。
http:// office.microsoft.com/en-gb/word-help/add-property-information-to-a-document-HA010163766.aspx

No, if you add anything to this zip file Word will start complaining about file corruption.
But don't reinvent the wheel. Word documents can have multiple different document properties. Just use that.
http://office.microsoft.com/en-gb/word-help/add-property-information-to-a-document-HA010163766.aspx

无远思近则忧 2024-09-13 18:15:09

为了完整起见,您还可以使用字段DocVariable。这实际上是一个书签,与文档属性的方式相同,但更加开放,至少在命名方面如此。这些最适合相对较小的信息。如果您想添加完整的文件,其他方法可能会更好。

请参阅:

什么是 Word 中的 DOCVARIABLE

For the sake of being complete, you can also use the field DocVariable. This is actually a bookmark in the same way the document properties but a more open ended, at least for naming. These are best for relatively small bits of information. If you want to add a complete file, the other methods might be better.

See:

What is a DOCVARIABLE in word

岁吢 2024-09-13 18:15:09

感谢您的所有回复。
我使用自定义属性将一些 UserID 数据添加到生成的文档中,这足以“跟踪”文档并能够在以后导入它。

http://msdn.microsoft.com/en-us/library/dhxe2d75.aspx

Thank you for all responses.
I used custom properties to add some UserID data to generated documents, it's enough to 'trace' document and be able to import it later.

http://msdn.microsoft.com/en-us/library/dhxe2d75.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文