文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Excel2003和excel2007读取方法

Excel2003和excel2007读取方法

时间:2010-12-09  来源:zuo777

如出现此错误信息:未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 。

 请下载http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe 安装

01 public static DataSet ReadExcel_ds(string FilePath, string companycd)
02  
03        {
04  
05             
06  
07            string subfile = FilePath.Substring(FilePath.LastIndexOf(".") + 1);
08  
09            DataSet ds = new DataSet();
10  
11            string strCon = "";
12  
13             if (subfile.ToUpper() == "XLS")
14  
15             {
16  
17                  strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + FilePath;
18  
19                                  
20  
21             }
22  
23             if (subfile.ToUpper() == "XLSX")//excel2007读取
24  
25             {
26  
27                  strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
28  
29                  
30  
31             }
32  
33              
34  
35             System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
36  
37             //string strCom = "SELECT * FROM [Sheet1$]";
38  
39             Conn.Open();
40  
41             System.Data.DataTable sTable = Conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);//新添加 2010-10-14
42  
43             string tableName = sTable.Rows[0][2].ToString().Trim();//新添加 2010-10-14  获取工作表名称
44  
45             string strCom = "SELECT * FROM [" + tableName + "]";
46  
47             System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
48  
49   
50  
51             myCommand.Fill(ds, "[" + tableName + "]");
52  
53             Conn.Close();
54  
55             return ds;
56  
57        }
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载