如何检索数据从数据库到word文档。净文档、数据库、数据、word

2023-09-04 04:41:25 作者:温柔的像雨

我要检索数据表数据库到一个Word文档和Excel工作表,但我能够做到这一点的Excel工作表和放大器;不是为MS Word或PDF格式。任何一个可以建议我该怎么办呢? 这是我的code。在.NET:

 保护无效btnExportToExcel_Click(对象发件人,EventArgs的)
    {
        Response.Clear();
        Response.AddHeader(内容处置,附件;文件名= DocumentReport.xls);
        Response.Charset的=;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType =应用程序/ vnd.ms-EXCEL;
        System.IO.StringWriter stringWrite =新System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite =新的HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        回复于(stringWrite.ToString());
        到Response.End();
    }
    保护无效btnExportToWord_Click(对象发件人,EventArgs的)
    {
        Response.Clear();
        Response.AddHeader(内容处置,附件;文件名= DocumentReport.docx);
        Response.Charset的=;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType =应用程序/ vnd.ms字;
        System.IO.StringWriter stringWrite =新System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite =新的HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        回复于(stringWrite.ToString());
        到Response.End();
    }
 

解决方案

嘛。您的code似乎在我结束是工作。您收到任何类型的异常出口过程中?我也收到了同样的前一阵子做同样的事情。

我刚才提出的 EnableEventValidation =假在aspx页面的Page指令和它的工作。您可以尝试一样。 它应该工作。

I have to retrieve the data form database to a word document and excel sheet but I am able to do it for excel sheet & not for ms word or pdf. Can any one suggest me how to do so? Here is my code in .net:

    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    protected void btnExportToWord_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.docx");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
怎么把数据库里的文件显示到网页中来

解决方案

Well. Your code seems to be working at my end. Are you receiving any kind of exception during export? I also received the same a while ago doing the same thing.

I just made EnableEventValidation="false" in Page directive of aspx page and it worked. You can try the same. It should work.