我正在尝试通过Open XML SDK更新Excel电子表格中的单元格:
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();
}
}
}
这会更新单元格,但只有在Excel应用程序恢复后才生效:
环境:
- DocumentFormat.OpenXml: 2.13.0
- Excel: 微软 265 MSO (16.0.14026.20270) 64 位
我做错了什么?
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString)
吗?但这样做也不行。 - Adam Shakhabov