使用for each语句导入excel文件 [英] Importing excel file using for each statement

查看:66
本文介绍了使用for each语句导入excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用基于DataGridViewTextBoxColumn标题文本的for each语句导入excel文件。问题是,当我导入文件时,它一直说我在ID列中不能有空值。有趣的是,如果我输入没有for each状态的代码,将每个单独的项表示为自己的对象,我就没有这个问题。



我正在尝试压缩我的代码,因此它更容易阅读,以防万一有未来的问题。



这是我的代码:



I am trying to import an excel file by using a for each statement based upon the DataGridViewTextBoxColumn header text. The problem is that when I import the file it keeps saying that I can not have null values in the "ID" column. Interesting enough, I do not have this problem if I type the code out without the for each state, expressing each individual item as its own object.

I am trying to condense my code so its a lot easier to read, just in case there are future problems.

Here is my code:

For Each item As DataGridViewTextBoxColumn In DataGridView1.Columns

 dtExcelData.Columns.Add(New DataColumn(item.HeaderText, GetType(String)))

 Next





如果有人可以提供这方面的帮助将不胜感激。



我尝试过:



我尝试添加



if anyone can help on this much would be appreciated.

What I have tried:

I have tried adding

If item.HeaderText = "ID" Then
Dim exceldat As New DataColumn("ID", GetType(Integer))
exceldat.AutoIncrement = True
exceldat.AutoIncrementSeed = DataGridView1.RowCount
exceldat.AutoIncrementStep = I
dtExcelData.Columns.Add(exceldat)
End If





在每个声明下但它没有帮助我继续得到同样的错误。



under the for each statement but it hasn't helped and I keep getting the same error.

推荐答案

引用:

如果有人可以提供这方面的帮助,将不胜感激。

if anyone can help on this much would be appreciated.



错误取决于您的Excel工作表,我们无法运行您的代码。

使用调试器检查变量,因为代码执行,1列可能是意外的。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


The error depend on your excel worksheet, we can't run your code.
Use the debugger to inspect variables as your code execute, 1 column is probably an unexpected one.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于使用for each语句导入excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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