2

How to create a word document file of both extensions .doc and .docx using ASP.Net with C# (2008) ?

Sreejesh Kumar
  • 2,449
  • 12
  • 51
  • 72

3 Answers3

2

I prefer to avoid the com route and instead generate the doc in the response stream. This has worked really well for me. Hope it helps.

        Response.ContentType = "application/msword";
        Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
        Response.Charset = "UTF-8";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + "mydoc.doc");

        Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>");
        Response.Write("<head>");
        Response.Write("<!--[if gte mso 9]> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> <![endif]-->");
        Response.Write("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"\"text/html; charset=UTF-8\"\">");
        Response.Write("<meta name=ProgId content=Word.Document>");
        Response.Write("<meta name=Generator content=\"Microsoft Word 9\">");
        Response.Write("<meta name=Originator content=\"Microsoft Word 9\">");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write("<div class=Section2>");

        // write some content here

        Response.Write("</body>");
        Response.Write("</html>");
        HttpContext.Current.Response.Flush();
earthling
  • 5,084
  • 9
  • 46
  • 90
  • Thanks. I will check this out. Well In Script Task coding section, How come I cant maintain the files which I had added for Reference? Each time when I go for Edit Script, I have to add the file for Reference for resuming the coding. Is there any other settings for maintaining the files added for reference ? – Sreejesh Kumar May 11 '10 at 06:41
  • and what is the namespace to be included for getting "Response" ? I am not getting System.Web.HttpResponse !!!!!!! – Sreejesh Kumar May 11 '10 at 06:45
  • I don't follow your first comment. Maybe you could explain further and somebody could chime in. As for the response, if you are calling it from your web form you should just be able to use as is. Odd. You might want to try Page.Response or Context.Response or System.Web.HttpContext.Current.Response. – earthling May 11 '10 at 06:51
  • No. I am not getting both Page.Response or Context.Response or System.Web.HttpContext.Current.Response. I am using Script Task editor in SSIS. – Sreejesh Kumar May 11 '10 at 06:55
  • I'm not familiar with Script Task editor. I suggest updating the tags on this post to get some folks familiar with that on here. – earthling May 11 '10 at 07:31
  • I have created the doc file. But when I opened the word document, an alert is shown. the message is "word cannot start the converter mswrd632.wpc". but I can see the contents in the word file except this error message. – Sreejesh Kumar May 11 '10 at 08:13
  • that looks to be a problem caused by a security patch. see http://support.microsoft.com/kb/973904 for fix – earthling May 11 '10 at 09:04
  • Thanks. Now the problem is fixed – Sreejesh Kumar May 11 '10 at 09:51
1

OfficeWriter is a DOC and DOCX API that is optimized for ASP.NET use. You can create both file formats with it:

http://www.officewriter.com

Eisbaer
  • 189
  • 1
  • 6