ASP.net dynamically generate MS Word document (2)

MS Word treat this generated “Word” document differently

When clicking download generated document, IE will pop up “File Download” dialog as expected. However when opening the downloaded document, MS Word will be in “Web Layout View” instead of “Print Layout View”.

Let MS Word “thinks” this is true word document and open in “Print Layout View” with zoom “90%

Replace original Visual Studio automatically generated .aspx header with the following code










OK! Now when MS Word opens this html style Word document, it more likely opens a real Word document.

Another issue comes up that MS Word is so smart, and it automatically breaks the page.

If you want to break the page on a certain position, you can insert the following code into your html style Word document.

Previous page

Next page

ASP.net dynamically generate MS Word document (1)

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.

Android on my SuperPad

Recently, I bought a SuperPad from eBay for my kid learning, which is running Google Android 2.1.

It’s pretty cool because I can develop my own educational application for my kid.  As I haven’t been playing Java for a long time, I have to refresh my memory, and it will slow down the process a little bit.

Download Eclipse IDE, Android simulator, setup development environment… Finally I have my HelloWorld running!!!!!! It’s so excited to see these two words appearing in my new SuperPad screen.

After few days’ trial, I have two applications ready for my kid Kids Draw and Spot the Difference.