在ASP.NET中读取Word文档并替换文本(Office 2003/2007)
我使用的是VS 2005,asp.net 2.0。
我需要在asp.net中读取一个word文档(.doc),进行一些替换,然后生成另一个word文档。
到目前为止还可以。我是在安装了 Office 2003 的计算机上完成的,因此在 VS 2005 中我添加了对“Microsoft Word 11.0 对象库”的引用。一切都很顺利。打开/保存word文档的代码:http://www.dnzone.com/go?1387
问题1:我需要在安装了Office 2007 的计算机中打开同一个项目并使其运行。当我这样做时,它无法识别引用。我只能添加对“Microsoft Word 12.0 对象库”的引用。我怎样才能以尽可能少的努力让它在两台机器上顺利工作?有办法这样做吗?
问题 2:当我将该应用程序部署到 Web 服务器时,它能正常工作吗?我是否必须在服务器中注册某些 dll 或组件或执行其他操作才能使此 Office 集成正常工作?
I'm using VS 2005, asp.net 2.0.
I need to read a word document (.doc) in asp.net, make some replaces and then generate another word document.
So far OK. I've done it in a machine that have the Office 2003 installed, so in VS 2005 I've added a reference to the "Microsoft Word 11.0 Object Library". Everything worked perfectly. Code to open/save the word document: http://www.dnzone.com/go?1387
Question 1: I need to open the same project in a machine that has Office 2007 installed and make it run. When I do it, it doesn't recognizes the references.. I only have the possibility to add reference to the "Microsoft Word 12.0 Object Library". How can I make it work smoothly in both machines with the less effort possible? Is there a way to do so?
Question 2: Will this application work fine when I deploy it to a web server? Do I have to register some dll or component in the server or do anything else in order to this office integration to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您永远不应该在服务器代码中使用 Word API。它不受支持并且通常无法可靠地工作。
You should never use the Word API from server code. It is not supported and will generally not work reliably.
使用 Word XML(在 Word 2002 及更高版本中可用)来创建模板,可以节省大量工作。检查生成的 XML 代码以了解必须在何处进行替换。与Office 2002、2003、2007兼容,无需服务器组件。
在 99% 的情况下,Word XML 看起来与相应的 DOC 文件相同,而且更易于维护。加上对所有 Word 功能的支持,这就是您可能比 RTF 更喜欢它的原因。
Save yourself alot of work and use the Word XML (available in Word 2002 and up) to create your templates. Check the XML code generated to see where you have to make your replacements. Compatible with Office 2002, 2003, 2007, and no server component needed.
Word XML looks the same as the corresponding DOC file in 99% of the cases, yet better maintainable. Plus support for all Word features, which is why you might favor it over RTF.
我不久前遇到了这个问题 - 我们最终将其保存为 RTF 文件,该文件仍然是格式化的,然后使用 FileStream 和 .Replace(someStringValue) 基于键值,例如 #DateForEventStart 所以你有这样的东西
然后你可以将其流式传输到内存流或您需要的任何内容。我不知道这对于你正在做的事情是否可能,但这是一个想法。
I ran into this problem a while back -- we ended up saving it as an RTF file which still comes out formatted then using FileStream and .Replace(someStringValue) based on key values, such as #DateForEventStart so you had something like this
then you can stream that out to a memstream or whatever you need. I don't know if that's possible for what you're doing, but it's a thought.
您可能想看看 Aspose 产品。它们被设计为在服务器环境中工作(但不是免费的)
You might want to take a look at the Aspose products. They are designed to work in a server environment (but are not free)
我们已经找到了问题所在,这与文件创建有关。我在现有文件上使用
FileStream
和FileMode.Open
。我更改为FileMode.Create
来创建一个全新的文件,然后..瞧! :)非常感谢大家的回复,特别是 Jan Jongboom。
We've found out the problem, it was something related with the file creation. I was using
FileStream
withFileMode.Open
on an already existing file. I changed toFileMode.Create
to create a brand new file and.. voillá! :)Thank you very much for everybody for your replies, specially to Jan Jongboom.