C#获取excel中sheel名称
2015-07-24 14:37
429 查看
public static DataTable GetExcelTable(string excelFilename)
{
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=35;Extended Properties=Excel 8.0;Persist Security Info=False",excelFilename);
DataSet ds = new DataSet();
string tableName;
using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))
{
connection.Open();
DataTable table = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
tableName = table.Rows[0]["Table_Name"].ToString();
string strExcel = "select * from " + "[" + tableName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, connectionString);
adapter.Fill(ds, tableName);
connection.Close();
}
return ds.Tables[tableName];
}
//VB Code
Dim connectionString As String '
Used to store the connection string
Dim customerList As New DataSet '
Used to store the temp records readed from the Excel file
Dim excelData As OleDb.OleDbDataAdapter
connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source={0};Extended Properties=""Excel 12.0;HDR=YES"";", fileName)
excelData = New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]",
connectionString)
excelData.TableMappings.Add("Sheet1", "ImportCustomer")
excelData.Fill(customerList)
{
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=35;Extended Properties=Excel 8.0;Persist Security Info=False",excelFilename);
DataSet ds = new DataSet();
string tableName;
using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))
{
connection.Open();
DataTable table = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
tableName = table.Rows[0]["Table_Name"].ToString();
string strExcel = "select * from " + "[" + tableName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, connectionString);
adapter.Fill(ds, tableName);
connection.Close();
}
return ds.Tables[tableName];
}
//VB Code
Dim connectionString As String '
Used to store the connection string
Dim customerList As New DataSet '
Used to store the temp records readed from the Excel file
Dim excelData As OleDb.OleDbDataAdapter
connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source={0};Extended Properties=""Excel 12.0;HDR=YES"";", fileName)
excelData = New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]",
connectionString)
excelData.TableMappings.Add("Sheet1", "ImportCustomer")
excelData.Fill(customerList)
相关文章推荐
- .Net Stack 技术栈
- C#关键字列表
- C#运算符优先级
- 用C#写一个实现进程监控的自动关机工具
- c# select标签绑定枚举,并以Description做Text显示
- C#学习笔记(十五):预处理指令
- C# 抽象工厂模式
- c#读取xml文档
- C#中??和?
- C#基础系列——小话泛型
- C# webbrowser实现真正意义上的F5刷新
- C#值传递和按引用传递
- windows服务
- High-Performance Timer in C#
- C# 程序控制
- C#验证字符串是否是数字、传真、邮政编码、网址或者电子邮件
- C#6.0语法糖
- hihoCoder #1051 补提交卡
- C#学习笔记——控件篇
- C# inline-hook / api-hook