1

I'm trying to read a row from an excel file in c# and store in the object using EPPLUS.

Now when I print the content of the object, it displays all the content of the row as single string.

How do I limit my access to single cell content? i.e., individual cell content so that I can use those particular fields as per my requirement.

var existingFile = new FileInfo("D:\\Fields.xlsx"); 
using (var package = new ExcelPackage(existingFile)) 
{
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null) 
    { 
        if (workBook.Worksheets.Count > 0) 
        { 
            ExcelWorksheet currentWorksheet = workBook.Worksheets.First(); 
            var lastrow = currentWorksheet.Dimension.End.Row; 
            var lastcol = currentWorksheet.Dimension.End.Column; 
            for (int i = 1; i <= lastrow; i++) 
            {
                for (int j = 1; j <= lastcol; j++) 
                {
                    object asd = new object(); 
                    asd = currentWorksheet.Cells[i, j].Value; 
                    Console.WriteLine(asd);
                }
            }
        }
     }
}
Jashwanth
  • 11
  • 4
  • Well, I don't have any experience using that library, but maybe this answer could help you http://stackoverflow.com/a/21118652/541432 – elvin Oct 16 '14 at 08:16
  • I feel its nothing to do with library. I got an access to excel file and I'm as well able to read all the data at a once but what I am looking for is to be able to access one particular value among those large chunk of data that is stored in the object. Hope I'm making sense. Sorry if I'm making it complicated. – Jashwanth Oct 16 '14 at 09:30

1 Answers1

1

Your code should work fine. If you are seeing the entire row of the excel file printed as a single string then my guess would be it is a problem with the content of the excel file itself in which case you would have to parse the string somehow. Post a screen shot or your, even better, the excel file itself online somewhere so we can take a look.

In the meantime, check out this code which creates its own excel file first and then reopens and saves using your code:

public void ConsoleWriteTest()
{
    //Create some data
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var pck = new ExcelPackage(existingFile))
    {
        var workbook = pck.Workbook;
        var worksheet = workbook.Worksheets.Add("newsheet");

        #region Data

        //The data
        worksheet.Cells["A1"].Value = "Col1";
        worksheet.Cells["A2"].Value = "sdf";
        worksheet.Cells["A3"].Value = "ghgh";
        worksheet.Cells["A4"].Value = "sdf";
        worksheet.Cells["A5"].Value = "wer";

        worksheet.Cells["B1"].Value = "Col2";
        worksheet.Cells["B2"].Value = "Group B";
        worksheet.Cells["B3"].Value = "Group A";
        worksheet.Cells["B4"].Value = "Group C";
        worksheet.Cells["B5"].Value = "Group A";

        worksheet.Cells["C1"].Value = "Col3";
        worksheet.Cells["C2"].Value = 634.5;
        worksheet.Cells["C3"].Value = 274.5;
        worksheet.Cells["C4"].Value = 453.5;
        worksheet.Cells["C5"].Value = 634.5;

        worksheet.Cells["D1"].Value = "Col4";
        worksheet.Cells["D2"].Value = 996440;
        worksheet.Cells["D3"].Value = 185780;
        worksheet.Cells["D4"].Value = 686468;
        worksheet.Cells["D5"].Value = 996440;

        #endregion

        pck.Save();
    }

    //Reopen the file
    using (var package = new ExcelPackage(existingFile))
    {
        ExcelWorkbook workBook = package.Workbook;
        if (workBook != null)
        {
            if (workBook.Worksheets.Count > 0)
            {
                ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
                var lastrow = currentWorksheet.Dimension.End.Row;
                var lastcol = currentWorksheet.Dimension.End.Column;
                for (int i = 1; i <= lastrow; i++)
                {
                    for (int j = 1; j <= lastcol; j++)
                    {
                        object asd = new object();
                        asd = currentWorksheet.Cells[i, j].Value;
                        Console.WriteLine(asd);
                    }
                }
            }
        }
    }
}

EDIT 1: Row or column as a collection

//Get entire rows or columns as collection and then print by casting
using (var package = new ExcelPackage(existingFile))
{
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count <= 0) 
            return;

        ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
        var lastrow = currentWorksheet.Dimension.End.Row;
        var lastcol = currentWorksheet.Dimension.End.Column;

        //get the row of column headers which are strings
        var asdrange = currentWorksheet.Cells[1, 1, 1, lastcol];
        Console.WriteLine("As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value);

        object asd = new object();
        asd = currentWorksheet.Cells[1, 1, 1, lastcol].Value;

        object[,] cellObjects = (object[,])asd;
        List<string> stringList = cellObjects.Cast<string>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(stringList[0]);
        Console.WriteLine(stringList[1]);
        Console.WriteLine(stringList[2]);
        Console.WriteLine(stringList[3]);

        //get the second row which is a mix of strings and double
        asdrange = currentWorksheet.Cells[2, 1, 2, lastcol];
        Console.WriteLine(Environment.NewLine + "As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value); 

        asd = currentWorksheet.Cells[2, 1, 2, lastcol].Value;
        cellObjects = (object[,])asd;
        List<object> objectList = cellObjects.Cast<object>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(objectList[0]);
        Console.WriteLine(objectList[1]);
        Console.WriteLine(objectList[2]);
        Console.WriteLine(objectList[3]);

        //Get Col3 which are doubles
        asdrange = currentWorksheet.Cells[2, 3, lastrow, 3];
        Console.WriteLine(Environment.NewLine + "As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value); 

        asd = currentWorksheet.Cells[2, 3, lastrow, 3].Value;
        cellObjects = (object[,])asd;
        List<double> doubleList = cellObjects.Cast<double>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(doubleList[0]);
        Console.WriteLine(doubleList[1]);
        Console.WriteLine(doubleList[2]);
        Console.WriteLine(doubleList[3]);
        Console.WriteLine(doubleList[4]);
        Console.WriteLine(doubleList[5]);
        Console.WriteLine(doubleList[6]);
        Console.WriteLine(doubleList[7]);
        Console.WriteLine(doubleList[8]);
    }
}
Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • Thank you @Ernie for taking interest in answering but I aint looking for this. Do you notice we are storing all the excel data in "asd" object in the end? Well, now, I'd like to get access to only few fields in that object. That could be a content of an individual cell or the data of a particular row. Hope I make sense. – Jashwanth Oct 17 '14 at 05:35
  • @Jashwanth, "storing all the excel data in "asd" object in the end" - according to the code you posted this is not what you are doing. In your code you are looping through cell by cell by specifying a row (i) or column (j) and not as a collection. But it sound like you want to get an ENTIRE row as a collection and then manipulate it? If that is the case, you will have to do some casting. See the additional code I pasted above for what I mean. Again, having an example excel file posted would help. – Ernie S Oct 17 '14 at 11:19