2

I'm trying to use the standard .Clear() method for an EPPlus spreadsheet without any luck. Anyone know how to clear all the data in a column using the EPPlus plugin? Below is what i have so far. Does not throw an exception but doesn't work at all.

if (File.Exists(path))
{
    //Connect to spreadsheet
    FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);
    FileInfo file = new FileInfo(path);
    _package = new ExcelPackage();

    //Pull data into EPPlus object
    _package.Load(stream);
    _sheet = _package.Workbook.Worksheets.First(); 

    //Clear out previous errors
    _sheet.Cells["L1"].Clear();
}
Crono
  • 10,211
  • 6
  • 43
  • 75
NealR
  • 10,189
  • 61
  • 159
  • 299
  • You are trying to modify the original document? If so, change your `FileAccess.Read` to `FileAccess.ReadWrite` – user1477388 Feb 20 '14 at 18:11
  • I don't want to change the original document, I want to alter what's stored in the `_sheet` object (an `ExcelPackage`) and save it somewhere else. I did try changing the access to `ReadWrite`, however, without any luck. – NealR Feb 20 '14 at 18:18

1 Answers1

4

I use the ExcelPackage a little differently (assuming we are talking about the same DLL):

public static FileInfo existingFile =
    new FileInfo(@"C:\Users\cle1394\Desktop\ExampleExcelFile.xlsx");

using (ExcelPackage xlPackage = new ExcelPackage(existingFile))
{
    ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];

    // clear value
    worksheet.Cell(iRow, 1).Value = "";

}

Try that, let me know if it works.

user1477388
  • 20,790
  • 32
  • 144
  • 264