简体   繁体   English

使用 DataTable C# 获取列名称的索引到 Map

[英]Get Index of Column Name to Map with DataTable C#

I want to import an excel data in database.我想在数据库中导入一个 excel 数据。 I have an excel which gets updated by team every month and they add new column with name "Jan data format" (current month name + "data format").我有一个 excel,每个月都会由团队更新,他们会添加名称为“Jan 数据格式”(当前月份名称 +“数据格式”)的新列。 which gets added after the last month column.在上个月的列之后添加。

DataTable dt = new DataTable();
            dt.Columns.Add("SOURCE");
            dt.Columns.Add("DUMMY CODE");
            dt.Columns.Add("CUSTOMER CODE");
            dt.Columns.Add("STK_CODE");

            DataRow row;

            while (((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 1]).Value2 != null)
            {
                row = dt.NewRow();
                row[0] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, colIndex]).Value2);
                row[1] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 2]).Value2);
                row[2] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 3]).Value2);
                row[3] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 4]).Value2);
                index++;

                rowIndex = 2 + index;
                dt.Rows.Add(row);
            }

Here row[1] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 2]).Value2);这里row[1] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)wks.Cells[rowIndex, 2]).Value2); i'm using column index.我正在使用列索引。 How to use Column Name in place of column index?如何使用列名代替列索引?

The only non-index identifier a column has in Excel is the letter one sees at the top of the column: A , B , etc. This cannot be changed.列在 Excel 中唯一的非索引标识符是在列顶部看到的字母: AB等。这是无法更改的。

Any text typed in a cell above other cells, which looks like a column header/name is just text, as far as Excel is concerned.就 Excel 而言,在其他单元格上方的单元格中键入的任何文本(看起来像列标题/名称)只是文本。

I'm using column index.我正在使用列索引。 How to use Column Name in place of column index?如何使用列名代替列索引?

So there is no such thing as a "Column Name", therefore there's no way to use string information to identify a column.所以没有“列名”这样的东西,因此无法使用字符串信息来标识列。

It would be necessary to loop all the cells in a row, testing their value, to identify a column that has a particular string as the column heading.有必要循环一行中的所有单元格,测试它们的值,以确定具有特定字符串作为列标题的列。 Something like就像是

string headerName ="two";
Excel.Worksheet sh = (Excel.Worksheet) excelApp.ActiveSheet;
Excel.Range headerRow = (Excel.Range) sh.UsedRange.Rows[1];
Excel.Range col = null;

foreach(Excel.Range cel in headerRow.Cells)
{
    if (cel.Text.ToString().Equals(headerName))
    {
        col = sh.Range[cel.Address, cel.End[Excel.XlDirection.xlDown]];
        break;
    }
}

foreach(Excel.Range cellInCol in col)
{
    Debug.Print(cellInCol.Value2.ToString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM