读取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; ...
随机推荐
- R语言学习 - 热图简化
绘制热图除了使用ggplot2,还可以有其它的包或函数,比如pheatmap::pheatmap (pheatmap包中的pheatmap函数).gplots::heatmap.2等. 相比于gg ...
- CAD得到多行文本(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_GetMTextContent 得到多行文本的,不包括格式控制符中的字符串值,详细说明如下: 参数 说明 LONG lId 多行 ...
- vue基础---事件处理
(1)事件监听 v-on 指令监听 DOM 事件,并在触发时运行JS代码 <div class="event_area"> {{message}} <button ...
- JAVA基础——构造函数方法总结(有参构造和无参构造)
使用构造器时需要记住: 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回 ...
- 移动端响应式rem
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...
- 暴力搜索+散列--P1008 三连击
题目描述 将1,2, ⋯,9共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行,每行3 ...
- 可以通过dict[key]获得dict[value]
dict={key:value,key2:value2} print (dict[key] ) 得到的是 dict[value] # 软文预存接口,通过key来预览未保存的软文,联查商品.kol ...
- 【模板】Lca倍增法
Codevs 1036 商务旅行 #include<cstdio> #include<cmath> #include<algorithm> using namesp ...
- C. Day at the Beach
codeforces 599c C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the be ...
- pyhthon第一个小脚本——文件备份
先说说这个脚本的作用:对指定路径的文件进行压缩备份到另一个指定的路径,并且压缩文件的文件名用当时的日期+时间命名. 先是对着<简明Python教程>上的代码敲的,一堆错误,书上给的是lin ...