vb.net 检查 Excel 中是否存在 Sheet 并将结果记录为布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21000965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:22:50  来源:igfitidea点击:

Check to see if Sheet exists in Excel and record result as Boolean

vb.netexceloffice-interop

提问by Ahmed Faizan

I am learninf excel to vb.net connection from this site: http://www.siddharthrout.com/2012/09/09/checking-if-a-sheet-exists/

我正在从这个站点学习 excel 到 vb.net 的连接:http: //www.siddharthrout.com/2012/09/09/checking-if-a-sheet-exists/

I am trying to check if sheet existsand record result in boolean.

我正在尝试检查工作表是否存在在 boolean 中记录结果

    Dim SheetNameToCheck As String = "Sheet1"
    Dim xs As Excel.Worksheet
    Dim sheet_found As Boolean
    '~~> Opens an exisiting Workbook. Change path and filename as applicable
    xlWorkBook = xlApp.Workbooks.Open("C:\...\myExcel2007file.xlsx")
    '~~> Display Excel
    xlApp.Visible = True
    '~~> Loop through the all the sheets in the workbook to find if name matches
    For Each xs In xlWorkBook.Sheets
        If xs.Name = SheetNameToCheck Then
            sheet_found = True
        Else
            sheet_found = False
        End If
    Next



    If sheet_found = True Then
        MessageBox.Show("The sheet " & SheetNameToCheck & " found.")
    Else
        MessageBox.Show("Not found.")
    End If

The problem is that the result comes as Not Found whatever string you check with

问题是结果是 Not Found 无论你检查什么字符串

The error comes in the For loop. First the loop checks excel Sheet1 to see if it meets the string to check which is "Sheet1". The variable sheet_found is obviously "True".

错误出现在 For 循环中。首先,循环检查 excel Sheet1 以查看它是否符合要检查哪个是“Sheet1”的字符串。变量 sheet_found 显然是“True”。

But when it goes to the next sheet, Sheet 2 and Sheet3 the result turns to false and I an unable to check if the sheet actually exists in the workbook.

但是当它转到下一张工作表时,工作表 2 和工作表 3 结果变为假,我无法检查工作表是否确实存在于工作簿中。

采纳答案by Patrick

under sheet_found = Trueshould be an Exit Forline before the Elsestatement, as varocarbas suggests

undersheet_found = True应该是语句Exit For之前的一行Else,正如 varocarbas 所建议的

回答by Ahmed Faizan

Thanks to varocarbas and patrick for the answers. Here is the code that worked

感谢 varocarbas 和 patrick 的回答。这是有效的代码

 Dim SheetNameToCheck As String = "Sheet22"
    Dim xs As Excel.Worksheet
    Dim sheet_found As Boolean
    '~~> Opens an exisiting Workbook. Change path and filename as applicable
    xlWorkBook = xlApp.Workbooks.Open("C:\...\myExcel2007file.xlsx")
    '~~> Display Excel
    xlApp.Visible = True
    '~~> Loop through the all the sheets in the workbook to find if name matches

    For Each xs In xlWorkBook.Sheets
        If xs.Name = SheetNameToCheck Then
            sheet_found = True
            Exit For
        Else
            sheet_found = False
        End If
    Next

    If sheet_found = True Then
        MessageBox.Show("The sheet " & SheetNameToCheck & " found.")
    Else
        MessageBox.Show("Not found.")
    End If

回答by Akos

Why bother with that else statement at the first place? Make it false as default, and only change it to true, if you find your sheet.

为什么首先要打扰 else 语句?将其设为默认值,只有在找到工作表时才将其更改为 true。