tags:

views:

356

answers:

1

I am trying to export the web page content into Word. In word 2007 the datas are displaying in correct format. But in case of word 2003 the display format is not correct. What is the reason for this? What should I have to do to rectify this?

I am displaying a textbox and some check box controls which is in the form of Matrix. Actually in the web page these controls are displayed using table property. In word 2003 the table property is reflecting in the word page. But in word 2007 the table property is removed and providing the expected answer.

A: 
  • This is my .CS code

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.IO;

using System.Text.RegularExpressions;

public partial class testword : System.Web.UI.Page

{

#region "Var Delaration"

public string gh;

public string strResponsePageContent = string.Empty;

public string strIndividualResponsePagesLocation = string.Empty;

StreamReader srDefaultIndiResponsePage = null;

#endregion

protected void Page_Load(object sender, EventArgs e)

{

}

protected void GenerateMsWordDoc(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)
{
    //strIndividualResponsePagesLocation = HttpRuntime.AppDomainAppPath + "Survey/IndividualResponseWordCss.txt";
    //srDefaultIndiResponsePage = new StreamReader(strIndividualResponsePagesLocation);
    //strResponsePageContent = srDefaultIndiResponsePage.ReadToEnd();  
    //System.Web.HttpResponse oResponse = System.Web.HttpContext.Current.Response;

    Response.Clear();
    Response.Buffer = true;
    Response.ContentEncoding = System.Text.Encoding.UTF7;
    System.Text.StringBuilder SB = new System.Text.StringBuilder();
    System.IO.StringWriter SW = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);
    tbl.RenderControl(htmlTW);
    string strBody = "<html>" +
        "<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
        "</body>" +
        "</html>";
    //string strBody = "<html>" + "<body>" + strResponsePageContent.ToString() + htmlTW.InnerWriter.ToString() + "</body>" +
    //   "</html>";      
    string fileName = "MsWordSample.doc";
    // You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
    Response.AppendHeader("Content-Type", "application/msword");
    Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
    Response.ContentEncoding = System.Text.Encoding.UTF7;

    string fileName1 = "C://Temp/Excel" + DateTime.Now.Millisecond.ToString();
    BinaryWriter writer = new BinaryWriter(File.Open(fileName1, FileMode.Create));
    writer.Write(strBody);
    writer.Close();
    FileStream fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read);
    byte[] renderedBytes;
    // Create a byte array of file stream length 
    renderedBytes = new byte[fs.Length];
    //Read block of bytes from stream into the byte array 
    fs.Read(renderedBytes, 0, System.Convert.ToInt32(fs.Length));
    //Close the File Stream 
    fs.Close();
    FileInfo TheFile = new FileInfo(fileName1);
    if (TheFile.Exists)
    {
        File.Delete(fileName1);
    }
    Response.BinaryWrite(renderedBytes);
    Response.Flush();
    Response.End();




}

}

  • This is my ASPx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testword.aspx.cs" Inherits="testword" %>

<title>Untitled Page</title>

<%-- <%Response.Write(gh);%>--%>

<form id="form1" runat="server">

    <div id="divAji" runat="server">

        <table border="0" id="tbl" runat="server">

            <tr id="fvIndividualSurveyResponse_trQuestionBindings">

                <td colspan="6">

                    <table id="fvIndividualSurveyResponse_tbQuestions" cellpadding="0" cellspacing="0"
                        style="width: 100%" border="0">
                        <tr>
                            <td id="fvIndividualSurveyResponse_tdQuestions" style="width: 100%">
                                <table id="tblCtrlContent" width="100%" cellpadding="0" cellspacing="0" border="0">
                                    <tr>
                                        <td colspan="6" style="width: 100%">
                                            <table id="Table1" width="100%" cellpadding="0" cellspacing="0" border="0">
                                                <tr>
                                                    <%--<td style="width: 1%">
                                                       <%-- <span>--%>
                                                       <%-- <span id="spnWhatisyourfavouritecolor?">
                                                        *--%>
                                                      <%--  </span>--%>
                                                  <%--      </span>
                                                    </td>--%>
                                                    <td style="width: 98%">
                                                        <span>1. What is your favourite color?</span></td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 100%">
                                            <span>NA</span></td>
                                    </tr>
                                </table>
                                <table id="Table2" width="100%" cellpadding="0" cellspacing="0" border="0">
                                    <tr>
                                        <td colspan="5" style="width: 100%">
                                            <table id="Table3" width="100%" border="0">
                                                <tr>
                                                    <%--<td style="width: 1%">
                                                        <span ></span>
                                                    </td>--%>
                                                    <td style="width: 98%">
                                                        <span>2. List the top three school ?</span></td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 40%">
                                            <span>one </span>
                                        </td>
                                        <td style="width: 60%">
                                            <span>National</span></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 40%">
                                            <span>two</span></td>
                                        <td style="width: 60%">
                                            <span>Devs</span></td>
                                    </tr>
                                    <tr>
                                        <td style="width: 40%">
                                            <span>three</span></td>
                                        <td style="width: 60%">
                                            <span>PS</span></td>
                                    </tr>
                                </table>
                                <table id="Table6" width="100%" cellpadding="0" cellspacing="0" border="0">
                                    <tr>
                                        <td colspan="6" style="width: 100%">
                                            <table id="Table7" width="100%" cellpadding="0" cellspacing="0" border="0">
                                                <tr>
                                                    <%-- <td style="width: 1%">
                                                <span ></span>--%>
                                                    <%--   </td>--%>
                                                    <td style="width: 98%">
                                                        <span>4. My family likes the following type(s) of chicken?</span></td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr id="trCheckBoxRadios_1000000411_55010">
                                        <td colspan="6" style="width: 100%">
                                            <table width="100%" cellpadding="0" cellspacing="0" border="0">
                                                <thead>
                                                    <tr>
                                                        <th width="12%">
                                                            &nbsp;</th>
                                                        <th scope="col" width="12%">
                                                            Crispy</th>
                                                        <th scope="col" width="12%">
                                                            Fried</th>
                                                        <th scope="col" width="12%">
                                                            Thandoori</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr id="htmlChkBox_1000000411_55010_56010">
                                                        <th scope="row">
                                                            Ginger</th>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_56010" type="checkbox" id="htmlChkBox_1000000411_55010_56010_0"
                                                                value="Crispy|6058" checked="checked" /></td>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_56010" type="checkbox" id="htmlChkBox_1000000411_55010_56010_1"
                                                                value="Fried|6059" checked="checked" /></td>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_56010" type="checkbox" id="htmlChkBox_1000000411_55010_56010_2"
                                                                value="Thandoori|6060" /></td>
                                                    </tr>
                                                    <tr id="htmlChkBox_1000000411_55010_57010">
                                                        <th scope="row">
                                                            Pepper</th>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_57010" type="checkbox" id="htmlChkBox_1000000411_55010_57010_0"
                                                                value="Crispy|6058" /></td>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_57010" type="checkbox" id="htmlChkBox_1000000411_55010_57010_1"
                                                                value="Fried|6059" checked="checked" /></td>
                                                        <td align="center">
                                                            <input name="htmlChkBox_1000000411_55010_57010" type="checkbox" id="htmlChkBox_1000000411_55010_57010_2"
                                                                value="Thandoori|6060" checked="checked" /></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            </tr>                           
                    </table>
              </td>
            </tr>
        </table>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

If you run this code using word 2003 and word 2007 you ll come to know what I am trying to ask