我当前正在格式化从数据集/数据网格导出的特定 Excel 文件的日期。
日期的格式如下:
DateTime date = Convert.ToDateTime(entry.Date);
string formatdate = String.Format("{0:yyyy/MM/dd}", date);
Run Code Online (Sandbox Code Playgroud)
创建数据集完成后,我使用以下代码将数据集导出到 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我唯一的问题是,一旦我将其导出到 Excel,Excel 会自动设置日期格式,如下所示:MM/DD/YYYY 而不是 YYYY/MM/DD。
我知道这可以通过在 Excel 中打开来手动实现,但导出正在构建到自动化系统中,需要进行硬编码。
有没有办法绕过Excel的日期时间自动格式化?
| 归档时间: |
|
| 查看次数: |
5464 次 |
| 最近记录: |