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的更多相关文章

  1. 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序。解决办法

    在64位服务器系统上,默认不支持Microsoft.Jet.OLEDB.4.0的驱动程序,系统默认会提示未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"的错误 ...

  2. 对路径的访问被拒绝,解决之后又报-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    服务器环境:Server 2008  64位系统 问题:在导入Excel题录表时报错,1对路径的访问被拒绝,2未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案 ...

  3. Microsoft ACE OLEDB 12.0 数据库连接字符串

    Excel 97-2003 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended ...

  4. (转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)

    string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Exten ...

  5. OleDB Destination 用法

    第一部分:简介 OleDB Destination component 是将数据流load 到destination,共有5种Data Access Mode,一般的Destination compo ...

  6. OleDb Source component 用法

    OleDb Source component 主要是从DB中获取数据,传递给下游组件,OleDb Source component的强大之处在于 query data 的mode有四种,如图 Tabl ...

  7. 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 ...

  8. 解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序

    在Windows Server 2008 x64 上部署一个Vs 2008开发的.net2.0 的asp.net web 程序,调用了office的组件来导入导出excel文件,其中托管管道模式为集成 ...

  9. SQL SERVER中的OLEDB等待事件

    OLEDB等待事件介绍 OLEDB等待类型是SQL SERVER 数据库中最常见的几种等待类型之一.它意味着某个会话(SPID)通过SQL Server Native Client OLEDB Pro ...

  10. .NET读取Excel数据,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序

    解决.NET读取Excel数据时,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序的操作: 1. 检查本机是否安装Office Access,如果未安装去去h ...

随机推荐

  1. [js]d3.js绘制拓扑树

    echart也支持拓扑树了 所需的json数据格式: children嵌套 vis.js也支持绘制拓扑树 数据格式: nodes: {id, label, title} edges: {from, t ...

  2. OO第一次博客

    过去的三周里我们完成了表达式求导的程序设计与构造.表达式求导程序,大致思路是实现一个表达式类,支持表达式的输入.求导运算和输出功能.可能的话,还可以增加表达式的化简方法,从而得到更高质量的输出结果.总 ...

  3. 关于网站的一些js和css常见问题的记录

    1. 文字超过宽度,给这个后面超过的文字用...来表示  white-space: nowrap;

  4. 指定时间生成cron表达式

    public class CronUtils { private static final SimpleDateFormat sdf = new SimpleDateFormat("ss m ...

  5. The type com.google.protobuf.GeneratedMessageV3$Builder cannot be resolved. It is indirectly referenced from required .classfiles

    在做项目的时候,导入了几个类,导入了相关的jar. 结果在package处报了The type com.google.protobuf.GeneratedMessageV3$Builder canno ...

  6. KeepAlived+HaProxy+MyCat+Percona双机热备PXC集群

    一.搭建PXC集群 1.环境:centos7+PXC5.7.21+mycat1.6.5 2.卸载mariadb rpm -qa | grep mariadb* yum -y remove mariad ...

  7. Installing ROS Indigo on the Raspberry Pi

    Installing ROS Indigo on the Raspberry Pi Description: This instruction covers the installation of R ...

  8. spring-boot集成activiti的model遇到问题汇总

    按照网上的七拼八凑整合网页版的部署将遇到的问题归置如下: 本人的springboot版本是:1.5.13.RELEASE 工作流相关: <!--工作流--> <dependency& ...

  9. 利用multiprocessing.managers开发跨进程生产者消费者模型

    研究了下multiprocessing.managers,略有收获,随笔一篇: 核心思路是构造一个manager进程,这个进程可以通过unix socket或tcp socket与其它进程通信:因为利 ...

  10. Web Deploy 发布网站错误 检查授权和委派设置

    Web Deploy发布ASP.NET网站给我们提供方便,配置好后可以很方便地发布网站到IIS服务器. 自安装Web Deploy一年以来,一直都用得好好地. 直到最近,Gitlab-CI自动发布出了 ...