簡體   English   中英

如何使用索引C#獲取Excel工作表名稱

[英]how to get excel sheet name using index c#

我正在嘗試導入多個excel workbook並將其綁定到datagridview ,如下圖所示,我的問題是我想使用索引獲取工作表名稱以使此方法可修復,但出現錯誤提示

我嘗試零索引我也得到錯誤無效索引。 (來自HRESULT的異常:0x8002000B(DISP_E_BADINDEX))

請注意 ,Excel工作簿可能有一張或多張紙

未處理System.Runtime.InteropServices.COMException
消息=無效的索引。 (來自HRESULT的異常:0x8002000B(DISP_E_BADINDEX))源= Microsoft.Office.Interop.Excel
ErrorCode = -2147352565 StackTrace:位於d:\\ Documents \\ Visual Studio 2013 \\ Projects \\ BlackList \\ BlackList \\ F0100中BlackList.F0100.ImportExcel(String FilePath)處的Microsoft.Office.Interop.Excel.Sheets.get_Item(Object Index)。 d:\\ Documents \\ Visual Studio 2013 \\ Projects \\ BlackList \\ BlackList \\ F0100.cs:BlackList.F0100.GetFilesList()處的cs行88:d中BlackList.F0100.B_Import_Data_Click(Object sender,EventArgs e)中的第71行: \\ Documents \\ Visual Studio 2013 \\ Projects \\ BlackList \\ BlackList \\ F0100.cs:System 125在System.Windows.Forms.Control.OnClick(EventArgs e)在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)在行。 Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按鈕,Int32單擊)在System.Windows.System.Windows.Forms.ButtonBase.WndProc(Message&m)在System.Windows.Forms.ButtonBase.WndProc(Message&m)。 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)(System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int3)處的Forms.Button.WndProc(Message&m) System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)的System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG和msg)的2 msg,IntPtr wparam,IntPtr lparam)原因,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:System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)中的第18行,Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart()處處於System.Threading.ExecutionContext.Run(ExecutionContext執行上下文,ContextCallback回調,對象狀態)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");

    }

我認為,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM