将所有gridview页面导出为ex​​cel

时间:2015-04-28 06:52:11

标签: asp.net .net vb.net gridview export-to-excel

我正在尝试将gridview列表导出到Excel工作表。一切正常,但是当我有多个页面时,gridview只导出第一页。

这是我使用的代码:

Public Sub Export(ByVal fileName As String, ByVal gv As GridView)    
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/ms-excel"
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    gv.AllowPaging = False
    GetData()
    Dim table As System.Web.UI.WebControls.Table = New System.Web.UI.WebControls.Table
    table.GridLines = gv.GridLines
    For Each row As GridViewRow In gv.Rows
        PrepareControlForExport(row)
        table.Rows.Add(row)
    Next
    table.RenderControl(htw)
    '  render the htmlwriter into the response
    HttpContext.Current.Response.Write(sw.ToString)
    HttpContext.Current.Response.[End]()
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个,它是我使用的excel导出的更好的抛光版本: (有些代码可能在C#中,转换为VB或者告诉我这样做。

    Public Sub Export(ByVal fileName As String, ByVal gv As GridView) 
{

            Response.ClearContent();
            Response.Charset = your charset//"Windows-1253";
            Response.ContentEncoding = Encoding.UTF8;
            string attachment = "attachment;" + filename;
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");

            HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
            HttpContext.Current.Response.Write("<BR><BR><BR>");
            HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
              "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
              "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
            int columnscount = gv.Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {
                HttpContext.Current.Response.Write("<Td style='background-color: #C0C0C0;' align=" + "center" + ">");
                HttpContext.Current.Response.Write("<B>");
                HttpContext.Current.Response.Write(gv.Columns[j].ToString());
                HttpContext.Current.Response.Write("</B>");
                HttpContext.Current.Response.Write("</Td>");
            }
            HttpContext.Current.Response.Write("</TR>");
            foreach (DataRow row in gv.Rows)
            {
                HttpContext.Current.Response.Write("<TR>");
                for (int i = 0; i < gv.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<Td align=" + "center" + ">"); ;
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</Td>");
                }

                HttpContext.Current.Response.Write("</TR>");
            }
            HttpContext.Current.Response.Write("</Table>");
            HttpContext.Current.Response.Write("</font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
}