C#使用OLEDB读取Excel的时间时,秒值没有了,怎么解决?

xxx355200 2020-05-07 04:18:45


如图,这个加油时间是有秒值的,但通过代码获取后,读不到秒。只到分钟。这样导致后面的计算会出问题,要读到秒才行。
让用户改文本格式不太现实。
有什么方法可以解决这个问题?

代码:
public static DataSet ExcelToDS (string file)
{
//string strConn = "Provider=Microsoft.Jet.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;HDR=NO;IMEX=1';";
string connstring = "";
string filetype = file.Substring(file.LastIndexOf('.') + 1);
DataSet dt = null;
if (filetype.ToLower() == "xlsx")
{
// Office 07及以上版本 不能出现多余的空格 而且分号注意
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
}
else //if (filetype.ToLower() == "xls")
{
//Office 07以下版本
connstring = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
}
using (OleDbConnection conn = new OleDbConnection(connstring))
{

conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//string tableName = schemaTable.Rows[0][2].ToString().Trim();
string tableName = "sheet1$";
string strExcel = "";
OleDbDataAdapter myCommand = null;

strExcel = "select * from [" + tableName + "]";

myCommand = new OleDbDataAdapter(strExcel, connstring);
dt = new DataSet();
myCommand.Fill(dt, "table1");

conn.Close();
return dt;

}

}
...全文
474 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不行 在Excel里面 你就把他全部显示出来呢,你在写入Excel的时候 日期转为string类型试试,如果是string 转为DateTime类型试试
xxx355200 2020-05-11
  • 打赏
  • 举报
回复
引用 1 楼 ajdkjalj 的回复:
换个方式,用npoi读试试


用npoi会经常出现版本不匹配,所以才换成OLEDB。
ajdkjalj 2020-05-08
  • 打赏
  • 举报
回复
换个方式,用npoi读试试

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧