简体   繁体   中英

Edit Saved Microsoft Word Document in C#/Asp.net

I am not sure if this is possible and every where I have searched, I cannot find a clear answer. I am saving a Microsoft Word document to a SQL Server 2008 table. Basically just converting the file to a Byte[] and writing that to the table. This word document is a "template" file. The file is a form that the user needs to fill out. What I am wondering, is after reading that file from SQL Server and before opening it up for the user, is there a way to autopopulate some fields in the form for the user? For example, if I know the address of the user already, can I autopopulate the address field in the template for them?

I know that using Microsoft.Office.Interop.Word , I can search the document for bookmarks and insert data at the bookmark. However, as far as I know, you cannot use Microsoft.Office.Interop.Word to open a Byte[].

Is there anyway to complete what I was looking for?

If you want to use OpenXML, then you can do it like this,

//Load your byte[] array into memory stream and then 
WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);

You can do what you are trying to achieve using OpenXML without installing word on the server side..More resources on OpenXMl can be found on http://openxmldeveloper.org/ . And the open xml sdk can be downloaded from here .

I think the general steps would be to

1) Save the file to the local hard drive of the user with a file name based on the template but with a .doc extension.

2)Open the file with interop, but keep it invisible.

3)Populate the fields with bookmarks.

4)Show it to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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