vb.net - Excel Export exporting same row multiple times -


i've written export procedure vb.net application, exports data displayed in datagridviewinto excel spreadsheet.

however, instead of copying 23 of rows in datagridview, copies same one, 23 times. wrong below for loopthat means copies 1 record?

for = 0 dgvexport.rowcount - 1  j = 0 dgvexport.columncount - 1   k integer = 1 dgvexport.columns.count       xlworksheet.cells(1, k) = dgvexport.columns(k - 1).headertext       xlworksheet.cells(i + 2, j + 1) = dgvexport(j, 1).value.tostring   next  next next 

i need field names copied, hence headertext line , k variable.

a snippet of dgv, show there different records; enter image description here

the resultant export;

enter image description here

edit

as suggested, changed code following

 k integer = 1 dgvexport.columns.count   xlworksheet.cells(1, k) = dgvexport.columns(k - 1).headertext  next   = 0 dgvexport.rowcount - 1   j = 0 dgvexport.columncount - 1    xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring   next  next 

but error

object reference not set instance of object

on line xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring

the original problem of duplicate rows this:

for = 0 dgvexport.rowcount - 1     j = 0 dgvexport.columncount - 1        k integer = 1 dgvexport.columns.count 

the cells being iterated many times. use 1 loop export headertext, data.


object reference not set instance of object

aka nullreferenceexception

if allowusertoaddrows true on dgv, means looping 1 many rows. cells in row nothing, nullreferenceexception:

 = 0 dgvexport.rowcount - 1      if dgvexport.rows(i).isnewrow continue      j = 0 dgvexport.columncount - 1          xlworksheet.cells(i + 3, j + 1) = dgvexport(j, i).value.tostring      next  next 

or change row loop:

' 1 less account new user row = 0 dgvexport.rowcount - 2 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

Laravel mail error `Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]` -

c# SetCompatibleTextRenderingDefault must be called before the first -