问题
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)
).
Word has these functions built-in, but when generating the document I have to assume that Word isn't installed on the machine.
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)?
回答1:
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).
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.
回答2:
Have you seen this, UpdateFieldsOnOpen
class. (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).
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 ;)
回答3:
You can set the value UpdateFieldsOnOpen
, when opening the document Word will ask you what you want to refresh. See this article for more details : http://www.samuraiprogrammer.com/blog/2010/08/09/OpenXMLHowToRefreshAFieldWhenTheDocumentIsOpened.aspx
来源:https://stackoverflow.com/questions/3122406/automatically-update-formula-field-after-generating-open-xml-word-document