oledb
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace ReadExcel
{
class Program
{
static void Main(string[] args)
{
string file = @"D:\tmp\Store 29-09-15.xlsx";
var dataSet = GetDataSetFromExcelFile(file);
Console.WriteLine(string.Format("reading file: {0}", file));
Console.WriteLine(string.Format("coloums: {0}", dataSet.Tables[0].Columns.Count));
Console.WriteLine(string.Format("rows: {0}", dataSet.Tables[0].Rows.Count));
Console.ReadKey();
}
private static string GetConnectionString(string file)
{
Dictionary<string, string> props = new Dictionary<string, string>();
string extension = file.Split('.').Last();
if (extension == "xls")
{
//Excel 2003 and Older
props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
props["Extended Properties"] = "Excel 8.0";
}
else if (extension == "xlsx")
{
//Excel 2007, 2010, 2012, 2013
props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
props["Extended Properties"] = "Excel 12.0 XML";
}
else
throw new Exception(string.Format("error file: {0}", file));
props["Data Source"] = file;
StringBuilder 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();
}
private static DataSet GetDataSetFromExcelFile(string file)
{
DataSet ds = new DataSet();
string connectionString = GetConnectionString(file);
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;
}
}
}
oledb的更多相关文章
- 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序。解决办法
在64位服务器系统上,默认不支持Microsoft.Jet.OLEDB.4.0的驱动程序,系统默认会提示未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"的错误 ...
- 对路径的访问被拒绝,解决之后又报-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。
服务器环境:Server 2008 64位系统 问题:在导入Excel题录表时报错,1对路径的访问被拒绝,2未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案 ...
- Microsoft ACE OLEDB 12.0 数据库连接字符串
Excel 97-2003 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended ...
- (转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...
- OleDB Destination 用法
第一部分:简介 OleDB Destination component 是将数据流load 到destination,共有5种Data Access Mode,一般的Destination compo ...
- OleDb Source component 用法
OleDb Source component 主要是从DB中获取数据,传递给下游组件,OleDb Source component的强大之处在于 query data 的mode有四种,如图 Tabl ...
- csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
ODP.NET: 引用: using Oracle.DataAccess; //Oracle g 11.2.0 using Oracle.DataAccess.Client; using Oracle ...
- 解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
在Windows Server 2008 x64 上部署一个Vs 2008开发的.net2.0 的asp.net web 程序,调用了office的组件来导入导出excel文件,其中托管管道模式为集成 ...
- SQL SERVER中的OLEDB等待事件
OLEDB等待事件介绍 OLEDB等待类型是SQL SERVER 数据库中最常见的几种等待类型之一.它意味着某个会话(SPID)通过SQL Server Native Client OLEDB Pro ...
- .NET读取Excel数据,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
解决.NET读取Excel数据时,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序的操作: 1. 检查本机是否安装Office Access,如果未安装去去h ...
随机推荐
- H3C交换机引发的奇葩故障
设备:H3C S5120-28P-SI 故障:某个交换机的接口速率只有100Mbps. 描述:这个故障还是很特别的,因为按普通的测试办法很难第一时间判断是交换机的固件问题,我也是做了几乎所有外围设备和 ...
- vue js判断长按触发及手指的上滑、下滑、左滑、又滑
<span class="btn" @touchstart="touchstart()" @touchmove="touchmove()&quo ...
- spring batch (一) 常见的基本的概念介绍
SpringBatch的基本概念介绍 内容来自<Spring Batch 批处理框架>,作者:刘相. 一.配置文件 在项目中使用spring batch 需要在配置文件中声明: 事务管理器 ...
- JavaScript实现RSA加解密
在GitHub上找到jsencrypt.js对RSA加解密的工具文件,地址分别是:https://github.com/travist/jsencrypt和https://github.com/ope ...
- windows 下面make的使用示例
---恢复内容开始--- 前面已经安装了windows下面的编译器g++和mingw32-make,下面就make做个示例说明 1.文档结构 |--src |--comm ...
- 利用FPN构建Faster R-CNN检测
FPN就是所谓的金字塔结构的检测器,(Feature Pyramid Network) 把FPN融合到Faster rcnn中能够很大程度增加检测器对全图信息的认知, 步骤如图所示: 1.先将图像送入 ...
- 我与C++的初识
Q1:学习<C++语言程序设计>课程之前,你知道什么是编程吗?谈谈上这门课之前你对编程的理解,以及你对自己编程能力的评估. A1:在学习<C++语言程序设计>课程之前,我其实对 ...
- django请求接收及文件上传
在写后端交互页面的时候常常会遇到接收来自前端页面请求的情况,例如 在写注册页面的时候,会提交一些页面信息,这时需要分三种情况讨论 第一种,接收单项信息: v = request.POST.getlis ...
- Python练习:初别Pandas
# Pandas安装- Anaconda 安装: conda install pandas 或者pip install pandas 参考 http://pandas.pydata.org/ ## S ...
- day19 python之re模块正则练习
1.匹配标签 import re ret = re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>" ...