I am trying to update a cell in Excel's spreadsheet via Open XML SDK:
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace DotNetSandbox.SO
{
public class CellUpdating
{
public static void Update()
{
using SpreadsheetDocument doc = SpreadsheetDocument.Open(@"c:\temp\test.xlsx", true);
Sheet sheet = doc.WorkbookPart.Workbook.Descendants<Sheet>().First();
WorksheetPart wsPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id);
Cell cell = wsPart.Worksheet.Descendants<Cell>().First(c => c.CellReference == "A2");
cell.CellValue = new CellValue("new");
wsPart.Worksheet.Save();
}
}
}
That updates cell, but only after recovering by Excel app:
Environment:
- DocumentFormat.OpenXml: 2.13.0
- Excel: Microsoft 265 MSO (16.0.14026.20270) 64 bit
What I do wrong?