Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

updating chart data in presentation - broken reference (?)

$
0
0




static void Main(string[] args) { using (PresentationDocument presDoc = PresentationDocument.Open(@"C:\Users\username\Desktop\tests\test.pptx", true)) { string rid = null; SlideId slideId = (SlideId)presDoc.PresentationPart.Presentation.SlideIdList.ChildElements[4]; SlidePart slidePart = (SlidePart)presDoc.PresentationPart.GetPartById(slideId.RelationshipId.ToString()); Slide slide = slidePart.Slide; //ChartPart chartPart = slidePart.ChartParts.FirstOrDefault(); List<ChartPart> list= slidePart.ChartParts.ToList<ChartPart>(); //ChartPart chartPart = slidePart.ChartParts.Last(); //ChartPart chartPart //for (int i = 0; i < list.Count(); i++) //{ // chartPart = list[i]; // if (chartPart.=="Chart3") // { // } //} ChartPart chartPart = list[1]; EmbeddedPackagePart worksheetPart=chartPart.EmbeddedPackagePart; //chartPart.Parts. using (SpreadsheetDocument embedDocument = SpreadsheetDocument.Open(worksheetPart.GetStream(), true)) { WorksheetPart sheetPart = (WorksheetPart)embedDocument.WorkbookPart.GetPartById(embedDocument.WorkbookPart.Workbook.Descendants<ss.Sheet>().Where(s => s.Name == "Sheet1").First().Id.ToString()); ss.Worksheet sheet = sheetPart.Worksheet; //series names ss.Cell cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B1").First(); cell.CellValue.Text = "C1"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C1").First(); cell.CellValue.Text = "C2"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D1").First(); cell.CellValue.Text = "C3"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E1").First(); cell.CellValue.Text = "C4"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F1").First(); cell.CellValue.Text = "C5"; //categories names cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "A2").First(); cell.CellValue.Text = "S1"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "A3").First(); cell.CellValue.Text = "S2"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "A4").First(); cell.CellValue.Text = "S3"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "A5").First(); cell.CellValue.Text = "S4"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "A6").First(); cell.CellValue.Text = "S5"; //values cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B2").First(); cell.CellValue.Text = "1"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C2").First(); cell.CellValue.Text = "2"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D2").First(); cell.CellValue.Text = "3"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E2").First(); cell.CellValue.Text = "4"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F2").First(); cell.CellValue.Text = "5"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B3").First(); cell.CellValue.Text = "6"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C3").First(); cell.CellValue.Text = "7"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D3").First(); cell.CellValue.Text = "8"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E3").First(); cell.CellValue.Text = "9"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F3").First(); cell.CellValue.Text = "10"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B4").First(); cell.CellValue.Text = "11"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C4").First(); cell.CellValue.Text = "12"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D4").First(); cell.CellValue.Text = "13"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E4").First(); cell.CellValue.Text = "14"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F4").First(); cell.CellValue.Text = "15"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B5").First(); cell.CellValue.Text = "16"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C5").First(); cell.CellValue.Text = "17"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D5").First(); cell.CellValue.Text = "18"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E5").First(); cell.CellValue.Text = "19"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F5").First(); cell.CellValue.Text = "20"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "B6").First(); cell.CellValue.Text = "66"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "C6").First(); cell.CellValue.Text = "62"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "D6").First(); cell.CellValue.Text = "58"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "E6").First(); cell.CellValue.Text = "54"; cell = sheet.Descendants<ss.Cell>().Where(c => c.CellReference == "F6").First(); cell.CellValue.Text = "50"; // modification } Chart chart = chartPart.ChartSpace.Elements<Chart>().First(); ExternalData ED = chartPart.ChartSpace.Elements<ExternalData>().First(); AutoUpdate AU = ED .Descendants<AutoUpdate>() .FirstOrDefault(); AU.Val = true; }

I created that code on base of some code found on openxmldeveloper site. It runs without errors, data in workbook is updated. The problem is that after running it, reference between chart and that workbook is broken (data on chart is not updated and there is no possibility to edit source data).In addition when I try to open that workbook, excel shows some message that there is some problem. When I choose to recover, it opens with good data.


Code of output xml files in which there are differences in comparison to source files:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?><xworksheet xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac"><xsheetPr codeName="Sheet1"/><xdimension ref="A1:F8"/><xsheetViews><xsheetView tabSelected="1" workbookViewId="0"><xselection activeCell="E12" sqref="E12"/></xsheetView></xsheetViews><xsheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/><xcols><xcol min="1" max="1" width="11" customWidth="1"/><xcol min="2" max="4" width="10" customWidth="1"/></xcols><xsheetData><xrow r="1" spans="1:6" x14ac:dyDescent="0.25"><xc r="A1" s="1" t="s"><xv>
     0</xv></xc><xc r="B1" s="2" t="s"><xv>
     C1</xv></xc><xc r="C1" s="2" t="s"><xv>
     C2</xv></xc><xc r="D1" s="2" t="s"><xv>
     C3</xv></xc><xc r="E1" s="3" t="s"><xv>
     C4</xv></xc><xc r="F1" s="3" t="s"><xv>
     C5</xv></xc></xrow><xrow r="2" spans="1:6" x14ac:dyDescent="0.25"><xc r="A2" s="4" t="s"><xv>
     S1</xv></xc><xc r="B2" s="7"><xv>
     1</xv></xc><xc r="C2" s="7"><xv>
     2</xv></xc><xc r="D2" s="7"><xv>
     3</xv></xc><xc r="E2" s="7"><xv>
     4</xv></xc><xc r="F2" s="7"><xv>
     5</xv></xc></xrow><xrow r="3" spans="1:6" x14ac:dyDescent="0.25"><xc r="A3" s="5" t="s"><xv>
     S2</xv></xc><xc r="B3" s="8"><xv>
     6</xv></xc><xc r="C3" s="8"><xv>
     7</xv></xc><xc r="D3" s="8"><xv>
     8</xv></xc><xc r="E3" s="8"><xv>
     9</xv></xc><xc r="F3" s="8"><xv>
     10</xv></xc></xrow><xrow r="4" spans="1:6" x14ac:dyDescent="0.25"><xc r="A4" s="5" t="s"><xv>
     S3</xv></xc><xc r="B4" s="8"><xv>
     11</xv></xc><xc r="C4" s="8"><xv>
     12</xv></xc><xc r="D4" s="8"><xv>
     13</xv></xc><xc r="E4" s="8"><xv>
     14</xv></xc><xc r="F4" s="8"><xv>
     15</xv></xc></xrow><xrow r="5" spans="1:6" x14ac:dyDescent="0.25"><xc r="A5" s="4" t="s"><xv>
     S4</xv></xc><xc r="B5" s="7"><xv>
     16</xv></xc><xc r="C5" s="7"><xv>
     17</xv></xc><xc r="D5" s="7"><xv>
     18</xv></xc><xc r="E5" s="7"><xv>
     19</xv></xc><xc r="F5" s="7"><xv>
     20</xv></xc></xrow><xrow r="6" spans="1:6" x14ac:dyDescent="0.25"><xc r="A6" s="5" t="s"><xv>
     S5</xv></xc><xc r="B6" s="6"><xv>
     66</xv></xc><xc r="C6" s="6"><xv>
     62</xv></xc><xc r="D6" s="6"><xv>
     58</xv></xc><xc r="E6" s="6"><xv>
     54</xv></xc><xc r="F6" s="6"><xv>
     50</xv></xc></xrow><xrow r="8" spans="1:6" x14ac:dyDescent="0.25"><xc r="B8" t="s"><xv>
     1</xv></xc></xrow></xsheetData><xpageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><xpageSetup orientation="portrait" r:id="rId1"/><xtableParts count="1"><xtablePart r:id="rId2"/></xtableParts></xworksheet>
<?xml version="1.0" encoding="utf-8" standalone="yes" ?><xworkbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><xfileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="9303"/><xworkbookPr codeName="ThisWorkbook" defaultThemeVersion="124226"/><xbookViews><xworkbookView xWindow="480" yWindow="75" windowWidth="21840" windowHeight="13740"/></xbookViews><xsheets><xsheet name="Sheet1" sheetId="1" r:id="rId1"/></xsheets><xcalcPr calcId="0" calcMode="manual"/><xoleSize ref="A1"/></xworkbook>

In attached xml code there of course should be ":" between "x" and "workbook" etc. I do not know why it disappeared ...





Viewing all articles
Browse latest Browse all 1288

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>