2

Use OpenFileDialogwith EPPlus. I get a compile error of:

The name 'sheet' does not exist in the current context

Now, the obvious issue is how do I associate the selected Excel file with my EPPPlus & 2 what do I do to remove the error above?

using OfficeOpenXml;
using OfficeOpenXml.Drawing;

private void btn_ReadExcelToArray_Click(object sender, EventArgs e)
{
  fd.Filter = "Excel Files|*.xlsx";
  fd.InitialDirectory = @"C:\";
  if (fd.ShowDialog() == DialogResult.OK)
  {          
    var columnimport = sheet.Cells["A2:A"];
    foreach (var cell in columnimport)
    {
        var column1CellValue = cell.GetValue<string>();
    }
  }
}
Big Pimpin
  • 427
  • 9
  • 23

1 Answers1

2

You are pretty close. All you have to do is create the package based on the stream (or you could use the fileinfo overload - either way). Like this:

var fd = new OpenFileDialog();
fd.Filter = "Excel Files|*.xlsx";
fd.InitialDirectory = @"C:\Temp\";

if (fd.ShowDialog() == DialogResult.OK)
{
    using (var package = new ExcelPackage(fd.OpenFile()))
    {
        var sheet = package.Workbook.Worksheets.First();
        var columnimport = sheet.Cells["A2:A"];
        foreach (var cell in columnimport)
        {
            var column1CellValue = cell.GetValue<string>();
        }
    }
}
Ernie S
  • 13,902
  • 4
  • 52
  • 79