C#读取Excel多个sheet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
         //读取Excel中多个表的名字
            string SourceExcelPath = "E:\\C# Projects\\ConsoleApplication1\\ForTesting2.xlsx";
            string StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;"+" Data Source=" + SourceExcelPath + ";"+"Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";//路径的正确性
            OleDbConnection ExcelConn = new OleDbConnection(StrConn);
            ExcelConn.Open();//打开连接
          //获取列表信息
            System.Data.DataTable SheetNames = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null,null,null,"Table"});//获取列表名称
            string []TableNames=new string[SheetNames.Rows.Count];
            for (int k = 0; k < SheetNames.Rows.Count;++k )
            {
                TableNames[k]=SheetNames.Rows[SheetNames.Rows.Count-k-1]["TABLE_NAME"].ToString();//遍历
                Console.WriteLine(TableNames[k]);
            }         
        }
    }
}

你可能感兴趣的:(EXCEL)