如何使用索引c#获取Excel工作表名称

时间:2018-02-11 15:48:59

标签: c# .net visual-studio-2013 .net-3.5

我正在尝试导入多个excel workbook并将其绑定到datagridview 如下图所示我的问题是我想使用索引来获取工作表名称 这个方法可修复我收到的错误是

我试过零索引我也得到错误索引无效。 (来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX))

注意 excel工作簿可能包含一个或多个工作表

  

System.Runtime.InteropServices.COMException未处理
  消息=索引无效。 (HRESULT异常:0x8002000B   (DISP_E_BADINDEX))Source = Microsoft.Office.Interop.Excel
  ErrorCode = -2147352565 StackTrace:          在Microsoft.Office.Interop.Excel.Sheets.get_Item(对象索引)          在d:\ Documents \ Visual Studio中的BlackList.F0100.ImportExcel(String FilePath)   2013 \ Projects \ BlackList \ BlackList \ F0100.cs:第88行          位于d:\ Documents \ Visual Studio 2013 \ Projects \ BlackList \ BlackList \ F0100.cs中的BlackList.F0100.GetFilesList():第71行          在d:\ Documents \ Visual Studio中的BlackList.F0100.B_Import_Data_Click(Object sender,EventArgs e)   2013 \ Projects \ BlackList \ BlackList \ F0100.cs:第125行          在System.Windows.Forms.Control.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息& m)          在System.Windows.Forms.ButtonBase.WndProc(消息& m)          在System.Windows.Forms.Button.WndProc(消息& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32)   dwComponentID,Int32原因,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32)   原因,ApplicationContext上下文)          在BlackList.Program.Main()的d:\ Documents \ Visual Studio 2013 \ Projects \ BlackList \ BlackList \ Program.cs:第18行          在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()InnerException:

    public void ImportExcel(string FilePath)
    {
        string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
        string cmdtxt = @"select * from [Sheet222$]";

        /////////////////////////////////////////////////////////////////////////////////
        Excel.Application excelApp = new Excel.Application();
        excelApp.Visible = true;

        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(FilePath,
                0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);

        Excel.Sheets excelSheets = excelWorkbook.Worksheets;

        Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(0);

        MessageBox.Show(excelWorksheet.Name, "test msg");

        excelWorkbook.Close(0);

        /////////////////////////////////////////////////////////////////////////////////

        using (OleDbConnection conn = new OleDbConnection(ConnStr))
        {
            conn.Open();

            OleDbDataAdapter DA = new OleDbDataAdapter(cmdtxt, conn);
            DA.Fill(dt);
            DGV_Data.DataSource = dt;

            conn.Close();
        }

        //Calculate record counts
        L_Rows_Count.Text = "count: " + (DGV_Data.Rows.Count - 1).ToString("n0");

    }

1 个答案:

答案 0 :(得分:1)

我想,

Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);
            string name = excelWorksheet.Name;//Sheet Name