关于excel、sql语句查询的问题

simon_c 2010-06-15 02:34:11

string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelName + ";Extended Properties='Excel 8.0;HDR=NO';";
OleDbConnection con = new OleDbConnection(strConn);
OleDbDataAdapter cmd = new OleDbDataAdapter("select top 4 * from ["+GetSheetName(excelName)+"$]", con);
DataSet ds = new DataSet();
try
{
cmd.Fill(ds);
}
catch
{
return null;
}
return ds;


如果excel里面有日期格式的单元格的话,那查出来的不是文本里显示的内容,比如2008-8-1,那我查出来的是40026。怎么解决。怎么能查出显示的内容?
...全文
391 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyzsy2008 2010-06-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 computerfox 的回复:]

首先要知道为什么会这样!

从EXCEL中读取文本,数值型数据都很简单,EXCEL没有做其他转换,
可是读取日期数据就出现了一个问题:
EXCEL保存日期数据实际上存放的是时间戳,从1900年开始算起。
也就是说 1900-1-1 在EXCEL中表示为 1 。
OK,明白了这个原理之后就可以开始操作了 :

C# code
static readonly DateTime ma……
[/Quote]

顶一个先
捷哥1999 2010-06-15
  • 打赏
  • 举报
回复
首先要知道为什么会这样!

从EXCEL中读取文本,数值型数据都很简单,EXCEL没有做其他转换,
可是读取日期数据就出现了一个问题:
EXCEL保存日期数据实际上存放的是时间戳,从1900年开始算起。
也就是说 1900-1-1 在EXCEL中表示为 1 。
OK,明白了这个原理之后就可以开始操作了 :


static readonly DateTime march1st1900 = new DateTime(1900, 03, 01);
static readonly DateTime december31st1899 = new DateTime(1899, 12, 31);
static readonly TimeSpan after1stMarchAdjustment = new TimeSpan(1, 0, 0, 0);

public static DateTime ConvertExcelDateToDate(string excelDate)
{
TimeSpan ts = TimeSpan.Parse(excelDate);
DateTime dt = december31st1899+ts;
if (dt >= march1st1900)
{
return dt - after1stMarchAdjustment;
}
return dt;
}
马老虎 2010-06-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]

设置单元格格式
mySheet.Cells(1,9).NumberFormatLocal = "@"
Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]);
r.NumberFormat = "@";
r.NumberForma = "0.00_ "
[/Quote]
如梦你咋啥都知道??
wuyq11 2010-06-15
  • 打赏
  • 举报
回复
设置单元格格式
mySheet.Cells(1,9).NumberFormatLocal = "@"
Excel.Range r = mySheet.get_Range(mySheet.Cells[1, 1], mySheet.Cells[1,3]);
r.NumberFormat = "@";
r.NumberForma = "0.00_ "
捷哥1999 2010-06-15
  • 打赏
  • 举报
回复
从EXCEL中读取文本,数值型数据都很简单,EXCEL没有做其他转换,
可是读取日期数据就出现了一个问题:
EXCEL保存日期数据实际上存放的是时间戳,从1900年开始算起。
也就是说 1900-1-1 在EXCEL中表示为 1 。
OK,明白了这个原理之后就可以开始操作了 :

s
tatic readonly DateTime march1st1900 = new DateTime(1900, 03, 01);
static readonly DateTime december31st1899 = new DateTime(1899, 12, 31);
static readonly TimeSpan after1stMarchAdjustment = new TimeSpan(1, 0, 0, 0);

public static DateTime ConvertExcelDateToDate(string excelDate)
{
TimeSpan ts = TimeSpan.Parse(excelDate);
DateTime dt = december31st1899+ts;
if (dt >= march1st1900)
{
return dt - after1stMarchAdjustment;
}
return dt;
}

转自:http://www.cnblogs.com/HQT/archive/2008/04/22/231960.html
simon_c 2010-06-15
  • 打赏
  • 举报
回复
help!~

110,476

社区成员

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

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

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