【问题标题】:how to get excel sheet name using index c#如何使用索引c#获取excel工作表名称
【发布时间】:2018-07-21 20:18:34
【问题描述】:

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

我尝试了零索引我也得到错误无效索引。 (HRESULT 异常:0x8002000B (DISP_E_BADINDEX))

注意excel工作簿可能有一张或多张表格

System.Runtime.InteropServices.COMException 未处理
消息=无效索引。 (来自 HRESULT 的异常:0x8002000B (DISP_E_BADINDEX)) 来源=Microsoft.Office.Interop.Excel
错误代码=-2147352565 堆栈跟踪: 在 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:line 71 中的 BlackList.F0100.GetFilesList() 在 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 事件) 在 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(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 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 上下文) 在 d:\Documents\Visual Studio 2013\Projects\BlackList\BlackList\Program.cs:line 18 中的 BlackList.Program.Main() 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 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 处没有工作表(索引集合为 zero-based)。此外,关闭工作簿并随后使用已关闭工作簿中的工作表(如MessageBox.Show)将失败。您想要的属性是Worksheet 对象的Name 属性。
  • @dlatikay 我忘了提到我尝试了零索引我也得到了错误Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))...发布更新

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


【解决方案1】:

我想,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多