0

我正在尝试将数据表导出到 asp.net mvc 中的 excel 表。我尝试了以下代码来导出。它没有显示任何错误,但也没有创建 excel 文件。那么这段代码有什么问题呢?

if (dt.Rows.Count > 0)
                {
                    string filename = "DownloadMobileNoExcel.xls";
                    System.IO.StringWriter tw = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dt;
                    dgGrid.DataBind();

                    dgGrid.RenderControl(hw);
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");

                    this.EnableViewState = false;
                    Response.Write(tw.ToString());
                    Response.End();
                }
4

1 回答 1

0

The process is explained in detail in this post: Export DataSet and DataTable to Excel 2007 in C#

于 2012-09-12T14:27:51.420 回答