2

OpenFileDialog与 EPPlus 一起使用。我收到以下编译错误:

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

现在,明显的问题是如何将选定的 Excel 文件与我的 EPPPlus & 2 关联起来,我该怎么做才能消除上述错误?

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

1 回答 1

2

你很接近。您所要做的就是根据流创建包(或者您可以使用 fileinfo 重载 - 无论哪种方式)。像这样:

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>();
        }
    }
}
于 2015-03-25T18:51:21.383 回答