我想将在memorystream中创建的工作簿附加到powerpoint图表。我创建了那个工作簿(我现在不确定100%是否正确),我需要以某种方式将它附加到图表和整个PP包中。
MemoryStream memoryStream = new MemoryStream();
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook);
WorkbookPart wbPart= spreadsheetDocument.AddWorkbookPart();
// create the worksheet
WorksheetPart wsPart= wbPart.AddNewPart<WorksheetPart>();
wsPart.Worksheet = new Worksheet(new SheetData());
Worksheet ws=wsPart.Worksheet;
ChartSpace chartSpace=chartPart.ChartSpace;
List<StringReference> dataReferences = chartSpace.Descendants<StringReference>().ToList();
if (dataReferences!=null)
{
Support.SupOpenXML.FillWorksheetWithChartData(wsPart.Worksheet, dataReferences);
}
ws.Save();
memoryStream.Seek(0, SeekOrigin.Begin);
var sr = new StreamReader(memoryStream);
string myStr = sr.ReadToEnd();
chartPart.AddEmbeddedPackagePart(myStr);
上一次操作给了我一个错误,myStr中有一些奇怪的值。我应该在代码中更改什么才能工作?我应该改变哪些关系才能发挥作用?
Stream:"PK\0\0\0\b\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\0\0xl/workbook.xml ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\n\0\0\0\0\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\v\0\0_rels/.rels ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\0\0\0\b\0*~hH????\0\0\0\0a\0\0\0\0xl/worksheets/sheet.xml ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\0\0\0\b\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\0\0xl/_rels/workbook.xml.rels ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK-\0\0\0\0\b\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0xl/workbook.xmlPK-\0\n\0\0\0\0\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\v\0\0\0\0\0\0\0\0\0\0\0\0\0I\0\0\0_rels/.relsPK-\0\0\0\0\b\0*~hH????\0\0\0\0a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0?\0\0\0xl/worksheets/sheet.xmlPK-\0\0\0\0\b\0*~hH\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0?\0\0\0xl/_rels/workbook.xml.relsPK\0\0\0\0\0\0\0\03\0\0\0\0"
答案 0 :(得分:0)
我使用了以下代码:
List<ExternalRelationship> references = chartPart.ExternalRelationships.ToList();
for (int i = chartPart.ExternalRelationships.Count() - 1; i >= 0; i--)
{
ReferenceRelationship refer = chartPart.ExternalRelationships.ElementAt(i);
chartPart.DeleteReferenceRelationship(refer);
}
chartPart.ChartSpace.RemoveAllChildren<ExternalData>();
string relationID = "rId99999";
EmbeddedPackagePart embeddedPackagePart1 = chartPart.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", relationID);
memoryStream.Seek(0, SeekOrigin.Begin);
embeddedPackagePart1.FeedData(memoryStream);
chartPart.CreateRelationshipToPart(embeddedPackagePart1, relationID);
ExternalData externalData1 = new ExternalData() { Id = relationID };
AutoUpdate autoUpdate1 = new AutoUpdate() { Val = false };
externalData1.Append(autoUpdate1);
chartPart.ChartSpace.Append(externalData1);
现在有效。