【问题标题】:DataGrid to excel not showing automatic gridlinesDataGrid to excel不显示自动网格线
【发布时间】:2012-07-11 09:27:24
【问题描述】:

想知道是否有人可以提供帮助。我有一个 .ashx 文件,它创建了一个填充有 C# DataGrid 的 excel 文档。这一切都很好,唯一的问题是当我打开 excel 文档时,自动网格线被关闭。有没有办法启用它们?

提前致谢, 空气

HttpResponse response = HttpContext.Current.Response;

            response.Clear();
            response.Charset = "";
            response.ContentEncoding = System.Text.Encoding.Default;
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"dataImportTemplate.xls\"");
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
                    dg.DataSource = ds.Tables[0];
                    dg.ShowHeader = false;
                    dg.DataBind();                    
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                }
            }

【问题讨论】:

  • 请告诉我们创建excel文档的代码
  • 您好,我已将代码添加到原帖中
  • 在传递给 response.write 之前尝试设置数据网格的 BorderColor、BorderStyle 和 BorderWidth
  • 嗨,试过了,它只设置了数据网格的边框。它没有启用 excel 文档上的自动网格线 :(

标签: c# excel datagrid ashx


【解决方案1】:

如果您导出到 csv,它会保留网格线。 这是很多人选择使用的非常简单的解决方法。

见类似帖子Here

该帖子包含一个帮助函数,据说可以将网格线添加到您的导出中。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-08-23
    • 2012-06-13
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多