Home > Blockchain >  Export DataGridView into Excel using EPPlus Error property 'item' is 'read only'
Export DataGridView into Excel using EPPlus Error property 'item' is 'read only'

Time:06-28

i want to ask about Exporting datagridview into excel using EPPlus VB.NET.

i using microsoft.interop before this to export datagridview into excel and its work after several month i can't use microsoft.interop because it keep retrieving COM error suddenly so i change my code and start using EPPlus like others.

But i can't export the whole datagridview into excel because an error that tell me that property 'item' is 'read only' and Value of type 'String' cannot be converted to 'ExcelRange'

            With worksheetData
                For Each column As DataGridViewColumn In DataGridView1.Columns
                    .Cells(1, column.Index   1) = column.HeaderText
                Next
                For i = 1 To Me.DataGridView1.RowCount
                    .Cells(i   1, 1) = Me.DataGridView1.Rows(i - 1).Cells("EENo").Value
                    For j = 1 To DataGridView1.Columns.Count - 1
                        .Cells(i   1, j   1) = DataGridView1.Rows(i - 1).Cells(j).Value
                    Next
                Next
            End With

this is my coding that i try to export the whole datagridview into excel.

the error is at line 3,6 and 8.

i don't know if it about my coding or about properties at design part.

please help me, Thank You.

CodePudding user response:

My understanding is that EPPLus is free for non-commercial use.

The problem and error you are getting is because the code is not fully qualifying “what” it wants to put in the Excel Cell. When you use the code…

 .Cells(i   1, j   1) = DataGridView1.Rows(i - 1).Cells(j).Value

The left side of the equals is an “Excel Cell”…

.Cells(i   1, j   1) = …

and to add a “Value” to it … you need to add the .Value property. Something like..

.Cells(1, column.Index   1).Value = column.HeaderText

And ….

.Cells(i   1, j   1).Value = DataGridView1.Rows(i - 1).Cells(j).Value

After these changes the code works as expected.

  • Related