首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET:将表(和复选框)写入word文档

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

Stack Overflow用户
提问于 2011-03-19 15:16:09
回答 1查看 2.2K关注 0票数 0

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

我目前使用以下代码来写入word文档:

代码语言:javascript
复制
        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文档?

第二个问题是,是否有人知道如何在word文档中编写复选框(就像你从网站上复制并粘贴到word中一样)

提前感谢!

杀人狂

EN

回答 1

Stack Overflow用户

发布于 2011-03-19 17:05:43

Word可以识别HTML。因此您可以使用Response.Write();方法简单地将任何超文本标记语言写入MS WORD。以下是代码示例。

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5360761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档