c# - 将excel中的日期和时间合并到数据库中
问题描述
我正在尝试从 excel 中的不同列获取日期和时间并写入数据库。例如在 excel 中
日期:23/01/2019
时间:18:30:00
需要读写的结果将是“2019-01-23 18:30:00”我做了谷歌一些解决方案并尝试了但仍然没有运气。
代码示例:
start.DateTime = DateTime.ParseExact(excelReader.GetDateTime(DatePOS) + " " + excelReader.GetDateTime(TimePOS),"yyyy-mm-dd hh:mm:ss",CultureInfo.InvariantCulture);
DatePOS/TimePOS 代表日期列和时间列的列位置,结果出现无效格式错误。当我检查声明并得到
GetDateTime(DatePOS) "23/01/2019 00:00:00"
GetDateTime(TimePOS) "30/12/1899 18:30:00"
start.DateTime "1/01/0001 00:00:00"
请帮忙,谢谢
解决方案
您应该使用GetString
而不是GetDateTime
。后者从 excel 列返回解析的日期。尝试这个
start.DateTime = DateTime.ParseExact(excelReader.GetString(DatePOS).Split(' ')[0] + " " + excelReader.GetString(TimePOS).Split (' ')[1],
"yyyy-mm-dd hh:mm:ss",
CultureInfo.InvariantCulture);
推荐阅读
- r - 使用管道为对象设置属性
- python - 在 tensorflow 数据集中显示示例不起作用
- azure-active-directory - Azure SQL 的服务器管理员只能通过 Azure AD 进行身份验证吗?我不喜欢没有 MFA 的普通密码
- python - 如何在python中将日期转换为列表
- python - 在一个折线图上绘制多列
- javascript - 是否可以使用 iframe 将 Web 应用程序(例如:ASP.net/node.js MVC 程序)嵌入到网页中?
- google-apps-script - Google 表格 - 如何从 iOS 应用程序运行脚本?
- azure - 用于根据 IDP 检查用户电子邮件并将其带到该 IDP 的 keycloak 流程
- javascript - 如何在 React 应用程序中的 ReactDOM.render 之前初始化 LocalStorage 中的数据
- ios - 使表格视图单元格中的文本字段成为第一响应者