简体   繁体   中英

How to remove time from datetime column in excel sheet?

I have written lines of code as

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;
    }
}

It generates excel sheet from ds (dataset).There is a column in dataset Expiry Date. Now whenever this function is called and excel sheet is generated successfully. Time also gets appended to the column Expiry Date in excel sheet document. I want to remove time from the expiry date column and date should come in mm/dd/yyyy format. Please help !!!

One solution would be to change the dataset:

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;
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM