问题
I would like to generate a word document from my ASP.NET application. Currently we show an "Agenda List" which is the agenda information and all items/subjects. This page needs to have the ability to open in word. The agenda page is not static, it's a dynamic list of agenda items pulled from SQL Server.
Any suggestions on the best solution? I'm looking for a quick solution and OpenXML seems to be a bit too time consuming. I'm open to purchasing third party tools.
Thanks!
回答1:
Use the Office OpenXML SDK. For later versions of Office (2007 and above), this is the standard. There are other ways as mentioned in other answers, but OpenXML will give you the greatest level of control over the output.
Here is some documentation to get you started: http://msdn.microsoft.com/en-us/office/bb265236.aspx
回答2:
You can either
Let the page generate the document yourself (like RTF format)
Save the Word template as XML and add your own placeholder where you fill in the content (I know you said this is time consuming which is true if you do a lot of updates to the template)
Used 3rd party tool like Aspose (http://www.aspose.com), it worked very well for me as it does save a lot of time to work with the document through objects in C#.
回答3:
There are three methods that I have used previously:
- Use the openXML library to programmatively create a document from scratch.
- Use Office automation to mail merge data with a Word template. You would create your template first and then use a DataSet to populate the data fields.
- If your database is SQL Server 2005 or later you can use SSRS to generate documents in a number of formats, including Word, PDF, Excel, and CSV.
回答4:
You don't need any 3rd party controls to create a Word document. From 2007 and onward Word can read html as a word document. You simply save any web page with the ".doc" extension and Word will sort it out.
Simply create your web page with whatever formatting you want then save it with a .doc extension.
I used HttpWebRequest
to call the Url (with parmaters) to my page then used WebResponse
and Stream
to get my page into a buffer, then StreamReader
and StreamWriter
to save it to an actual document. I've then got my own custom function to download the file.
If anyone wants my code let me know
回答5:
You can also try our 3rd party GemBox.Document library.
Here is a sample C# code how to generate template Word (DOCX) document programmatically and use mail merge to import data into it:
var document = new DocumentModel();
document.Sections.Add(
new Section(document,
new Paragraph(document, "Agenda List:"),
new Paragraph(document,
new Field(document, FieldType.MergeField, "RangeStart:Agendas"),
new Field(document, FieldType.MergeField, "Subject") { CharacterFormat = { Bold = true } },
new SpecialCharacter(document, SpecialCharacterType.LineBreak),
new SpecialCharacter(document, SpecialCharacterType.Tab),
new Field(document, FieldType.MergeField, "Description")),
new Paragraph(document,
new Field(document, FieldType.MergeField, "RangeEnd:Agendas"))));
var agendas = new object[]
{
new { Subject = "First agenda subject", Description = "First agenda description." },
new { Subject = "Second agenda subject", Description = "Second agenda description." }
};
document.MailMerge.Execute(agendas, "Agendas");
document.Save("Agendas.docx");
You can also easily save Word document to ASP.NET response stream.
回答6:
I can suggest you to try our Elerium Word .Net Writer component to generate Word document from ASP.NET page. There is a demo with sourse code that shows how to create new Word document using dinamic datatable:
http://eleriumsoft.com/Word_NET/WordWriter/Demo.aspx
Disclaimer: I'm a developer at Elerium Software.
来源:https://stackoverflow.com/questions/15989686/what-is-best-solution-to-generate-word-document-from-asp-net