将date列转换为datatable中的字符串 [英] Convert date column to string in datatable

查看:88
本文介绍了将date列转换为datatable中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据表有多个日期列。导出到excel时我得到的错误是dateformat错误。我需要将这些数据列转换为数据表中的字符串。任何人都可以建议请。



我尝试过:



Im having datatable with multiple date columns. While exporting to excel im getting error that dateformat is wrong. I need to convert those data column to string in datatable. can anyone suggest pls.

What I have tried:

DataTable dt = dv.ToTable();
           foreach (DataRow dr in dt.Rows)
           {

           }

推荐答案

对我来说 - 您的问题与日期列无关,而是与您用于将数据导出到MS Excel文件的方法有关。因为您没有向我们展示您尝试导出到Excel的代码和示例数据,我们无法帮助您。

似乎您将日期存储为字符串,这就是您的原因'面对错误的日期格式。
As to me - your problem is not related with date column, but with the method you use to export data to MS Excel file. Because you didn't show us your code and sample data you're trying to export to Excel, we aren't able to help you.
Seems you're storing dates as string, that's why you're facing with wrong format of date.


你可以试试这个,假设你的列名是Date



You can try this, assuming your column name is Date

foreach (DataRow row in dt.Rows)
{
    DateTime dt = DateTime.Parse(row["Date"].ToString());
    row["Date"] = dt.ToShortDateString();
}


这篇关于将date列转换为datatable中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆