C#获取Excel里sheet名,其表内容

  1. public static DataTable GetExcelTable(string excelFilename)  
  2. {  
  3.     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);  
  4.     DataSet ds = new DataSet();  
  5.     string tableName;  
  6.     using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))  
  7.     {  
  8.         connection.Open();  
  9.         DataTable table = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);  
  10.         tableName = table.Rows[0]["Table_Name"].ToString();  
  11.         string strExcel = "select * from " + "[" + tableName + "]";  
  12.         OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, connectionString);  
  13.         adapter.Fill(ds, tableName);  
  14.         connection.Close();  
  15.     }  
  16.     return ds.Tables[tableName];  
  17. }
  18.   //VB Code
  19.     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)

转载于:https://www.cnblogs.com/lgc19/archive/2010/11/08/1871853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值