将gridview导出到excel文件

发布于 2024-12-14 23:43:38 字数 862 浏览 3 评论 0原文

我有这个代码 它一直给我错误,

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim response As HttpResponse = HttpContext.Current.Response()
    response.Clear()
    response.AddHeader("content-disposition", "attachment;filename=XXXXXX.xls")
    response.ContentType = "application/vnd.ms-excel"
    Dim s As System.IO.StringWriter = New System.IO.StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(s)
    GridView1.RenderControl(htw)
    response.Write(s.ToString)
    response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

End Sub

错误是在我单击导出之后:RegisterForEventValidation 只能在期间调用

Render(); 

,并且它突出显示此脚本

 GridView1.RenderControl(htw)

任何建议

i have this code
it keeps giving me error

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim response As HttpResponse = HttpContext.Current.Response()
    response.Clear()
    response.AddHeader("content-disposition", "attachment;filename=XXXXXX.xls")
    response.ContentType = "application/vnd.ms-excel"
    Dim s As System.IO.StringWriter = New System.IO.StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(s)
    GridView1.RenderControl(htw)
    response.Write(s.ToString)
    response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

End Sub

the error is after i click on export : RegisterForEventValidation can only be called during

Render(); 

and it highlight this script

 GridView1.RenderControl(htw)

any advices

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

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

发布评论

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

评论(2

木森分化 2024-12-21 23:43:38

禁用页面级别的事件验证:

<%@ Page Language="VB" AutoEventWireup="false" EnableEventValidation="false"  ...

但我建议创建一个真正的Excel文件并写入二进制到流而不是 HTML 表。强烈推荐EppPlus易于使用并支持 LINQ。 (GPLv2)。

Disable event validation on page level:

<%@ Page Language="VB" AutoEventWireup="false" EnableEventValidation="false"  ...

But i would suggest creating a real excel file and write that binary to the stream instead of a HTML table. EppPlus is highly recommended, easy to use and supports LINQ. (GPLv2).

待天淡蓝洁白时 2024-12-21 23:43:38

** 复制粘贴以下代码并测试您的输出 ** 在添加引用Microsoft Excel 11.0 对象库/Microsoft Excel 12.0 对象库/Microsoft Excel 14.0 对象库之前

        String fileName ="GVtoExcel";

        Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

        ExcelApp.Application.Workbooks.Add(Type.Missing);
        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
        {

            ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

        }
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {

            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {

                ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

            }

        }
        ExcelApp.ActiveWorkbook.SaveCopyAs(fileName);

        ExcelApp.ActiveWorkbook.Saved = true;

        ExcelApp.Quit();
       System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
       ExcelApp = null;

** Copy Paste the below coding and test your output ** before that Add Reference to Microsoft Excel 11.0 Object Library/Microsoft Excel 12.0 Object Library/Microsoft Excel 14.0 Object Library

        String fileName ="GVtoExcel";

        Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

        ExcelApp.Application.Workbooks.Add(Type.Missing);
        for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
        {

            ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

        }
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {

            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {

                ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

            }

        }
        ExcelApp.ActiveWorkbook.SaveCopyAs(fileName);

        ExcelApp.ActiveWorkbook.Saved = true;

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