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. angular当router使用userhash:false时路由404问题

    angular当router使用userhash:false时路由404问题 安装iis urlrewrite2.0组件 在根目录下创建 Web.config <configuration> ...

  2. JAVA判断是否是手机设备访问

    package com.common.util; import java.util.regex.Matcher;import java.util.regex.Pattern; /** * 检测是否为移 ...

  3. datetime模块处理时间

    python常用的处理时间的库有:datetime,time,calendar.datetime库包括了date(储存日期:(年.月.日),time(储存时间:(小时.分.秒和微秒),timedelt ...

  4. redis----------基本命令使用

    1.查看全部缓存数据的key keys * 2.清空当前redis数据库缓存 flushdb  (redis默认由16个库(0~15号). 且默认使用的是0号库.库之间的切换使用select命令例如: ...

  5. 【转】Oracle EBS中查询Profile的各种SQL

    参考 http://blog.csdn.net/pan_tian/article/details/7652968#t0 Using API FND_PROFILE.save to update pro ...

  6. JQuery小知识

    一.禁用鼠标右键 $(document).ready(function() { $(document).bind("contextmenu", function(e) { retu ...

  7. pickel加速caffe读图

    64*64*3小图(12KB),batchSize=128,训练样本100万, 全部load进来内存受不了,load一次需要大半天 训练时读入一个batch,ali云服务器上每个batch读入时间1. ...

  8. TTL与非门电路的工作原理

    分立元件门电路虽然结构简单,但是存在着体积大.工作可靠性差.工作速度慢等许多缺点.1961年美国德克萨斯仪器公司率先将数字电路的元器件和连线制作在同一硅片上,制成了集成电路.由于集成电路体积小.质量轻 ...

  9. (Review cs231n) CNN in Practice

    Make the most of your data Data augmentation 加载图像后,对图像做一些变化,这些变换不改变图像的标签. 通过各种变换人为的增大数据集,可以避免过拟合提高模型 ...

  10. 示例, linq分组

    public class HIS_CLIREGISTER : BaseModel{ private String _FBCODE;[StringLength(8)]/// <summary> ...