简体   繁体   中英

EPPlus To Read 1st Column Of Excel Into Array

Use OpenFileDialog with 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>();
    }
  }
}

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>();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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