读取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; ...
随机推荐
- CAD如何动态绘制带面积周长的圆?
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- 出现For input string: "" 错误
然后是因为后台生成的是一个数组,前台取的是一个对象,所以会产生这个错误 前后台交互时 mv.addObject("vo1",fhList.get(0));}将数组改成fhList. ...
- Leetcode724:寻找数组的中心索引(java、python3)
寻找数组的中心索引 给定一个整数类型的数组 nums,请编写一个能够返回数组"中心索引"的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相 ...
- Python基础之简介
参考原文 廖雪峰Python教程 什么是Python? Python是一种计算机程序设计语言,又被称为胶水语言,它是高级的编程语言. Python能干什么? 网站后端程序员.自动化运维.数据分析师.游 ...
- LeetCode_16 3SumCloest
3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...
- 洛谷——P2659 美丽的序列
P2659 美丽的序列 单调栈维护区间最小值,单调递增栈维护区间最小值, 考虑当前数对答案的贡献,不断加入数,如果加入的数$>$栈顶,说明栈顶的元素对当前数所在区间是有贡献的,同时加入当前的数. ...
- Codeforces 263B. Appleman and Card Game
B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
- java方法的虚分派和方法表
java:方法的虚分派(virtual dispatch)和方法表(method table) Java方法调用的虚分派 虚分配(Virtual Dispatch) 首先从字节码中对方法的调用说起.J ...
- HDU 4903 (模拟+贪心)
Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...
- Linux下汇编语言学习笔记75 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...