C# 通过 oledb 操作Excel实例代码
作者:李子康 时间:2022-12-22 04:27:49
整理文档,搜刮出一个C# 通过 oledb 操作Excel实例代码,稍微整理精简一下做下分享。
public string GetConnectionString()
{
Dictionary<string, string> props = new Dictionary<string, string>();
// XLSX - Excel 2007, 2010, 2012, 2013
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "Excel 12.0 XML";
props["Data Source"] = @"C:\tools\MyExcel.xlsx";
// XLS - Excel 2003 and Older
//props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
//props["Extended Properties"] = "Excel 8.0";
//props["Data Source"] = "C:\\MyExcel.xls";
var sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
return sb.ToString();
}
public void WriteExcelFile()
{
string connectionString = GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "CREATE TABLE [table1] (id INT, name VARCHAR, datecol DATE );";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(1,'AAAA','2014-01-01');";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(2, 'BBBB','2014-01-03');";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(3, 'CCCC','2014-01-03');";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE [table1] SET name = 'DDDD' WHERE id = 3;";
cmd.ExecuteNonQuery();
conn.Close();
}
}
public DataSet ReadExcelFile()
{
DataSet ds = new DataSet();
string connectionString = GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// Get all Sheets in Excel File
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// Loop through all Sheets to get data
foreach (DataRow dr in dtSheet.Rows)
{
string sheetName = dr["TABLE_NAME"].ToString();
if (!sheetName.EndsWith("$"))
continue;
// Get all rows from the Sheet
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
DataTable dt = new DataTable();
dt.TableName = sheetName;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
ds.Tables.Add(dt);
}
cmd = null;
conn.Close();
}
return ds;
}
来源:http://www.cnblogs.com/lizikang2013/p/6930833.html?utm_source=tuicool&utm_medium=referral
标签:C#,oledb,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaSE中compare、compareTo的区别
2021-12-28 09:37:47
![](https://img.aspxhome.com/file/2023/4/77134_0s.png)
Java多线程之线程同步
2023-01-24 18:39:00
![](https://img.aspxhome.com/file/2023/8/101988_0s.png)
Springboot+Mybatis-plus不使用SQL语句进行多表添加操作及问题小结
2021-09-30 10:31:10
![](https://img.aspxhome.com/file/2023/7/94377_0s.png)
Android接入阿里云热修复介绍
2023-09-16 08:53:01
java复制文件的4种方式及拷贝文件到另一个目录下的实例代码
2023-05-15 16:03:25
对int array进行排序的实例讲解
2021-12-09 06:51:15
![](https://img.aspxhome.com/file/2023/1/68101_0s.png)
一篇文章带你深入了解Java封装
2023-11-20 00:37:45
如何通过Android Stduio来编写一个完整的天气预报APP
2023-10-11 17:45:01
![](https://img.aspxhome.com/file/2023/8/96028_0s.png)
浅谈java面向对象中四种权限
2023-03-09 18:43:46
设置Android设备WIFI在休眠时永不断开的代码实现
2022-08-26 09:03:00
SpringBoot2.7 WebSecurityConfigurerAdapter类过期配置
2022-09-23 00:27:32
![](https://img.aspxhome.com/file/2023/8/92708_0s.jpg)
Android 听筒模式的具体实现实例
2023-09-24 03:13:01
一文给你通俗易懂的讲解Java异常
2021-12-20 14:40:56
![](https://img.aspxhome.com/file/2023/4/66694_0s.png)
java selenium 操作弹出窗口示例代码
2021-06-27 05:29:08
Unity的IPostprocessBuildWithReport实用案例深入解析
2022-07-29 23:30:15
Java @Transactional指定回滚条件
2023-11-19 02:47:23
![](https://img.aspxhome.com/file/2023/7/59537_0s.png)
java8学习教程之函数引用的使用方法
2023-08-28 12:03:19
详解Android的网络数据存储
2022-08-22 01:39:39
Kotlin语言使用WebView示例介绍
2021-10-14 05:56:21
![](https://img.aspxhome.com/file/2023/8/138338_0s.jpg)
三种Android单击事件onclick的实现方法
2022-05-21 16:54:03