我有一个包含一系列图表的Word文档(.docx)。我的代码成功更新饼图背后的值,Word会相应地显示结果(刷新图表的前端,无论您想要调用它)。
对于聚类条形图而言,同样令人遗憾的是不能说。工作簿中的值更新但前端不刷新以显示新值。
一旦我右键单击图表并选择编辑数据(我甚至不需要编辑任何内容,只需在打开时关闭工作簿窗口),然后刷新图表上显示的值。
有没有我可以强制刷新前端而不经过那个?关闭文件并重新打开无效。
我有什么:
wordcon.Application wordapp = new wordcon.Application();
wordcon.Document docx = new wordcon.Document();
wordapp.ActiveDocument.InlineShapes[2].Chart.ChartData.Activate();
excelcon.Workbook excelwb = wordapp.ActiveDocument.InlineShapes[2].Chart.ChartData.Workbook;
excelcon.Worksheet excelws = excelwb.Worksheets[1];
excelws.Cells[1, 2] = reference.top_issues_1_name;
excelws.Cells[6, 1] = "a";
excelws.Cells[6, 2] = reference.top_issues_1;
excelws.Cells[5, 1] = "b";
excelws.Cells[5, 2] = reference.top_issues_2;
excelws.Cells[4, 1] = "c";
excelws.Cells[4, 2] = reference.top_issues_3;
excelws.Cells[3, 1] = "d";
excelws.Cells[3, 2] = reference.top_issues_4;
excelws.Cells[2, 1] = "e";
excelws.Cells[2, 2] = reference.top_issues_5;
excelwb.Close();
Marshal.ReleaseComObject(excelws);
Marshal.ReleaseComObject(excelwb);
docx.SaveAs2(reference.chart_file_forked_path + reference.chart_file_forked_file_name);
wordapp.Quit();
Marshal.ReleaseComObject(docx);
Marshal.ReleaseComObject(wordapp);
答案 0 :(得分:1)
我在Powerpoint中遇到了这个问题。我怀疑解决方案会类似。
<强>未测试强>
如果这在Word中不起作用,请告诉我,我会修改。
Dim cObj as Shape 'Shape container for the ChartObject
'Set cObj = '
'## Expose the data sheet'
cObj.Chart.chartData.Activate
'## Minimize it'
cObj.Chart.chartData.Workbook.Application.WindowState = -4140
'## code to manipulate the cObj, if any.'
'## when done, close the ChartData'
'## When you're done, close the chartData'
cObj.Chart.chartData.Workbook.Close
答案 1 :(得分:0)
这是我正在运行的“触摸”单词doc并强制刷新嵌入式Excel图表的代码(我正在使用OpenXml库):
//open word doc
using (var wDoc = WordprocessingDocument.Open(someDocFileInfo.FullName, true))
{
//open embedded excel object with chart
var stream = wDoc.MainDocumentPart.EmbeddedPackageParts.First().GetStream();
using (var ssDoc = SpreadsheetDocument.Open(stream, true))
{
var wbPart = ssDoc.WorkbookPart;
var sheet = wbPart.Workbook.Descendants<Sheet>().FirstOrDefault(i => i.Name == "Sheet1");
if (sheet != null)
{
var ws = (wbPart.GetPartById(sheet.Id) as WorksheetPart).Worksheet;
//change the data in the excel sheet
var cell = InsertCellInWorksheet("A", 2, ws);
cell.CellValue = new CellValue("42");
cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.Number);
ws.Save();
}
}
//now 'touch' the word doc to refresh the chart
var settingPart = wDoc.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
var updateFields = new UpdateFieldsOnOpen();
updateFields.Val = new OnOffValue(true);
settingPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
settingPart.Settings.Save();
}
}