读取和写入DOCX文件 [英] Read and Write DOCX file

查看:130
本文介绍了读取和写入DOCX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个正在使用的docx文件.一个docx文件包含产品的文本信息(起始序列号,长度,宽度和高度).另一个docx文件包含一个不干胶标签,其中包含图像和第一个文件中的所有文本信息.

I have 2 docx files that I am working with. One docx file contains text information of a product (start serial number, length, width, and height). The other docx file contains a sticker label with an image and all of the text information from the first file.

这是我目前正在做的事情: 我打开第一个docx文件并复制所有文本信息(序列,长度,宽度和高度) 然后,我将每个信息粘贴到包含格式化标签的第二个docx文件中. 如果需要制作多个标签,请复制标签并将序列号加1.

This is what I do currently: I open the first docx file and copy all of the text information (serial, length, width, and height) Then I paste each info into the second docx file that contains the formatted label. If I need to make more than one label, I copy the label and increment the serial number by 1.

为不同的产品制作多个标签需要花费大量时间.我的目标是想出一种更简便的方法来从一个docx提取数据并将其注入到另一个docx.另外,在需要时生成更多标签.

This takes a lot of time to make several labels for different products. My goal is to come up with an easier way to take data from one docx and inject it into the other. Also, generating more labels when needed.

我的第一个想法是提取docx文件以获取其xml内容.然后使用javascript,c ++或任何其他语言读取数据.然后,请用户输入要生成的标签数量,处理xml并将其重新打包为docx文件.

My first thought was to extract the docx file to get it's xml contents. Then read the data using javascript, c++, or any other language. Then Ask user to input number of labels to generate, manipulate the xml, and repack it as a docx file.

然后,我考虑过尝试使用Windows Office的邮件合并"功能,但我从未做过此事.

Then I thought about trying to use the windows office "mail merge" feature, but I have never done this before.

我想知道是否有人对从一个docx文件导入数据并生成标签到另一个docx的简单解决方案有任何建议.

I would like to know if anyone has any suggestions for an easy solution to import data from one docx file and generating labels into another.

我愿意接受任何建议.

I am open for any suggestion.

我也不是一个专业的程序员.我是计算机工程专业的本科生,在c,c ++,java,javascript,python,MIPS汇编和php方面具有一定的经验.

Also, I am not a professional programmer. I am an undergraduate computer engineering student with some experience in c, c++, java, javascript, python, MIPS assembly, and php.

推荐答案

  1. 我知道的唯一开源(可能更容易获得)的解决方案是:

http://poi.apache.org/

http://poi.apache.org/document/quick-guide -xwpf.html

这是一个不错的选择,它涉及到速度,它是免费软件.

This is a good bet when it comes to speed and it is free software.

但是,如果打开文件,请对其进行更改并再次保存-结果可能会不稳定:格式可能会略有偏离.至少在我与pptx同行的测试中.

But if you open a file, alter it and save it again - the result can be flaky: The formatting can be slightly off. At least in my tests with the pptx counterpart.

我认为,当您与用户进行交互(网页?)以创建文档时,可以在该库周围构建一个小的HTTP Api.

I reckon when you have user interaction (web page?) in order to create the document, you can build a small HTTP Api around the library.

还有: http://www.docx4java.org/trac/docx4j -我还没有测试过.

There is also: http://www.docx4java.org/trac/docx4j - which I have not tested yet.

  1. 您还可以采用C#/Redmond方式:在格式化的准确性方面,Interop(上述问题的第一个答案中的第二个示例)方法可提供最佳结果.基本上,当您使用Interop打开文件时,更改和保存文件后,它们的外观将相同.但是,在与用户交互时,您不能使用它-因为它会启动一个单独的MS Office进程-并且我不会依靠自己的用户体验来依靠它.但是,如果您想在单个用户会话中批量生成这些文件,它将产生很好的效果.

    The Interop (2nd Example in the first answer of the question above) way gives the best result when it comes to the accuracy of the formatting. Basically when you open a file with Interop - it will look the same when you alter and save it. But you cannot use this when interacting with a user - because it starts a separate MS Office process - and I would not count on this from my own user experience. But if you want to generate these files as a batch in a single user session - it will deliver a good result.

    我无法评论上述SO问题中描述的"OpenXML SDK"库.

    I cannot comment on the "OpenXML SDK" library described in the above SO question.

    这篇关于读取和写入DOCX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆