0

我有一个包含一系列图表的 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);
4

2 回答 2

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  
于 2013-04-24T02:23:24.393 回答
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();
       }
    }
于 2018-06-06T13:53:45.890 回答