【发布时间】: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 的日期时间自动格式化?
【问题讨论】: