从C#的DataTable获取单元格值 [英] Get Cell Value from a DataTable in C#

查看:2403
本文介绍了从C#的DataTable获取单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个 DataTable的DT ,里面有大量的数据。



我要得到具体的单元格值从数据表,说的细胞[I,J] 。其中,
I - >行j - >列。我将遍历I,J与价值两个 forloops



但我想不通我怎么能叫通过其索引细胞



下面的代码:

 的(I = 0;我LT&= dt.Rows.Count  -  1;我++)
{
为(J = 0; J< = dt.Columns.Count - 1; J ++)
{
细胞无功= dt.Rows [I] [J]。
xlWorkSheet.Cells第[i + 1,J + 1] =细胞;
}
}


解决方案

的DataRow 也有一个索引

 对象cellValue = dt.Rows [I] [J]。 



但我宁愿强类型的 字段 扩展方法,这也支持的可空类型的:

  INT数= dt.Rows [I]与技术领域下,INT>(J); 



甚至更好的可读性和更不容易出错的列的名称:

 双otherNumber = dt.Rows [I]与技术领域下,双>(DoubleColumn); 


Here is a DataTable dt, which has lots of data.

I want to get the specific Cell Value from the DataTable, say Cell[i,j]. Where, i -> Rows and j -> Columns. I will iterate i,j's value with two forloops.

But I can't figure out how I can call a cell by its index.

Here's the code:

for (i = 0; i <= dt.Rows.Count - 1; i++)
        {
            for (j = 0; j <= dt.Columns.Count - 1; j++)
            {
                var cell = dt.Rows[i][j];
                xlWorkSheet.Cells[i + 1, j + 1] = cell;
            }
        }

解决方案

The DataRow has also an indexer:

Object cellValue = dt.Rows[i][j];

But i would prefer the strongly typed Field extension method which also supports nullable types:

int number = dt.Rows[i].Field<int>(j);

or even more readable and less error-prone with the name of the column:

double otherNumber = dt.Rows[i].Field<double>("DoubleColumn");

这篇关于从C#的DataTable获取单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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