C#将Excel中的数据转换成DataSet
作者:hebedich 时间:2021-10-29 18:20:55
使用C#在不借助第三方插件的情况下将Excel中的数据转换成DataSet
/// <summary>
/// EXCEL数据转换DataSet
/// </summary>
/// <param name="filePath">文件全路径</param>
/// <param name="search">表名</param>
/// <returns></returns>
private DataSet GetDataSet(string fileName)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";
OleDbConnection objConn = null;
objConn = new OleDbConnection(strConn);
objConn.Open();
DataSet ds = new DataSet();
//List<string> List = new List<string> { "收款金额", "代付关税", "垫付费用", "超期", "到账利润" };
List<string> List = new List<string> { };
DataTable dtSheetName = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in dtSheetName.Rows)
{
if (dr["Table_Name"].ToString().Contains("$") && !dr[2].ToString().EndsWith("$"))
{
continue;
}
string s = dr["Table_Name"].ToString();
List.Add(s);
}
try
{
for (int i = 0; i < List.Count; i++)
{
ds.Tables.Add(List[i]);
string SheetName = List[i];
string strSql = "select * from [" + SheetName + "]";
OleDbDataAdapter odbcCSVDataAdapter = new OleDbDataAdapter(strSql, objConn);
DataTable dt = ds.Tables[i];
odbcCSVDataAdapter.Fill(dt);
}
return ds;
}
catch (Exception ex)
{
return null;
}
finally
{
objConn.Close();
objConn.Dispose();
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:C#,Excel,DataSet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现支付宝退款功能
2021-09-25 14:00:00
![](https://img.aspxhome.com/file/2023/0/62240_0s.jpg)
BeanDefinitionRegistryPostProcessor如何动态注册Bean到Spring
2023-11-24 12:56:16
![](https://img.aspxhome.com/file/2023/1/59941_0s.jpg)
C#简单实现SNMP的方法
2021-11-25 18:42:38
Android SurfaceView预览变形完美解决方法
2021-11-14 10:29:22
java String的intern方法
2021-07-05 03:23:52
Android Studio和阿里云数据库实现一个远程聊天程序
2023-06-14 21:09:16
![](https://img.aspxhome.com/file/2023/4/92614_0s.jpg)
Android调用前后摄像头同时工作实例代码
2021-09-10 05:34:45
![](https://img.aspxhome.com/file/2023/9/101829_0s.png)
DataGridView控件显示行号的正确代码及分析
2022-07-26 22:05:50
C# Partial:分部方法和分部类代码实例
2022-04-07 11:28:26
拉钩网java笔试题分享
2022-02-13 08:48:25
Spring Boot如何通过自定义注解实现日志打印详解
2022-07-04 14:10:18
![](https://img.aspxhome.com/file/2023/1/78891_0s.png)
MybatisX-Generator自动代码生成插件教程
2022-01-08 10:50:48
![](https://img.aspxhome.com/file/2023/7/67677_0s.png)
Mybatis注解实现多数据源读写分离详解
2021-12-15 21:44:16
![](https://img.aspxhome.com/file/2023/1/62211_0s.jpg)
Spring Boot 中application.yml与bootstrap.yml的区别
2023-07-09 12:30:56
Java多态和实现接口的类的对象赋值给接口引用的方法(推荐)
2023-11-26 11:59:41
记一次公司JVM堆溢出抽丝剥茧定位的过程解析
2023-11-09 13:11:24
![](https://img.aspxhome.com/file/2023/7/59007_0s.jpg)
教你用Java在个人电脑上实现微信扫码支付
2023-07-22 20:52:15
![](https://img.aspxhome.com/file/2023/2/57722_0s.png)
简单了解Java方法的定义和使用实现详解
2023-10-30 16:12:46
Android TV 3D卡片无限循环效果
2022-03-26 04:06:48
SpringBoot yaml中的数组类型取值方式
2022-10-14 20:05:36