我尝试从Excel文件中删除工作表,并且尝试了很多来自Internet的资源,但总是得到相同的结果:内容不可读。
我也尝试过:
Sheet sheet = workbook.WorkbookPart.Workbook.Descendants<Sheet>().First(s => s.Name.Equals(sheetName));
sheet.Remove();
workbook.WorkbookPart.Workbook.Save();
加上此:
sheet.RemoveAllChildren()
但是文件总是损坏的。
请!
更新
using (MemoryStream xlsxStream = new MemoryStream())
{
using (var fileStream = File.OpenRead(templatePath))
fileStream.CopyTo(xlsxStream);
...
using (var workbook = SpreadsheetDocument.Open(xlsxStream, true, new OpenSettings { AutoSave = true }))
{
Sheet sheet = workbook.WorkbookPart.Workbook.Descendants<Sheet>().First(s => s.Name.Equals(sheetName));
sheet.Remove();
workbook.WorkbookPart.Workbook.Save();
...