如何从excel表中的datetime列中删除时间?

时间:2016-01-22 08:59:38

标签: c# asp.net excel c#-4.0

我已编写代码行

public static void GenrateExcel(DataSet ds, string FileName, string TemplateName)
{
 try
    {
       ReplcateColumnSpace(ds);

       HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
       HttpContext.Current.Response.Charset = "";

       XmlDataDocument xdd = new XmlDataDocument(ds);
       XslTransform xt = new XslTransform();
       xt.Load(HttpContext.Current.Server.MapPath("~/ExcelTemplate/" + TemplateName + ".xsl"));
       xt.Transform(xdd, null, HttpContext.Current.Response.OutputStream);
       HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
       //   HttpContext.Current.Response.Flush();

       HttpContext.Current.Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

它从ds(数据集)生成excel表。数据集到期日期中有一列。现在,只要调用此函数并成功生成excel表单。时间也会附加到Excel工作表文档中的列到期日期。我想从失效日期列中删除时间,日期应以mm / dd / yyyy格式显示。请帮忙 !!!

1 个答案:

答案 0 :(得分:0)

一种解决方案是更改数据集:

foreach (DataTable dt in ds.Tables)
        {
            if (dt.Columns.Contains("ExpiryDate"))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dr["ExpiryDate"] = DateTime.Parse((dr["ExpiryDate"].ToString())).Date;
                }
            }
        }