C# 在C中从DataGrid导出到Excel时停止日期自动格式化#

C# 在C中从DataGrid导出到Excel时停止日期自动格式化#,c#,excel,datetime,C#,Excel,Datetime,我当前正在格式化从数据集/数据网格导出的特定Excel文件的日期。 日期的格式如下: DateTime date = Convert.ToDateTime(entry.Date); string formatdate = String.Format("{0:yyyy/MM/dd}", date); 创建数据集完成后,我使用以下代码将数据集导出到Excel文件: public static void ExportDStoExcel(DataSet ds, string filename)

我当前正在格式化从数据集/数据网格导出的特定Excel文件的日期。
日期的格式如下:

DateTime date = Convert.ToDateTime(entry.Date);
string formatdate = String.Format("{0:yyyy/MM/dd}", date);
创建数据集完成后,我使用以下代码将数据集导出到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的DateTime自动格式化功能?

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

现在您只需输出HTML表格,Excel就会解释它喜欢的格式。您必须降低到Excel级别才能指定列的属性(将type设置为Text而不是General)。
这意味着您需要生成实际的xls文件(有各种库可供使用)。或者(如果可以接受对Office 2010的限制)获得了可以使用常规.NET API编写的格式。

我也遇到了同样的问题,并通过在文本前添加一个不间断空格(non-breaking space)来解决。已停止Excel自动格式化。这不是最干净的解决方案,但却为我带来了好处……

我也遇到了同样的问题,通过在文本前添加一个不间断的空格( )来解决它。已停止Excel自动格式化。这不是最干净的解决方案,但却帮了我的忙…

您可以使用mso数字格式设置excel单元格的样式

mso编号格式:“\\@”

\@
将告诉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();
    }
}   
response.ContentType=“应用程序/vnd.ms excel”;
AddHeader(“内容处置”、“附件;文件名=\”“+文件名+”\”);
回答。写(“”);
Write(“td{mso编号格式:\\@;}”);
使用(StringWriter sw=new StringWriter())
{
使用(HtmlTextWriter htw=新的HtmlTextWriter(sw))
{
DataGrid dg=新DataGrid();
dg.DataSource=ds.Tables[0];
dg.DataBind();
调度主任(htw);
response.Write(sw.ToString());
回答。写(“”);
response.End();
}
}   

您也可以尝试使用特定的日期格式。 参考:


mso编号格式:“yyyy\/mm\/dd”
您可以使用mso编号格式设置excel单元格的样式

mso编号格式:“\\@”

\@
将告诉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();
    }
}   
response.ContentType=“应用程序/vnd.ms excel”;
AddHeader(“内容处置”、“附件;文件名=\”“+文件名+”\”);
回答。写(“”);
Write(“td{mso编号格式:\\@;}”);
使用(StringWriter sw=new StringWriter())
{
使用(HtmlTextWriter htw=新的HtmlTextWriter(sw))
{
DataGrid dg=新DataGrid();
dg.DataSource=ds.Tables[0];
dg.DataBind();
调度主任(htw);
response.Write(sw.ToString());
回答。写(“”);
response.End();
}
}   

您也可以尝试使用特定的日期格式。 参考:


mso编号格式:“yyyy\/mm\/dd”

要使用微软的开放XML SDK 2.0走开放XML之路,看起来它应该做到这一点!谢谢你给我指明了正确的方向!要使用微软的OpenXMLSDK2.0走开放XML路线,看起来它应该做到这一点!谢谢你给我指明了正确的方向!