C# 读取Excel
2024-08-27 03:43:40
直接添代码:
public void connExcel(string strPath)
{
//string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"; OleDbDataReader myDataReader = null;
OleDbConnection myOleDbConnection = new OleDbConnection(strConn);
OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", myOleDbConnection); try
{
myOleDbConnection.Open();
myDataReader = myOleDbCommand.ExecuteReader();
while (myDataReader.Read())
{
Console.WriteLine("序号:" + myDataReader.GetValue());//列1
Console.WriteLine("标题:" + myDataReader.GetValue());//列2
Console.WriteLine("预期结果:" + myDataReader.GetValue());//列3
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
// Always call Close when done reading.
if (myDataReader != null)
myDataReader.Close(); // Close the connection when done with it.
if (myOleDbConnection != null)
myOleDbConnection.Close();
}
}
遇到问题:
1.open时,报“找不到可安裝的ISAM解決方法”
解决方法:'Excel 8.0;HDR=Yes;IMEX=1;'这个要用单引号引起来
2.报错:外部表不是预期的格式错误
错误原因1: 由于Excel 97-2003的连接格式与Excel 2010 的 不同造成。
解决方法:
//2003(Microsoft.Jet.Oledb.4.0)
string strConn = string.Format("Provider=Microsoft.Jet.OLEDB..0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
//2007 , 2010(Microsoft.ACE.OLEDB.12.0)
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB..0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'", excelFilePath);
3.Access有密碼設置時,當用C#連接Access Database時可能會出現"找不到可安裝的ISAM"。
解决方法:string dsn = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:DataBase Password=123;Data Source=C:\\...\\*.mdb";
最新文章
- scrollTop和scrollLeft的兼容解决万全方法
- centos 安装jdk
- [selenium webdriver Java]常用api
- Opencv各个版本的万能头文件
- char *详细指针
- Linux入门学习教程:虚拟机体验之KVM篇
- snprintf 返回值陷阱 重新封装
- 阿里云服务器实战(二): Linux MySql5.6数据库乱码问题
- Jmeter运行后出现乱码
- 自定义的jdbc连接工具类JDBCUtils【java 工具类】
- HDU 1556-Color the ball-树状数组
- redis客户端、分布式锁及数据一致性
- Vue.js的复用组件开发流程
- Http指南(1)
- url自动补全index.php
- 洛谷P4180 [BJWC2010]次小生成树(最小生成树,LCT,主席树,倍增LCA,倍增,树链剖分)
- 发现vi出现此错误~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycm_core.so: undefined symbol: clang_getCompletionFixIt
- AM335x移植linux内核_转
- 把 Activity 改成 ListActivity继续使用 setContentView
- ubuntu :扩充虚拟机的磁盘容量
热门文章
- 轻量级SaaS在线作图工具(继之前介绍后完整介绍)
- ArcGIS中定义图框样式
- access中根据一个表创建另一个
- 错误:StrictMode $ AndroidBlockGuardPolicy.onNetwork
- 【读书笔记】iOS网络-运行循环
- ios开发随笔第一篇-button,label按钮的一些属性的使用
- QA:Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, ReasonPhrase:Method Not Allowed.
- 深入剖析 Spring 框架的 BeanFactory
- PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求
- [转][原]openstack-kilo--issue(六)kilo版openstack的dashboard在session超时后重新登录报错解决办法