What is the best solution for creating a Word document from ASP.NET?

I would like to generate a Word document from my ASP.NET application. We are currently displaying an โ€œAgenda Listโ€, which is the agenda and all items / items. This page should be able to open with a word. The agenda page is not static; it is a dynamic list of agenda items retrieved from SQL Server.

Any suggestions on the best solution? I am looking for a quick solution, and OpenXML seems too laborious. I am open to buying third-party tools.

Thanks!

+4
source share
6 answers

Use the Office OpenXML SDK. For later versions of Office (2007 and later), this is the standard. There are other ways, as mentioned in other answers, but OpenXML will give you the maximum level of control over the output.

Here are some documents to help you get started: http://msdn.microsoft.com/en-us/office/bb265236.aspx

+5
source

You can either

  • Let the page generate the document itself (for example, RTF format)

  • Save the Word template as XML and add your own placeholder where you fill in the content (I know you said that it takes a lot of time, which is true if you make a lot of updates for the template)

  • Used by a third-party tool such as Aspose ( http://www.aspose.com ), it worked very well for me, since it saves a lot of time working with a document through objects in C #.

+4
source

There are three methods that I used earlier:

  • Use the openXML library to programmatically create a document from scratch.
  • Use Office automation to send merge data using a Word template. You must first create your template, and then use the DataSet to populate the data fields.
  • If your database is SQL Server 2005 or later, you can use SSRS to create documents in several formats, including Word, PDF, Excel, and CSV.
+2
source

You can also try our third-party library GemBox.Document .

Here is an example of C # code with which you can create a Word Word document (DOCX) and use mail merge . 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 a Word report in ASP.NET .

+1
source

You do not need any third-party controls to create a Word document. From 2007 onwards, Word can read html as a Word document. You simply save any web page with the extension .doc , and Word will sort it.

Just create your web page using any formatting, and then save it with the extension .doc.

I used HttpWebRequest to call Url (with parmaters) to my page, then used WebResponse and Stream to get my page to the clipboard, then StreamReader and StreamWriter to save it to the actual document. Then I got my own custom function to upload the file.

If someone wants my code to let me know

+1
source

I can suggest you try Elerium Word.Net Writer to create a Word document from an ASP.NET page. There is a demo code with a source code that shows how to create a new Word document using a dynamic datatable:

http://eleriumsoft.com/Word_NET/WordWriter/Demo.aspx

Disclaimer: I am a developer at Elerium Software.

0
source

All Articles