如何修改 WebControls.GridView 中的字段列?

发布于 2024-11-24 03:27:56 字数 1880 浏览 0 评论 0原文

概述: 使用 C#,我将 LINQ to SQL 数据表输出到 WebControls.GridView,然后将其输出到从网页提供的 Excel 文件。如何修改 GridView 中的字段?

问题:Excel 正在截断和/或将数字字符串转换为科学记数法。

198886545467896 变成 1.98887E+14

为了将该字段保留为字符串,我需要将该字段包装为字符串公式,如下所示,以便 Excel 能够按预期显示它。

="198886545467896"

问题:如何使用 ="[number]" 将字段数据包装在特定的 GridView 列中?

这是我的相关 C#:

public void DownloadExcelReport(int id)
    {
        // See: http://www.codeshelve.com/code/details/54/export-to-excel-in-asp-net-mvc
        // Or: http://www.billsternberger.net/asp-net-mvc/export-to-excel-or-csv-from-asp-net-mvc-with-c/
        // Or: http://blog.wiredworx.co.uk/website-and-seo/c-tutorials-and-tips-visual-studio/exporting-to-an-excel-file-from-asp-net-mvc/
        // same solution - discussed differently            

        IEnumerable<Customer> customers = Db.Customers(id);

        // Create a grid and bind the customer data to it.
        var grid = new System.Web.UI.WebControls.GridView();
        grid.DataSource = customers;
        grid.DataBind();

        // TODO: I need to wrap the data in column 2 with =" "
        // UPDATE: INSERT the code from the accepted answer below right here

        // Prep the http response to return an excel mime type file
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
        Response.ContentType = "application/excel";

        // Output the grid via the html writer to the response
        StringWriter sw = new StringWriter();
        var htw = new System.Web.UI.HtmlTextWriter(sw);
        grid.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

更新:回想起来,这本来可以写得更简单,以隔离特定问题:GridView 导航起来很笨拙。不过,很高兴拥有有关如何从 Linq to Sql 表一直输出到 Excel 文件以供以后参考的完整解决方案。

Overview: Using C#, I'm outputting a LINQ to SQL table of data into a WebControls.GridView and then outputting it to an Excel file served from a webpage. How do I modify a field in the GridView?

Problem: Excel is truncating and/or converting a string that is numeric to scientific notation.

198886545467896 turns into 1.98887E+14

In order to retain this field as a string, I need to wrap the field into a string formula as follows so that Excel will display it as intended.

="198886545467896"

Question: How do I wrap the field data in a specific GridView column with ="[number]"?

Here is my relevant C#:

public void DownloadExcelReport(int id)
    {
        // See: http://www.codeshelve.com/code/details/54/export-to-excel-in-asp-net-mvc
        // Or: http://www.billsternberger.net/asp-net-mvc/export-to-excel-or-csv-from-asp-net-mvc-with-c/
        // Or: http://blog.wiredworx.co.uk/website-and-seo/c-tutorials-and-tips-visual-studio/exporting-to-an-excel-file-from-asp-net-mvc/
        // same solution - discussed differently            

        IEnumerable<Customer> customers = Db.Customers(id);

        // Create a grid and bind the customer data to it.
        var grid = new System.Web.UI.WebControls.GridView();
        grid.DataSource = customers;
        grid.DataBind();

        // TODO: I need to wrap the data in column 2 with =" "
        // UPDATE: INSERT the code from the accepted answer below right here

        // Prep the http response to return an excel mime type file
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
        Response.ContentType = "application/excel";

        // Output the grid via the html writer to the response
        StringWriter sw = new StringWriter();
        var htw = new System.Web.UI.HtmlTextWriter(sw);
        grid.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

Update: in retrospect, this could have been written much more simply, to isolate the specific issue: GridView is clunky to navigate. However, it's nice to have the whole solution in how to output from a Linq to Sql table all the way to an Excel file for later reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2024-12-01 03:28:00

像这样的东西吗?

System.Web.UI.WebControls.GridView() test = new System.Web.UI.WebControls.GridView();
for (int i = 0; i < grid.Rows.Count; i++)
{
    test.Rows[i].Cells[<yer column index>].Text = "=/"" + test.Rows[i].Cells[<yer column index>].Text + "/"";
}

Something like this?

System.Web.UI.WebControls.GridView() test = new System.Web.UI.WebControls.GridView();
for (int i = 0; i < grid.Rows.Count; i++)
{
    test.Rows[i].Cells[<yer column index>].Text = "=/"" + test.Rows[i].Cells[<yer column index>].Text + "/"";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文