private static string[] GetExcelSheetNames(OleDbConnection conn)
        {
            DataTable dtbSheets = null;
            String[] arrExcelSheets = null;
            using (conn)
            {
                try
                {
                    conn.Open();

// Get the data table containing the schema
                    dtbSheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

if (dtbSheets == null)
                    {
                        return null;
                    }

arrExcelSheets = new String[dtbSheets.Rows.Count];
                    int intI = 0;

// Add the sheet name to the string array.
                    foreach (DataRow dr in dtbSheets.Rows)
                    {
                        arrExcelSheets[intI] = dr["TABLE_NAME"].ToString();
                        intI++;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    // Close the connection
                    conn.Close();
                }
                return arrExcelSheets;
            }
        }

private static DataTable GetDataTableFromXls(OleDbConnection conn, string spreadSheetName)
        {
            DataTable datTemp = null;

using (conn)
            {
                try
                {
                    string strComand = "select * from [" + spreadSheetName + "]";

conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strComand, conn);
                    datTemp = new DataTable(spreadSheetName);
                    adapter.Fill(datTemp);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    // Close the connection
                    conn.Close();
                }
            }
            return datTemp;
        }
        // Get the spreadsheet that contain data
        public static DataTable GetDataTableWithData(string serverLocation)
        {
            OleDbConnection xlsConn = null;
            DataTable datXls = null;

try
            {
                string strConnStr = null;
                // Connection string for Excel
                strConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + serverLocation + ";Extended Properties=\"Excel 8.0; HDR=NO; IMEX=1\"";
                xlsConn = new OleDbConnection(strConnStr);
                // Get Sheet names from an Excel Book
                string[] arrXls = GetExcelSheetNames(xlsConn);

try
                {
                    foreach (string strSheet in arrXls)
                    {
                        xlsConn = new OleDbConnection(strConnStr);
                        datXls = GetDataTableFromXls(xlsConn, strSheet);
                        if (datXls != null && datXls.Rows.Count > 0)
                        {
                            break;
                        }
                    }
                }
                catch// (SqlException se)
                {
                    //throw new Exception(se.Message);
                }

return datXls;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (xlsConn.State == ConnectionState.Open)
                {
                    xlsConn.Close();
                }
                xlsConn.Dispose();
            }
        }

原文转载:http://www.cnblogs.com/moss_tan_jun/archive/2010/08/06/1793747.html

读取Excel文件到DataTable中的更多相关文章

  1. 读取excel 文件到datatable

    上一篇文章介绍了将datatable 内容导出到excel 文件,这里介绍如何将一个excel 文件读取出来,并保持到datatable 中,实际这样的应用场景也是经常遇到的. 这里继续使用了Micr ...

  2. C#读取excel数据到datatable中

    DataTable dtGBPatient = new DataTable(); string strConn;string excelName; //注意:把一个excel文件看做一个数据库,一个s ...

  3. 【C#】采用OleDB读取Excel文件转DataTable

    using System; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using ...

  4. 读取Excel文件的两种方法

    第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...

  5. 如何在C#中打开和读取EXCEL文件

    这篇文章向您展示如何在C#Windows Forms Application中使用ExcelDataReader,ExcelDataReader.DataSet打开和读取Excel文件.创建一个新的W ...

  6. 读取Excel文件中的单元格的内容和颜色

    怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...

  7. 用python的pandas读取excel文件中的数据

    一.读取Excel文件   使用pandas的read_excel()方法,可通过文件路径直接读取.注意到,在一个excel文件中有多个sheet,因此,对excel文件的读取实际上是读取指定文件.并 ...

  8. C# 读取EXCEL文件的三种经典方法

    1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { stri ...

  9. .Net读取Excel文件时丢失数据的问题 (转载)

    相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...

随机推荐

  1. zabbix web监控

    深入浅出Zabbix 3.0 -- 第十章 Web 监控 http://www.mamicode.com/info-detail-1824545.html

  2. 20.IO流部分笔记

    20.IO流部分笔记 2018/09/06 1.IO流  1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...

  3. 洛谷——P3173 [HAOI2009]巧克力

    P3173 [HAOI2009]巧克力 题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短 ...

  4. Re0:DP学习之路 母牛的故事 HDU - 2018

    解法 一定要注意斐波那契数列的原始意义,斐波那契数列也叫作兔子数列是兔子繁衍的一种表示方法.同样适用于别的情况的动物繁衍问题 原始的是3个月一胎现在四个月那么方程就是 f(n)=n n<=4 f ...

  5. CCF201609-2 火车购票 java(100分)

    试题编号: 201609-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一 ...

  6. visioStudio常见问题

    问题一: 在做项目时候,使用VisioStudio 2008,一不小心将设置恢复到了原始,一直找不到需要的东西. 比如生成方式“debug”和“Release”选择框没有.一些图标也没有. 经过不断的 ...

  7. macos-mojave

    macos-mojave https://itunes.apple.com/cn/app/macos-mojave/id1398502828?mt=12

  8. uestc 1903

    #include<stdio.h> int main() { int n,m,i,t; scanf("%d",&t); while(t--){ scanf(&q ...

  9. vue.js中的路由vue-router2.0使用

    在我们平时工作中,我们有时候会有需求,按照不同的规则,加载不同的组件,页面不去跳转,常见的操作是ajax的异步操作,实现局部刷新加载新数据 在vue中,我们写了很多不同的组件,这时候,实现不刷新调用新 ...

  10. 网线切割&切绳子

    题目描述 Wonderland居民决定举行一届地区性程序设计大赛.仲裁委员会志愿负责这次赛事并且保证会组织一次有史以来最公正的比赛.为此,所有参赛者的电脑和网络中心会以星状网络连接,也就是说,对每个参 ...