Home » excel » c# – Can not read from inputstream in FileMode.Append,FileAccess.Write

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

You can do the same actions easily with Microsoft open xml
https://docs.microsoft.com/en-us/office/open-xml/how-to-get-worksheet-information-from-a-package