相对于第一列中的值循环遍历excel工作表中第二列的行

时间:2018-11-27 10:01:47

标签: c# excel

我的Excel表如下

enter image description here

我想循环浏览excel表,在该表中应形成最终结果,将LookUpTableValue中的值与LookUpTableName中的值存储起来<​​/ p>

例如

PQR:{aaaa,bbbb,cccc,dddd,eeee},
GHI:{hhhh,iii,jjj,kkk,lll}.

下面是我尝试过的代码,

static internal bool createLookupTable(ProjectContext projContext,string lookupTableName)
    {
        bool isLookupTableCreated = false;
      //  string[] lookupTableValues = {"PQR","GHI"};
        //LookupTableCreationInformation NewLookupTable = new LookupTableCreationInformation();
        DataTable de = new DataTable();
        try
        {
            using (XLWorkbook xlworkBook = new XLWorkbook("C:\\Users\\Desktop\\CustomFieldDetails1.xlsx"))
            {
                IXLWorksheet workSheet = xlworkBook.Worksheet(2);
                bool firstRow = true;
                foreach (IXLRow row in workSheet.Rows())
                {
                    if (firstRow)
                    {
                        foreach (IXLCell xLCell in row.Cells())
                        {
                            de.Columns.Add(xLCell.Value.ToString());
                        }
                        firstRow = false;
                    }
                    else
                    {
                        if (row.FirstCellUsed() != null & row.LastCellUsed() != null)
                        {
                            de.Rows.Add();
                            int j = 0;
                            foreach (IXLCell xLCell in row.Cells(row.FirstCellUsed().Address.ColumnNumber, row.LastCellUsed().Address.ColumnNumber))
                            {
                                de.Rows[de.Rows.Count - 1][j] = xLCell.Value.ToString();
                                lookupTableValues[j++] = xLCell.Value.ToString();
                                j++;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // throw ex;
        }
        return isLookupTableCreated;
    }

但是我得到的结果是

PQR:{aaaa,bbbb,cccc,dddd,eeee,hhh,iii,jjj,kkk,lll}, 
GHI:{aaaa,bbbb,cccc,dddd,eeee,hhhh,iii,jjj,kkk,lll}

任何帮助将不胜感激

0 个答案:

没有答案