读取Excel文件到DataTable中
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中的更多相关文章
- 读取excel 文件到datatable
		
上一篇文章介绍了将datatable 内容导出到excel 文件,这里介绍如何将一个excel 文件读取出来,并保持到datatable 中,实际这样的应用场景也是经常遇到的. 这里继续使用了Micr ...
 - C#读取excel数据到datatable中
		
DataTable dtGBPatient = new DataTable(); string strConn;string excelName; //注意:把一个excel文件看做一个数据库,一个s ...
 - 【C#】采用OleDB读取Excel文件转DataTable
		
using System; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using ...
 - 读取Excel文件的两种方法
		
第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...
 - 如何在C#中打开和读取EXCEL文件
		
这篇文章向您展示如何在C#Windows Forms Application中使用ExcelDataReader,ExcelDataReader.DataSet打开和读取Excel文件.创建一个新的W ...
 - 读取Excel文件中的单元格的内容和颜色
		
怎样读取Excel文件中的单元格的内容和颜色 先创建一个Excel文件,在A1和A2中随意输入内容,设置A1的字体颜色为红色,A2的背景为黄色.需要 using Excel = Microsoft.O ...
 - 用python的pandas读取excel文件中的数据
		
一.读取Excel文件 使用pandas的read_excel()方法,可通过文件路径直接读取.注意到,在一个excel文件中有多个sheet,因此,对excel文件的读取实际上是读取指定文件.并 ...
 - C# 读取EXCEL文件的三种经典方法
		
1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { stri ...
 - .Net读取Excel文件时丢失数据的问题 (转载)
		
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
 
随机推荐
- JAVA基础——is-a 、have-a、和 like-a的区别
			
1.is-a,has-a,like-a是什么 在面向对象设计的领域里,有若干种设计思路,主要有如下三种: is-a.has-a.like-a java中在类.接口.抽象类中有很多体现. 了解java看 ...
 - 【计算几何】二维凸包——Graham's Scan法
			
凸包 点集Q的凸包(convex hull)是指一个最小凸多边形,满足Q中的点或者在多边形边上或者在其内.右图中由红色线段表示的多边形就是点集Q={p0,p1,...p12}的凸包. 一组平面上的点, ...
 - <SpringMvc>入门六 异常处理
			
如果不做异常处理,那么一直将错误向上抛出,则会最后在页面上显示错误代码 服务启动后,访问test1方法,页面会报500 为了提示友好的错误页面,所以需要做异常处理 1.编写自定义异常类(做提示信息的) ...
 - pageContext对象的使用及常用方法
			
pageContext对象的使用及常用方法 制作人:全心全意 获取页面上下文的pageContext对象是一个比较特殊的对象,通过它可以获取JSP页面的request.response.session ...
 - 创建sum求多元素的和
			
a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...
 - PAT 1133 Splitting A Linked List
			
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
 - cookie & cookies
			
cookie & cookies "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgq ...
 - HDU 2604 矩阵快速幂
			
题目大意 给定长度为l的只有f,m两种字母 的序列,问不出现fff,fmf的序列个数有多少个 每次的下一个状态都与前一次状态的后两个字母有关 比如我令mm : 0 , mf : 1 , fm : 2 ...
 - [codevs 1482]路线统计(矩阵乘法)
			
题目:http://codevs.cn/problem/1482/ 分析:很像“经过K条边的最短路径条数”.但有所不同,那就是不是边数固定,而是路径总长度固定.看似不能用矩阵乘法了……但注意到每条边的 ...
 - Filter过滤器机制
			
tomcat内部过滤器采用了责任链的设计模式, Tomcat的过滤器主要由Filter.FilterChain组成,FilterChain包含一个Filter数组.当Wrapper执行FilterCh ...