@vaibhavd-9419, Welcome to Microsoft Q&A, based on my research and test, you could try the following code to update spreadsheet table header values.
using (SpreadsheetDocument document = SpreadsheetDocument.Open("1.xlsx", true))
{
WorkbookPart wbPart = document.WorkbookPart;
Sheet sheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(sheet.Id));
Cell theCell = wsPart.Worksheet.Descendants<Cell>().
Where(c => c.CellReference == "A1").FirstOrDefault();
string updatetext = "new Header";
if (theCell != null)
{
if (theCell.DataType != null && theCell.DataType == CellValues.SharedString)
{
var sharedStringTable = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
var element = sharedStringTable.ChildElements[int.Parse(theCell.InnerText)];
if (string.IsNullOrEmpty(element.InnerText))
{
SharedStringItem sst = new SharedStringItem(new Text(updatetext));
sharedStringTable.AppendChild<SharedStringItem>(sst);
int value = sharedStringTable.ChildElements.ToList().IndexOf(sst);
theCell.CellValue = new CellValue(value.ToString());
sharedStringTable.Save();
}
else
{
element.InnerXml = element.InnerXml.Replace(element.InnerText, updatetext);
sharedStringTable.Save();
}
}
else
{
theCell.CellValue = new CellValue(updatetext);
theCell.DataType = new EnumValue<CellValues>(CellValues.String);
}
}
wbPart.Workbook.Save();
}
Based on my test, I could update the A1 value to "New Header" successfully.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.