【问题标题】:Stop Date Auto-Format when Exporting from DataGrid to Excel in C#在 C# 中从 DataGrid 导出到 Excel 时停止日期自动格式化
【发布时间】:2011-05-23 17:51:50
【问题描述】:

我目前正在格式化从 DataSet/DataGrid 导出的特定 Excel 文件的日期。
日期格式如下:

DateTime date = Convert.ToDateTime(entry.Date);
string formatdate = String.Format("{0:yyyy/MM/dd}", date);

创建 DataSet 后,我​​使用以下代码将 DataSet 导出到 Excel 文件:

public static void ExportDStoExcel(DataSet ds, string filename)
    {
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.Charset = "";

        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                DataGrid dg = new DataGrid();
                dg.DataSource = ds.Tables[0];
                dg.DataBind();
                dg.RenderControl(htw);
                response.Write(sw.ToString());
                response.End();
            }
        }

    }

我唯一的问题是,一旦我将其导出到 Excel,Excel 会自动格式化日期,如下所示:MM/DD/YYYY 而不是 YYYY/MM/DD。

我知道这可以通过在 Excel 中打开来手动实现,但导出正在构建到自动化系统中,需要进行硬编码。

有没有办法绕过 Excel 的日期时间自动格式化?

【问题讨论】:

    标签: c# excel datetime


    【解决方案1】:

    我遇到了同样的问题,并通过在文本前面添加一个不间断空格 (&nbsp) 来解决它。停止 Excel 自动格式化。不是最干净的解决方案,但对我有用...

    【讨论】:

      【解决方案2】:

      现在您只是在输出 HTML 表格,Excel 会按照自己的喜好进行解释。您必须将自己降低到 Excel 的级别才能指定列的属性(将类型设置为文本而不是常规)。 这意味着您需要生成实际的 xls 文件(那里有各种库)。或者(如果可以接受对 Office 2010 的限制)使用 Open XML 格式,您可以使用常规 .NET API 编写。

      【讨论】:

      • 打算使用 Microsoft 的 Open XML SDK 2.0 走 Open XML 路线,看起来应该可以解决问题!感谢您为我指明正确的方向!
      【解决方案3】:

      您可以使用 mso-number-format 设置 excel 单元格样式

      mso-number-format:"\\@"

      \@ 将告诉 excel 仅处理文本格式的所有数据。所以不会发生自动格式化。

      请像这样更新您的代码:

      response.ContentType = "application/vnd.ms-excel";
      response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
      response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
      response.Write("<head><style> td {mso-number-format:\\@;} </style></head><body>");
      
      using (StringWriter sw = new StringWriter())
      {
          using (HtmlTextWriter htw = new HtmlTextWriter(sw))
          {
              DataGrid dg = new DataGrid();
              dg.DataSource = ds.Tables[0];
              dg.DataBind();
              dg.RenderControl(htw);
              response.Write(sw.ToString());
      
              response.Write("</body></html>");
              response.End();
          }
      }   
      

      您也可以尝试使用特定的日期格式。 参考:http://cosicimiento.blogspot.in/2008/11/styling-excel-cells-with-mso-number.html

      mso-number-format:"yyyy\/mm\/dd"

      【讨论】:

        猜你喜欢
        • 2011-12-03
        • 2020-03-26
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-22
        相关资源
        最近更新 更多