开发者

Read word document in ASP.NET and replace text (Office 2003/2007)

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 开发者_Python百科I have to register some dll or component in the server or do anything else in order to this office integration to work?


You should never use the Word API from server code. It is not supported and will generally not work reliably.


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.


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

    public void something()
    {
        StreamReader stream = new FileStream(filePath);
        string template = stream.ReadToEnd();
        template.Replace(templateKeyValue, objectValue);
    }

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.


You might want to take a look at the Aspose products. They are designed to work in a server environment (but are not free)


We've found out the problem, it was something related with the file creation. I was using FileStream with FileMode.Open on an already existing file. I changed to FileMode.Create to create a brand new file and.. voillá! :)

Thank you very much for everybody for your replies, specially to Jan Jongboom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜