Use .NET to replace Word content (take data from the database to replace the bookmark inside Word)

xiaoxiao2021-03-06  99

In the current project, the user needs to print a document. When you do it, you will use the form, follow the format, but what you play is always different from them. One sentence, I don't want to see it. I also had a good way to have a good way. Later I thought, simply took their Word Template to replace. When I started, I didn't know how to do it, Google found, but no The same satisfaction. Later, I found a little bit on MSDN, and then asked my colleagues. I finally got it. The effect is not bad. The whole process is like this: (I use ASP.NET C #) 1. First need Word's DLL introduced, if you installed Word, there is a MSWORD9.OLB file below his installation directory (you can add a reference) Here you need to pay attention to the above file, you may be different from Office, The file name is different, and the parameters of the Open and Save methods below will also differ because of the version, the parameters of the Open in Office2003 seem to be 16, while the parameters in 2000 are only 12, called Be sure to pay attention to 2. To add a sentence in the webconfig file: Mainly simulated, if you don't add, the program is reported to report the wrong access. (And you need to do a good one with a bookmarks) 3. Newly created one side, adding such as Using Word; below is the specific function: (I have no sort of functions here, maybe some useless ) open file: private Word.Document OpenDoc (! string strDocPath, ref Word.Application WordApp, int flag) {if (File.Exists (strDocPath)) return null; object fileName = (object) strDocPath; object isVisible = missing; object Readonly = missing; // make word visible, so you can see what's have "@ o // via Open Create a Word.Document Doc = NULL; try {// doc = wordapp.documents. Open (ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing) DOC = Wordapp.Documents.Open (Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing; // IF ( FLAG == 1) // listStyle (DOC); Return Doc;

} CatCH (Exception EX) {throw new exception; return null;}} replacement template content: string strWordTemplate = server.mappath ("../ Test / office .doc"); // Here is Your template file word.application wordapp = new word.applicationclass (); // Define a word.application object word.document doc = OpenDoc (strWordTemplate, Ref WordApp, 1); // Define a word.Document object try {/ / Below is the data from the database, I am sorry, this code is a bit bad datatable temptable = this.createtable ("SELECT * WORKFLOW WHERE AppID =" Convert.Toint32 (AppID) "And ContentID =" Convert.Toint32 (ContentID) ); If (Temptable.Rows.count> 0) {string temptime = Temptable.Rows [0] ["Swtime"]. Tostring (). Trim (); int pos = temptime.indexof (""); string all = Temptime.substring (0, POS); int pre = all.indexof ("-"); int next = all.lastindexof ("-"); string year = all.substring (0, pre) .trim (); string Month = all.substring (pre 1, next-pre-1) .trim (); string day = all.substring (next 1, all.length-next-1) .trim (); foreach (Word.Bookmark BM in doc.bookmarks) // This is the most critical place: all for documents Bookmarks Conveniently match {switch (bm.name) {copy "advice": // Replace the contents of the Advice bookmark, other bm.select (); bm.range.text = this.createtable ("SELECT Advice from Workflow_Advice Where AppID = " Convert.TOINT32 (AppID) " And ContentId = " Convert.Toint32 (this.contentID) " And Stepid = 1 "). Rows [0] [" Advice "]. TOSTRING (). Trim () Break; Case "day": bm.select (); bm.range.text = day; break; cas

LWDW ": bm.select (); bm.range.text = temptable.rows [0] [" lwdw "]. Tostring (). Trim (); break; case" lwh ": bm.select (); bm. Range.text = Temptable.Rows [0] ["SWH"]. TOSTRING (). Trim (); Break; Case "Month": bm.select (); bm.range.text = month; break; casse "nowyear ": Bm.select (); bm.range.text = year; break; case" subject ": bm.select (); bm.range.text = temptable.rows [0] [" Subject "]. Tostring () .Trim (); Break; Case "SWH": bm.select (); bm.range.text = temptable.rows [0] ["LSH"]. TOSTRING (). Trim (). Substring (4, temptable. Rows [0] ["LSH"]. TOSTRING (). Trim (). Length-4); Break;}}} Object Fn = (Object) Server.mAppath ("../ Test / Temp.doc"); Doc.saveas (Ref Fn, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref Missing, Ref missing, ref missing, ref missing, ref missing, ref missing; // This is saved as a file response.Redirect ("../test/temp.doc"); // directly Open Saved Files in IE, then directly call the print function of IE //doc.saveas(Ref Fn, Ref Missing, Ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, Ref missing, ref missing, ref missing, ref missing;} catch (exception err) {this.tbxml.text = err.tostring ();} finally {doc.close (Ref missing) (REF MISSING , REF MISSING, WORDAPP.QUIT (Ref missing, ref missing, ref missing); WordApp = null;}} Doc.close (Ref missing, ref missing, ref missing);

Wordapp.quit (Ref missing, ref missing, ref miss); WordApp = NULL; this code seems to do not work, every time you turn your print, you will hold a word error, and Winword.exe inside the process is not closed, don't know how Winning. About some objects in Word, such as: Application Document Selection Range Bookmark object, I am not very familiar, please refer to the help on MSDN, it is very detailed. I hope to help everyone: Now there is a new problem: the above code is running normally under XP Office 2000, but the error message filter is in use in Win2000Server Office2000. Description: Execute an unprocessed exception during the current web request. Check the stack tracking information to learn more about the error and the code caused in the code. Abnormal Details: System.Runtime.InteropServices.comException: Message Filter Display Applications are being used.

Source error:

Row 132: // Open Word Template Row 133: String StrWordTemplate = Server.mappath ("../ Test / Settlement .doc"); line 134: Word.Application Wordapp = new word.Applicationclass (); line 135: Word.Document Doc = OpenDOC (strWordTemplate, Ref WordApp, 1); line 136: The winword.exe2000 inside the TRY can't kill it, it is a depressed supplement: I finally solved this problem: Or authority problem: please follow the method below Modify the server's permissions to solve such problems

Open WORD in the web, why is it to prompt to open macros?

Author:

Game.19xz

source:

19xz.com

Because the user does not run the word permission on the server, it should be made in the server. 1. Start - run - DCMCNFG - Microsoft Word Document - Property - Identity - Interactive User Otherwise, "message Filter Display Application In Used Error 2. Start - Run - DCMCNFG - Microsoft Word Document - Properties - Security - Using Custom Access - Add Everyone or DCNFG - Microsoft Word Document - Property - Security - Using the default access DCOMCNFG - Default Security Mechanism - Default Access Permissions - Adding the Everyone Otherwise, "Reject Access Permissions" error, or unable to open macro storage

转载请注明原文地址:https://www.9cbs.com/read-96705.html

New Post(0)