ASP.NET:将表(和复选框)写入word文档

时间:2011-03-19 07:16:09

标签: c# asp.net

我正在用c#编写一个ASP.NET应用程序,我正在尝试将一个表写入word文档。

我目前使用以下代码来写一个单词doc:

        string strContent = "This content goes to the word document";
        string attach = "attachment; filename=wordtest.doc";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";

        HttpContext.Current.Response.ContentType = "application/msword";
        HttpContext.Current.Response.AddHeader("content-disposition", attach);
        HttpContext.Current.Response.Write(strContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();

现在,我的问题是,是否有人知道如何将表格写入word文档?

第二个问题是,如果有人知道如何写一个单词doc的复选框(就像你从网站上复制它并将其粘贴到单词中)

提前致谢!

Killerwes

1 个答案:

答案 0 :(得分:1)

Word可以识别HTML。因此,您可以使用Response.Write()将任何HTML写入MS WORD;方法。这是代码示例。

string strBody = "<html>" +

            "<body>" + 

                "<div>Your name is: <b>" + txtName.Text + "</b></div>" +

                "<table width="100%" style="background-color:#cfcfcf;"><tr><td>1st Cell body data</td><td>2nd cell body data</td></tr></table>" +

                "Ms Word document generated successfully." +

            "</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.Write(strBody);