简体   繁体   English

VB.NET Gembox-切换活动电子表格

[英]VB.NET Gembox - Switch Active Spreadsheet

I've looked all over for this and think I'm just hitting a mental brick wall for something simple. 我到处都在寻找,并认为我只是为了一些简单的事情而碰上了精神障碍。 But still. 但是还是。 I'm writing a quick program to help me with some mileage spreadsheets for different vehicle. 我正在编写一个快速程序,以帮助我处理一些针对不同车辆的里程电子表格。 Each vehicle has its on worksheet within the spreadsheet, I'm using GemBox in VB.net. 电子表格中的每辆车都有其工作表,我正在VB.net中使用GemBox。

Basically, dending on which button you press it chooses the correct sheet for the corresponding vehicle. 基本上,在按下哪个按钮时,它会为相应的车辆选择正确的纸张。 I cannot find anything, anywhere that tells me how to choose a different existing sheet as the active worksheet. 我什么都找不到,告诉我如何选择其他现有工作表作为活动工作表。

This is my test code atm. 这是我的测试代码atm。

Public Sub SetMiles(vehicle As String)

    Dim wb = ExcelFile.Load(file)
    Dim ws = wb.Worksheets.ActiveWorksheet(vehicle) 

    loc = "F12"
    ws.Cells(loc).Value = "800"


End Sub

In GemBox.Spreadsheet you don't need to set the sheet as active in order to use it. 在GemBox.Spreadsheet中,您无需将工作表设置为活动状态即可使用它。

In other words, let's say you have an Excel file which has "Sheet1" and "Sheet2" . 换句话说,假设您有一个包含"Sheet1""Sheet2"的Excel文件。 To write into those sheets you can use the following: 要写到这些表中,可以使用以下命令:

Dim wb = ExcelFile.Load(File)

Dim ws = wb.Worksheets("Sheet1")
ws.Cells("A1").Value = "Foo"

ws = wb.Worksheets("Sheet2")
ws.Cells("A1").Value = "Bar"

You can also use the following: 您还可以使用以下内容:

Dim wb = ExcelFile.Load(File)

Dim ws = wb.Worksheets(0)
ws.Cells("A1").Value = "Foo"

ws = wb.Worksheets(1)
ws.Cells("A1").Value = "Bar"

So, I believe that what you need is the following: 因此,我相信您所需要的是以下内容:

Public Sub SetMiles(vehicle As String)

    Dim wb = ExcelFile.Load(File)
    Dim ws = wb.Worksheets(vehicle)

    Loc = "F12"
    ws.Cells(Loc).Value = "800"

End Sub

Last, in case you do need to set some sheet as active, then you can do that with GemBox.Spreadsheet as following: 最后,如果确实需要将某些工作表设置为活动表,则可以使用GemBox.Spreadsheet进行以下操作:

wb.Worksheets.ActiveWorksheet = wb.Worksheets(vehicle)

However, again GemBox.Spreadsheet doesn't care if the sheet is active or not, you can access and modified it regardless of that. 但是,同样,GemBox.Spreadsheet不在乎工作表是否处于活动状态,无论如何都可以对其进行访问和修改。 By setting the sheet as active, that sheet will be the first visible one that you see when you open that file in an Excel application. 通过将工作表设置为活动状态,该工作表将成为在Excel应用程序中打开该文件时看到的第一个可见工作表。

wb.Sheets(vehicle).Activate is the simplest way. wb.Sheets(vehicle).Activate是最简单的方法。

Although I recommend that you also validate the vehicle string to ensure that Sheet actually exists. 尽管我建议您也验证vehicle字符串,以确保Sheet确实存在。 You can then either ignore, display a message or create a new sheet. 然后,您可以忽略,显示消息或创建新工作表。

I was assuming that you wanted to activate the sheet so that the user can do manual input. 我假设您想激活工作表,以便用户可以进行手动输入。 If you are doing automated input (no user interaction), then you are better off not activating the sheet. 如果您正在执行自动输入(无用户交互),那么最好不要激活工作表。 Something along the lines of: 类似于以下内容:

Public Sub SetMiles(vehicle As String, wb as Workbook, loc as string, Mileage as string)
' passing the values is better encapsulation for OOP purposes
' in your example above, loc="F12", Mileage = "800"
' You have passed the Mileage as a string - but you could also use a numeric value.

' Validate sheet name here if important.
' Validate range name here if important.
    wb.Sheets(vehicle).Range(loc).Value = Mileage

End Sub

Edit: Appears GemBox uses Worksheets instead of Sheets . 编辑:出现GemBox使用Worksheets而不是工作Sheets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM