Search code examples
c#excelstreamepplus

Can not read from inputstream in FileMode.Append,FileAccess.Write


I am trying to read a excel file using

 using (var stream = new FileStream(filepath, FileMode.Append,FileAccess.Write))
                        {
                            ExcelWorksheet worksheet;
                            using (ExcelPackage pck = new ExcelPackage(stream))

actually I am trying to add a worksheet in existing excel file so that's why I am using append , and when I used filemode.create then it is overtiring my existing tab with the new one so what should I do?


Solution

  • Just use the following code and it will add a new worksheet to your current file:

    FileInfo newFile = new FileInfo("YourFile.xlsx");
    using (ExcelPackage p = new ExcelPackage(newFile))
    {
        p.Workbook.Worksheets.Add("YourNewSheet");
        p.Save();
    }