简体   繁体   English

生成Open XML Word文档后自动更新公式字段

[英]Automatically update formula field after generating Open XML Word document

I have a library that generates Word documents using the OpenXML SDK, one of the functionalities of the library is the generation of tables with formulas on the last row (such as SUM(ABOVE) ). 我有一个使用OpenXML SDK生成Word文档的库,该库的一个功能是生成最后一行包含公式的表(例如SUM(ABOVE) )。

Word has these functions built-in, but when generating the document I have to assume that Word isn't installed on the machine. Word具有内置的这些功能,但在生成文档时,我必须假设Word未安装在计算机上。

The problem is that if I just generate the table and the formula field, it won't be updated automatically when the document is opened, the user would have to open the document and manually select "update field" for the function to be calculated. 问题是,如果我只是生成表格和公式字段,它将不会在打开文档时自动更新,用户必须打开文档并手动选择“更新字段”才能计算函数。

Is there any way to do this within the code (without having to calculate the function manually)? 有没有办法在代码中执行此操作(无需手动计算功能)?

No, this is by design. 不,这是设计的。 See Which fields are updated when you open, repaginate, or print document for details. 有关详细信息,请参阅打开,重新打印或打印文档时更新的字段

This true of any manual input as well (for example if you had 10 in A1, 10 in A2, inserted =SUM(ABOVE) in A3, you'd have 20. But if you changed A1 to 15, A3 would not update automatically unless and until you manually did so). 对于任何手动输入也是如此(例如,如果您在A1中有10个,在A2中有10个,在A3中插入=SUM(ABOVE) ,则您有20个。但是如果您将A1更改为15,则A3将不会自动更新除非并且直到你手动这样做)。

The only ways around this are to create an add-in (VSTO, VBA, etc.) to sink the Document_Open event on client machines and run some code to update all fields or create an Interop app on the server that opens the doc, renders it with some code and then saves it back before sending it on further. 解决此问题的唯一方法是创建一个加载项(VSTO,VBA等)以在客户端计算机上接收Document_Open事件并运行一些代码来更新所有字段或在打开文档的服务器上创建Interop应用程序,呈现它带有一些代码,然后在进一步发送之前将其保存回来。

Have you seen this, UpdateFieldsOnOpen class. 你看过这个, UpdateFieldsOnOpen类。 ( https://msdn.microsoft.com/en-us/library/cc861799 ) It should force an update of all fields on the first time a documented is opened using an app that can update it (such as Word). https://msdn.microsoft.com/en-us/library/cc861799 )它应该在第一次使用可以更新它的应用程序(例如Word)打开文档时强制更新所有字段。

I guess that each field that has it's Dirty flag set to true will get an update, but don't quote me on that ;) 我想每个将其Dirty标志设置为true的字段都会得到更新,但不要引用我的内容;)

You can set the value UpdateFieldsOnOpen , when opening the document Word will ask you what you want to refresh. 您可以设置值UpdateFieldsOnOpen ,打开文档Word时会询问您要刷新的内容。 See this article for more details : http://www.samuraiprogrammer.com/blog/2010/08/09/OpenXMLHowToRefreshAFieldWhenTheDocumentIsOpened.aspx 有关更多详细信息,请参阅此文章: http//www.samuraiprogrammer.com/blog/2010/08/09/OpenXMLHowToRefreshAFieldWhenTheDocumentIsOpened.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM