string Path = "c:/Book1.xlsx";
string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" + Path+ ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
MessageBox.Show( ds.Tables["table1"].Rows.Count.ToString());
在执行到conn.Open();时出错!说 “外部表不是预期的格式。”
怎么回事??
我用的是vs2008+office2007
引用加了~
解决方案 »
- 谁有 用c#实现建立一个VPN拨号连接并自动拨号的源码啊
- 线程调用资源如何释放
- webBrowser控件,点击该控件里的超链接,如何取出超链接的值
- "在向服务器发送请求时发生传输级错误。 (provider: 共享内存提供程序, error: 1 - 执行读/写操作期间检测到 I/O 错误。)"
- 请哪位热心人给一个c#的下载地址。vs.net2k3的。
- 请问,如何将一个xml文档存入数据库?
- 菜鸟提问:什么是C/S和B/S呀?有什么区别?
- 在C#里面如何取得这个SQL値那?
- 如何调用下面的API函数???
- 这个sleep延时没有达到想要的效果怎么回事
- 快放假了,求几本c#的好书。
- 高手进,问点问题
string Path = @"c:\Book1.xlsx";
string Path = "c:\\Book1.xlsx";或者 string Path = @"c:\Book1.xlsx";
正确写法:C:\\Book1.xlsx
我也遇到过这样的一种情况,我是通过写文件的形式先把数据写进Excel,再用这种方式读的时候,就报这个错误
建议改下路径试下,如果不行,新建一个Excel,不要拷贝原来Excel的数据,自己写数据进去,再试试
string strConn =@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path+ ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
MessageBox.Show( ds.Tables["table1"].Rows.Count.ToString());
http://blog.csdn.net/Mittermeyer/archive/2008/04/05/2253152.aspx