Hello,
The problem is that I can change contents of the sheet inside word doc but after opening the doc user needs to double click the sheet and press ESC to see the updated content.
I saw an example of updating Chart data, but I need the exact code which resolves my PROBLEM,
PLEASE JUST PUT THE REQUIRED CODE TO RESOLVE THE PROBLEM NOT ANY LINK, JUST MY PROBLEM!
THANK YOU.
IEnumerable<EmbeddedObject> objects = mainPart.Document.Descendants<EmbeddedObject>();//Problem: update cached values in the word-processing document.
for (int i = 0; i < mainPart.EmbeddedPackageParts.Count(); i++)
{
EmbeddedPackagePart epp = (EmbeddedPackagePart)mainPart.EmbeddedPackageParts.ElementAt(i);
DocumentFormat.OpenXml.Vml.Shape shape = objects.ElementAt(i).ChildElements.OfType<DocumentFormat.OpenXml.Vml.Shape>().First();
if (shape.Alternate != null)
{
Stream stream = epp.GetStream(FileMode.OpenOrCreate);
BookmarkValue bv = excelObjectBookmarkValues.FirstOrDefault(P => P.Key.Trim().ToLower() == shape.Alternate.Value.Trim().ToLower());
if (bv != null)
{
List<DataModel.BookmarkValue> excelBookmarkValues = bv.DetailList;
using (SpreadsheetDocument ssDoc = SpreadsheetDocument.Open(stream, true))
{
FlushCachedValues(ssDoc);
WorkbookPart wbPart = ssDoc.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().First();
if (theSheet != null)
{
Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(theSheet.Id))).Worksheet;
SheetData sheetData = ws.GetFirstChild<SheetData>();
foreach (var item in excelBookmarkValues)
{
try
{
DefinedName name = wbPart.Workbook.Descendants<DefinedName>().FirstOrDefault(c => c.Name.Value == item.Key);
if (name != null)
{
Cell cell = sheetData.Descendants<Cell>().FirstOrDefault(c => c.CellReference.Value == ExtractCellReference(name.Text));
if (cell != null)
{
cell.CellValue = new CellValue("123456789");
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
}
}
}
catch
{
}
}
}
}