【问题标题】:Export user controls gridview to excel将用户控件gridview导出到excel
【发布时间】:2012-12-26 12:39:30
【问题描述】:

在我的 aspx 页面上,我有一个 Usercontrol,其中包含一个 Gridview,其数据是根据从 aspx 页面传递到 UserControl 的参数动态加载的,我称之为 QuotesReport1

导出运行时,只将布局导出到Spreadsheet,并没有gridview的数据,例如:


<style>
    body
    {
        margin: 0px;
    }
</style>
<div>
</div>

我的导出代码是:

protected void Page_Load(object sender, EventArgs e)
{
    string toDate = "";
    string fromDate = "";

    toDate = Request.QueryString.Get("toDate");
    fromDate = Request.QueryString.Get("fromDate");


    QuotesReport1.ToDate = DateTime.Parse(toDate);
    QuotesReport1.FromDate = DateTime.Parse(fromDate);
    QuotesReport1.Status = QuotesReport.quoteStatus.PENDING_REVIEW;

    string attachment = "attachment; filename=Quotes_Pending.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    QuotesReport1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control) { }

在 UserControl 内部,运行 SQL 查询,该查询返回一个 DataTable,然后将其绑定到 UserControl 内部的 Gridview,类似于:

SQLData da = new SQLData();
GridView1.DataSource = da.SGetDataTable(query);
GridView1.DataBind();

【问题讨论】:

  • 在将数据写入excel表之前绑定网格。
  • 绑定gridview的代码在哪里?
  • 将该代码粘贴到您的原始问题中.. cmets 部分中的格式使其难以阅读...
  • 谢谢@DJKRAZE,我已经相应地更新了。
  • 只是一个简单的问题,网格实际上是否显示任何数据..?您是否已通过 Page_Load 事件上的代码以及在 UserControl 所在的代码中放置断点..?

标签: c# asp.net excel gridview


【解决方案1】:

我有最简单的方法

public class ExcelUtility
{
    public static void ToExcel(object dataSource)
    {
        GridView grid = new GridView { DataSource = dataSource };
        grid.DataBind();

        StringBuilder sb = new StringBuilder();
        foreach (TableCell cell in grid.HeaderRow.Cells)
            sb.Append(string.Format("\"{0}\",", cell.Text));
        sb.Remove(sb.Length - 1, 1);
        sb.AppendLine();

        foreach (GridViewRow row in grid.Rows)
        {
            foreach (TableCell cell in row.Cells)
                sb.Append(string.Format("\"{0}\",", cell.Text.Trim().Replace("&nbsp;", string.Empty)));
            sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();
        }
        ExportToExcel(sb.ToString());
    }

    private static void ExportToExcel(string data)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Report.csv");
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.Write(data);
        HttpContext.Current.Response.End();
    }
}

使用:

   string result = ExcelUtility.ToExcel(_db.FindAll());
   ExcelUtility.ExportToExcel(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    相关资源
    最近更新 更多