[使用C#中的模板创建文件时如何更新excel文件单元格数据?

问题描述 投票:0回答:1

我正在使用模板文件创建新的Excel文件,但无法编辑创建的新文件中的内容,请提供帮助。预先感谢。

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);```

c# excel epplus
1个回答
0
投票

似乎您正在以只读访问模式打开文件

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

请您尝试一下

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.