telemaco10399
telemaco10399

Reputation: 39

Exporting .xlsx/.xls using Closed XML

I have to export two Listboxes into two columns of an Excel file. I researched which solution was the most proper and I chose ClosedXML. So I setted my savefiledialog, a workbook and a worksheet in order to export it. At the beginning of the code I typed the line in order to use ClosedXML Imports ClosedXML.Excel and I'm using the following code:

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

I'm getting back four same errors, one for each worksheet.Cells line I typed.

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'.

I can't understand where the error can be. I searched around but provided codes online are only for C#. I'm a beginner programmer and maybe I'm doing wrong while "translating" from C# to VB.net. Thanks all are gonna answer me. Best regards.

Upvotes: 0

Views: 774

Answers (1)

Raidri
Raidri

Reputation: 17550

Use worksheet.Cell(1, 1) (without the s) to access a single cell.

With the Cells() method you can access a range of cells, but for that you need other parameters and there is no overload which accepts two integers. The error message shows other overloads to the method which accept two parameters, but not two integers and no automatic conversion is possible.

Upvotes: 1

Related Questions