-2

I have a prepared dictionary in c# and xlsx file with filled columns. I need to find the column's and row's indexes if the cell contains the value provided from a dictionary.

I am using ExcelPackage and ExcelWorksheet. Dictionary:

public static Dictionary<string, string> ExcelColumnsMapping = new Dictionary<string, string>()
{
    {"property name", "column name"}
}

XLS

var excelFile = new FileInfo(importFileFullPath);
 using (ExcelPackage package = new ExcelPackage(excelFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];           
            }

And here I wonder how can i find the cell indexes with provided data. I'd like to see an example with any string or something. Thank you for help!

Deryl
  • 66
  • 5
  • What library are you using? What code do you have? –  Aug 28 '18 at 13:04
  • 2
    Please provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) for your problem. We would like to help you, but you need to show us that you have attempted to code a solution first. – absoluteAquarian Aug 28 '18 at 13:06
  • I'm curious why there is no code in the question pertaining to loading the spreadsheet? Do you have any code loading the spreadsheet? –  Aug 28 '18 at 13:16

1 Answers1

0

I didn't understand what you want to do very well but this is a methode that search for a value in an xlsx file and returns the line and the column if found.

using Excel = Microsoft.Office.Interop.Excel; //you should add the reference to that library for it to work

public Tuple<int, int> search(string Providedvalue)
{
    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range range;

    string str;
    int rCnt;
    int cCnt;
    int rw = 0;
    int cl = 0;
    xlApp = new Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open("path/exemple.xlsx", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    range = xlWorkSheet.UsedRange;
    rw = range.Rows.Count;
    cl = range.Columns.Count;

    bool found = false;
    for (rCnt = 1; rCnt <= rw && found == false; rCnt++)
    {
            for (cCnt = 1; cCnt <= cl && found == false; cCnt++)
            {
                dynamic x = (range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                str = x.ToString();
                if (Providedvalue == str)
                {
                    int line = rCnt;
                    int column = cCnt;
                    return new Tuple<int, int>(line, column);
                }
            }
    }
    return null;
}
Abdou
  • 330
  • 1
  • 9