简体   繁体   English

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

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

I am trying to import multiple excel workbook and bind it to datagridview as shown below my problem is I want to get sheet name using index to make this method fixable I am getting error that says 我正在尝试导入多个excel workbook并将其绑定到datagridview ,如下图所示,我的问题是我想使用索引获取工作表名称以使此方法可修复,但出现错误提示

I tried zero index I am get error too Invalid index. 我尝试零索引我也得到错误无效索引。 (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) (来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX))

Note that excel workbook may has one or more sheets 请注意 ,Excel工作簿可能有一张或多张纸

System.Runtime.InteropServices.COMException was unhandled 未处理System.Runtime.InteropServices.COMException
Message=Invalid index. 消息=无效的索引。 (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) Source=Microsoft.Office.Interop.Excel (来自HRESULT的异常:0x8002000B(DISP_E_BADINDEX))源= Microsoft.Office.Interop.Excel
ErrorCode=-2147352565 StackTrace: at Microsoft.Office.Interop.Excel.Sheets.get_Item(Object Index) at BlackList.F0100.ImportExcel(String FilePath) in d:\\Documents\\Visual Studio 2013\\Projects\\BlackList\\BlackList\\F0100.cs:line 88 at BlackList.F0100.GetFilesList() in d:\\Documents\\Visual Studio 2013\\Projects\\BlackList\\BlackList\\F0100.cs:line 71 at BlackList.F0100.B_Import_Data_Click(Object sender, EventArgs e) in d:\\Documents\\Visual Studio 2013\\Projects\\BlackList\\BlackList\\F0100.cs:line 125 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int3 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) 2 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at BlackList.Program.Main() in d:\\Documents\\Visual Studio 2013\\Projects\\BlackList\\BlackList\\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: 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");

    }

I think, 我认为,

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