查找行中包含一个值的列的索引 [英] Find Index of a Column that Contains a Value in a Row

查看:70
本文介绍了查找行中包含一个值的列的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从excel导入的数据表.因为它如何被进口我需要基于第一行的条目名称的列.

I have a datatable that was imported from excel. Because of how it gets imported I need to Name the Columns based on the entries on the first row.

foreach (DataColumn c in spreadsheet.WorkbookData.Tables[0].Columns)
            {

                string cellValue = spreadsheet.WorkbookData.Tables[0].Rows[0][c].ToString();
                spreadsheet.WorkbookData.Tables[0].Columns[c.ToString()].ColumnName = cellValue;
            }


这很好用,但是当到达某个点时,列名将开始重复.我想打它,所以我只是做了一个M_" + cellValue当我来到那里开始重复.我知道它从哪里开始重复(我可以打开excel文件),但是我希望能够基于搜索找到该点(在这种情况下,是第二次出现"EDI_Std_Nomenclature"),因此用户可以在以下情况下更新该表:


This works great, but when it hits a certain point the column names would start repeating. I want to make it so I just do a "m_" + cellValue when I come to where it starts repeating. I know where it starts repeating(I can open the excel file), but I want to be able to find that point based on a search(in this case at the second occurrence of "EDI_Std_Nomenclature") so the user could update the table if it ever changed.

推荐答案

如果我理解正确的问题,您可以尝试spreadsheet.WorkbookData.Tables[0].Columns[c.ToString()].Ordinal,它返回列的位置
If I understood the question right, you might try spreadsheet.WorkbookData.Tables[0].Columns[c.ToString()].Ordinal, which returns the position of the column


这篇关于查找行中包含一个值的列的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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