如何在具有修复位置的excel文件中导入dataGridView [英] How to import dataGridView in excel file with a fix position

查看:72
本文介绍了如何在具有修复位置的excel文件中导入dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我有一个dataGridView,我想在我的excel文件中导入。但是我希望gridview的位置在第14行第1列的excel文件中。



我有这个代码:

  Dim 作为 整数 =  14  

对于 i = 0 grdvwInterestBreakdown.RowCount - 1
对于 j = 0 grdvwInterestBreakdown.ColumnCount - 1
xlworksheet.Cells(Row,j + 1 )= _
grdvwInterestBreakdown(j, i).Value.ToString()
下一步
行=行+ 1
下一步



但它保持不变返回此错误:

对象引用未设置为对象的实例。



我的代码中是否缺少某些内容或是否有其他方法导入固定位置的数据?



感谢您的帮助。

解决方案

xlworksheet似乎没什么 - 检查两次!


替换:

 grdvwInterestBreakdown(j +  1 ,i +  1 )。Value.ToString()



with:

 grdvwInterestBreakdown(j,i).Value.ToString()





工作表中的单元格索引以1(一)开头,gridview的索引从0(零)开始到行数-1。



我更喜欢使用ADO.NET。看看这里:从.NET应用程序访问Microsoft Office数据 [ ^ ]


  Dim 作为 整数 =  13  

对于 i = 0 grdvwInterestBreakdown.RowCount - 2
对于 j = 0 grdvwInterestBreakdown.ColumnCount - 1
xlworksheet.Cells(Row,j + 1 )= grdvwInterestBreakdown(j,i).Value。 ToString()
下一步
行=行+ 1
下一步





grdvwInterestBreakdown.RowCount - 1 - 返回没有值的最后一行。

如果gridview中有20行,则最后一行中的两行为null,这就是它返回此错误的原因:

对象引用未设置为对象的实例


Hi!

I have a dataGridView which I want to import in my excel file. But I want the position of the gridview at the excel file in Row 14, Column 1.

I have this code:

Dim Row As Integer = 14

For i = 0 To grdvwInterestBreakdown.RowCount - 1
   For j = 0 To grdvwInterestBreakdown.ColumnCount - 1
      xlworksheet.Cells(Row, j + 1) = _
         grdvwInterestBreakdown(j, i).Value.ToString()
   Next
   Row = Row + 1
Next


But it keeps on returning this error:
Object reference not set to an instance of an object.

Is there something missing in my codes or is there another way to import data with fixed position?

Thank you for the help.

解决方案

xlworksheet seems to be Nothing - check it twice!


Replace:

grdvwInterestBreakdown(j + 1, i + 1).Value.ToString()


with:

grdvwInterestBreakdown(j, i).Value.ToString()



Index of cell in Worksheet starts with 1 (one), index of gridview starts with 0 (zero) to the count of rows -1.

I'll prefer to use ADO.NET. Have a look here: Accessing Microsoft Office Data from .NET Applications[^]


Dim Row As Integer = 13

For i = 0 To grdvwInterestBreakdown.RowCount - 2 
   For j = 0 To grdvwInterestBreakdown.ColumnCount - 1
      xlworksheet.Cells(Row, j + 1) = grdvwInterestBreakdown(j, i).Value.ToString()
   Next
   Row = Row + 1
Next



grdvwInterestBreakdown.RowCount - 1 - Returns the last row which has no value.
When you have 20 rows in the gridview, two of the last row is null that is why it returns this error:
Object reference not set to an instance of an object


这篇关于如何在具有修复位置的excel文件中导入dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆