使用 Closed XML 导出 .xlsx/.xls

Exporting .xlsx/.xls using Closed XML

我必须将两个列表框导出到 Excel 文件的两列中。我研究了哪种解决方案最合适,然后选择了 ClosedXML。所以我设置了我的保存文件对话框、一个工作簿和一个工作表以便将其导出。在代码的开头,我输入了这一行以使用 ClosedXML Imports ClosedXML.Excel,并且我正在使用以下代码:

Dim t As Integer
t = CInt(Form10.Label13.Text)
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim workbook As New XLWorkbook()
    Dim worksheet = workbook.Worksheets.Add("prova1")
    worksheet.Cells(1, 1).Value = "Time"
    worksheet.Cells(1, 2).Value = "Heat ratio"
    For i = 0 To t
        worksheet.Cells(i + 2, 1).Value = Form10.ListBox1.Items(i)
        worksheet.Cells(1 + 2, 2).Value = Form10.ListBox2.Items(i)
    Next
    workbook.Save()
End If

我返回了四个相同的错误,每个 worksheet.Cells 行输入一个错误。

Error   BC30519 Overload resolution failed because no accessible 'Cells' can be called without a narrowing conversion:
    'Function Cells(usedCellsOnly As Boolean, includeFormats As Boolean) As IXLCells': Argument matching parameter 'usedCellsOnly' narrows from 'Integer' to 'Boolean'.
    'Function Cells(usedCellsOnly As Boolean, includeFormats As Boolean) As IXLCells': Argument matching parameter 'includeFormats' narrows from 'Integer' to 'Boolean'.
    'Function Cells(usedCellsOnly As Boolean, options As XLCellsUsedOptions) As IXLCells': Argument matching parameter 'usedCellsOnly' narrows from 'Integer' to 'Boolean'.
    'Function Cells(usedCellsOnly As Boolean, options As XLCellsUsedOptions) As IXLCells': Argument matching parameter 'options' narrows from 'Integer' to 'XLCellsUsedOptions'.

我不明白错误在哪里。我四处搜索,但在线提供的代码仅适用于 C#。我是一名初学者程序员,也许我在从 C#“翻译”到 VB.net 时做错了。谢谢大家会回答我。最好的问候。

使用 worksheet.Cell(1, 1)(不带 s)访问单个单元格。

使用 Cells() 方法,您可以访问一系列单元格,但为此您需要其他参数,并且没有接受两个整数的重载。错误消息显示该方法的其他重载接受两个参数,但不接受两个整数且无法自动转换。