如何将gridview的所有页面导出到excel,而不包含控件的列

本文关键字:excel 控件 包含 gridview | 更新日期: 2023-09-27 17:54:50

我有一个gridview,有2列,是一个下拉列表和上传控制。我试图将网格视图导出到excel和代码工作,但它只适用于导出当前视图而不是所有页面。当我尝试导出所有页面时,数据绑定失败,代码崩溃。请帮助,如果你知道让所有的页面导出到excel/csv有或没有包含控件的列的方式。

<asp:ImageButton ID="ExportBtn" runat="server" ImageUrl="../Images/ExcelIcon.jpg" Alt="Export" Width="40px" Height="40px" style="border-width:0px" onclick="ExportBtn_Click" />

protected void ExportBtn_Click(object sender, ImageClickEventArgs e)
{
        gv.Columns[7].Visible = false;
        gv.Columns[8].Visible = false;
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Documents.xls");
        Response.ContentType = "application/excel";
        gv.AllowPaging = false;
        gv.DataBind();
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        Response.Charset = String.Empty;
        gv.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

当我尝试导出所有页面时,我得到的错误是:服务器错误在'…20150605 _0846_local"应用程序。

'DocStatusDropDown'有一个无效的SelectedValue,因为它不存在于项目列表中。参数名称:value

描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。

Exception Details: System.ArgumentOutOfRangeException: 'DocStatusDropDown' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Source Error: 

Line 194:        gvMainGrid.Columns[8].Visible = false;
Line 195:        gvMainGrid.AllowPaging = false;
Line 196:        gvMainGrid.DataBind();
Line 197:        Response.ClearContent();
Line 198:        Response.AppendHeader("content-disposition", "attachment; filename=MissingDocuments.xls");

如何将gridview的所有页面导出到excel,而不包含控件的列

使用ExcelLibrary很容易实现。但是你需要传递一个dataSet to CreateWorkbook方法。

    protected void CreateExcel_Click(object sender, EventArgs e)
     {
         List<Student> dataSource = (List<Student>)GrdData.DataSource; 
         DataSet ds = new DataSet("NewDataSet"); 
         DataTable dTable = ExtentionHelper.ToDataTable<Student>(dataSource);
         ds.Tables.Add(dTable);
         ExcelLibrary.DataSetHelper.CreateWorkbook("C://MyExcelFile.xlsx", ds);
      }

ExtentionHelper从List<Student>获取数据表

public static class ExtentionHelper
    {
        public static DataTable ToDataTable<T>(this IEnumerable<T> collection)
        {
            DataTable dt = new DataTable("DataTable");
            Type t = typeof(T);
            PropertyInfo[] pia = t.GetProperties();
            //Inspect the properties and create the columns in the DataTable
            foreach (PropertyInfo pi in pia)
            {
                Type ColumnType = pi.PropertyType;
                if ((ColumnType.IsGenericType))
                {
                    ColumnType = ColumnType.GetGenericArguments()[0];
                }
                dt.Columns.Add(pi.Name, ColumnType);
            }
            //Populate the data table
            foreach (T item in collection)
            {
                DataRow dr = dt.NewRow();
                dr.BeginEdit();
                foreach (PropertyInfo pi in pia)
                {
                    if (pi.GetValue(item, null) != null)
                    {
                        dr[pi.Name] = pi.GetValue(item, null);
                    }
                }
                dr.EndEdit();
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Documents.xls"));
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
gv.AllowPaging = false;
gv.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

谢谢大家!我找到了一种方法,可以在没有故障控制列的情况下复制网格视图并导出它。也把重复的gv变成不显示,所以没有UI尴尬。