Currently I am using ASP.net and working on a web project, which requires dynamically generate Microsoft Word submission document. The generated document must be specific format, and include page number, last print date, company logo, etc.
At first, I was thinking of using COM (Component Object Model). Because MS Word can comfortably work with .Net framework, .Net provide easy runtime callable wrapper. But it requires MS Word to be installed at server side, and it may slow down web server running. I have to give up.
And then I was looking for 3rd party .Net library generate MS Word. It’s not a cheap solution either.
After google, finally I found out that MS Word is compatible with HTML file format. As long as you rename .htm to .doc. MS Word can easily open. What I can do is to create a html template and then filled out the dynamic sections and response back to end user.
Here is the code snap shot:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request["projectID"] != null)
{
//fill dynamic section
this.catlist.DataBind();
Response.Clear();
//add word document header to response
Response.AddHeader("content-disposition", "attachment;filename=Submission.doc");
Response.Charset = "utf-8";
Response.ContentType = "application/ms-word";
//flush out whole page to word document
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this.Page.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
}
}
I will keep posting the problem that I met and the solution in the next few posts.