将excel中的数据导入到DataTable中
来源:互联网 发布:高频电子线路教材知乎 编辑:程序博客网 时间:2024/05/16 06:50
需要using using System.Data.OleDb;
如果是2007以上的excel文件需要安装AccessDatabaseEngine.exe
下载地址是:http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe
下面是实现过程
public DataTable GetDataFromExcel(string filePath)
{string connStr = "";
string fileType = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(fileType)) return null;
if (fileType == ".xls")
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
else
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\"";
string sql_F = "Select * FROM [{0}] ";
OleDbConnection conn = null;
OleDbDataAdapter da = null;
DataTable dataTable = new DataTable();
try
{
// 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open();
da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand(String.Format(sql_F, "Sheet1$"), conn);
da.Fill(dataTable);
}
catch (Exception ex)
{
}
finally
{ // 关闭连接
if (conn.State == ConnectionState.Open)
{
conn.Close();
da.Dispose();
conn.Dispose();
}
}
conn.Close();
da.Dispose();
conn.Dispose();
return dataTable;
}
0 0
- 使用NPOI将DataTable中的数据导入到Excel中
- 将excel中的数据导入到DataTable中
- 将Excel导入到虚拟DataTable中
- 将DataTable中的数据导入到数据库中
- 将DataTable中的数据导出到Excel中
- C#中如何将DataTable中的数据导出到Excel
- c#将dataTable中的数据导出到Excel表中
- 将DataTable数据导出到Excel中
- 【C#.NET】将excel中的sheet1导入到dataGridView1中 ,或者导入到DataTable,导入到DataSet
- 将EXCEL中的数据导入到DataSet中
- 怎样将Excel中的数据导入到SQLServer2000数据库中
- 怎样将Excel中的数据导入到SQLServer2000数据库中
- c#将datagridview中的数据导入到Excel中(winForm)
- 将DataGrid中的数据完全导入到EXCEL中
- 将excel表中的数据导入到matlab中
- C#实现将DataGridView中的数据导入到Excel中
- 将excel中的数据手工导入到oracle中
- 怎样将excel中的数据导入到oracle数据库中
- 关于屏幕方向的问题总结
- 从穷人到百万富翁之间的七级台阶 你走了几级?
- org.springframework.beans.factory.BeanDefinitionStoreException
- An internal error occurred during: "Computing size". The profile is
- Go语言_接口查询
- 将excel中的数据导入到DataTable中
- warning: Automatic Preferred Max Layout Width before iOS 8.0
- java jms
- 欢迎使用CSDN-markdown编辑器
- 每次返回高度的一半
- 一念永恒 > 第123章 战!
- 怎样通过ZBrush中的DynaMesh创建身体(一)
- /etc/passwd
- 判断jar包中class文件的java编译版本