您的位置:首页 > 数据库

.net 中读取Excel2007的方法(服务器不装office2007) for ExcelToSQL

2012-01-18 14:17 447 查看
在ExcelToSQL这个小程序里面客户要求做到不需要安装office的软件就能读取Excel里面的内容,经过一番折腾找到以下的方法。

http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=7554f536-8c28-4598-9b72-ef94e038c891&displayLang=en

先到微软官方下载ACE OLEDB 12.0,如果连接失效的话直接在官方的select里搜索 “ACE OLEDB 12.0”

然后使用oledbconnection的方法读取数据,链接字串如下

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0
Xml;HDR=YES";

获取excel文件第1个sheet的名称的函数

string tableName = null;

//Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +

"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))

{

conn.Open();

DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

tableName = dt.Rows[0][2].ToString().Trim();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: