简体   繁体   中英

How to update the excel file cell data, when the file is created using a template in C#?

I am creating a new excel file using a template file, but I am unable to edit the contents in the new file created, please assist with the same. Thanks in advance.

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);

using (ExcelPackage package = new ExcelPackage(fileStream))
{
     package.Save();
}

string name = "filecreated.xlsx";
string fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
fileStream.Position = 0;

//return file 
return File(fileStream, fileType, name);```

It Seems like you are opening the file in read only access mode

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);

Will you please try like

FileInfo existingFile = new FileInfo(filepath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
  //As we have not modified the file, its useless to call Save()

  //This will override the opened file
  package.Save();
}

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