Creating Word document from .net application
I have a .net web application written in C# that is only used on an Intranet.
开发者_如何学GoI have a page on which I need to create a Word document, modify some of the text dynamically, save the Word document and send it as an email attachment.
I tried doing it using Word Automation and, although it worked on my development machine, it doesn't work on a server when other users try to use it.
So ... how to create a Word document on the fly, save it and use it as an email attachment? I have searched and people say 'use open xml' ... but (apart from the fact I have no idea how to do it) would this allow me to create and save a Word document that will be able to be opened by people using old copies of Word (say as far back as Word 97).
And I keep reading the words 'Sharepoint Server' but, again, have no idea what/how this might fit in with a .net web application.
Can anyone point me in the right direction please?
OpenXML is the right way to go right now. Creating a Word document from scratch is actually fairly easy and there are many tutorials.
- Check out this Video tutorial: How Do I: Create a Word Document Using the SDK for Open XML Formats?
- Then there is another video including example code: Building Word 2007 Documents Using Office Open XML Formats
- Good step-by-step tutorial: Creating an Open XML Document in .NET (and another one: Create Word document using the OpenXML 2.0 SDK)
Just because you tagged your question with Sharepoint I am not sure whether you are using Sharepoint - if so there is even some example code about Generating Documents from SharePoint Using Open XML Content Controls directly from Microsoft.
The most likely reason the code worked on your development machine but not on the server is because your development machine probably has the Office/Word Primary Interop Assemblies installed on it but you do not say whether these are installed on the server or not. I am assuming they are not.
If your users are on Microsoft Office XP (2002) or Office 2003 AND have the Microsoft Office Compatibility Pack installed, then your best option is to use the Open XML SDK to generate a .docx file on the fly, which your users will be able to open by having the compatibility pack installed. Once your users have opened the document, they can then save it in the normal .doc format.
If your user base includes users of Office 2000 or earlier, you are out of luck as there is no compatibility pack for these versions of Office.
You will not be able to generate the .doc format on the web server without having the Office assemblies installed and, as others have pointed out, this is strongly discouraged by Microsoft.
Have you considered using RTF? RTF files are just text, so you could use normal .Net file I/O to read in the file, change the bit you are interested in, and then write it out again. RTFs can be read by versions of Word from 97 onwards (and probably older versions too). One drawback is if you need to do any complicated etc changes to the document, which may be difficult if you are just processing the RTF as text.
Others have commented on why the word automation probably isn't working and why its a no no anyway.
OpenXML is good but only supports the newer file formats.
If you want to support the older .doc file formats (Word97 etc) then you're going to have to look at another component and probably a commercial one such as (no connection) Aspose Words
Don't ever use Office Automation from a service. Office Automation was designed to automate the Office desktop applications. It assumes the presence of a UI.
It also assumes single threading. A server (especially a web server of any kind) will be operating on multiple threads, and the Office Automation code just doesn't handle that at all.
Additionally, you must have a valid Office license for every end user who uses the code that calls Office Automation.
精彩评论