使用 C# 在 Word 2003 中导入 HTML

发布于 2024-07-19 06:36:12 字数 230 浏览 5 评论 0原文

我正在尝试将网页内容导出到Word中。 在 Word 2007 中,数据以正确的格式显示。 但对于word 2003,显示格式不正确。 这是什么原因呢? 我应该做什么来纠正这个问题?

我正在显示一个文本框和一些矩阵形式的复选框控件。 实际上在网页中这些控件是使用表格属性显示的。 在Word 2003中,表格属性反映在Word页面中。 但在 Word 2007 中,表属性被删除并提供了预期的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-07-26 06:36:12
  • 这是我使用系统的.CS代码

使用系统数据;

使用系统配置;

使用系统集合;

使用系统.Web;

使用 System.Web.Security;

使用 System.Web.UI;

使用 System.Web.UI.WebControls;

使用 System.Web.UI.WebControls.WebParts;

使用 System.Web.UI.HtmlControls;

使用系统文本;

使用系统.IO;

使用 System.Text.RegularExpressions;

公共部分类 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();




}

}

  • 这是我的 ASPx 页面

<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%">
                                                             </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>

如果您使用 word 2003 和 word 2007 运行此代码,您就会知道我想问什么

  • 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

<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%">
                                                             </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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文