Today, I studied. NET to achieve word dynamic fill in the practice of data printing, feel quite a harvest ~
I've done excel related things before, so I have some knowledge of office com, and I found the COM and its help documents that need to be referenced well. The practice is to add COM--------Microsoft Word 11.0 Object Library in the reference. Then introduce namespaces:
Copy Code code as follows:
Using wordapplication = Microsoft.Office.Interop.Word.Application;
Using Document = Microsoft.Office.Interop.Word.Document;
Using Bookmark = Microsoft.Office.Interop.Word.Bookmark;
Specific implementation code:
Wordapplication word = null;
/**////<summary>
Implement dynamic Add data print Preview
</summary>
<param name= "path" >word template path </param>
<param name= "Adstu" > Dispensing student related information </param>
private void PrintPreview (string path, Adstuinfo adstu)
... {
if (word = = null)
... {
Word = new wordapplication ();
}
object omissing = System.Reflection.Missing.Value; This thing for a long time, unlike the operation of Excel, with type.mising not yet.
Object path1 = (object) path;
Document doc = null;
Doc = Word. Documents.openold (ref path1, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref Omiss ING, ref omissing, ref omissing);
Replace bookmark #region Replace bookmark
Object markname = "Applicant Unit";
Bookmark BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = Adstu.sisch;
Markname = "Apply for professional";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = Adstu.sima;
Markname = "examinee name 1";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = Adstu.name;
Markname = "examinee number";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = adstu.no;
Markname = "Dispensing major";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = ADSTU.ADMA;
Markname = "Print Time";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = GetDate (). ToString ();
Markname = "examinee Name 2";
BM = doc. Bookmarks.get_item (ref markname);
Bm. Range.Text = Adstu.name;
#endregion
Word. Visible = true;
Doc. PrintPreview ();
}
Specific explanation: First there is a template. doc file, in the template where you need to add data to set up a book signature,. NET program is to open the template, with the specific need to add value to replace the first set of the bookmark, and then return to the Word Print preview page, the idea is very simple ~ but the problem is also a drop ~ ~
This program is to do the printing of our college graduate students to do a certiorari letter, but the form is from the Graduate school department that got it, mainly in there covered the chapter ~ so you can only use those forms to play, but the table left empty is too small, can not use the same font to fight up, Strange can only blame the postgraduate department did not think we this will have this high-grade things ~ ~ Haha ~ estimated generally are hand-filled ~ ~ ~ Tomorrow the past do not know exactly how to get, estimated that this thing in the printing certiorari letter is not play a role ~ ~ ~ After saving will certainly be useful!
April 16 Modified:
Think about it a few days ago. There is still room for improvement. I did in front of a document, one by one, print, consider the data hundreds of later this printer printing efficiency is not high, because each time only to the printer to send a print task, it takes the next task is to take time, So my idea is to create a document that needs to be printed, so that not only can save the document, but also can send a task to the printer can print data, NB Printer printing speed is very fast ~ ~
Write down the specific operation of the idea is to open two Word documents (of course, is not visible during the build process, one or the other is to replace the bookmark, but instead of printing, copy to another document that stores all the tables that have been generated and gets the Word document. Print or save whatever they are. The specific code is as follows:
First in the using as:
Using Range = Microsoft.Office.Interop.Word.Range;
Doc. Content.copy (); Copy doc, Doc is a document that replaces bookmarks each time.
Object save = false;
Doc. Close (ref save, ref omissing, ref omissing); Close Doc.
Range range = Printdoc.content; All the contents of the Printdoc, Printdoc is the target save document.
Range. Collapse (ref omissing); To determine the location of the paste, in fact this place I want to put a valid parameter, the paste position as the end of the document, rather than the default document in the front, but the help document is written in VB, passed a direction:=wdcollapseend, I did not get out of the day, I still know VB ~ ~ Sad ~ so I forgot to pass a null value past, card for a while! Thank someone and I sent a few text messages, just a moment to get through to me ~
Range. Paste (); Paste to the target location, once completed.
Although this is the case, but in the production of the target document is also very resource-intensive, I looked at the program actually accounted for more than 100 m of memory!! and ~ ~ and ~ ~ My machine didn't stand up to the first two experiments! Panic ~ ~ At that time was really in the crazy ~ who knows how many times after the success of the silent! But think of the college that machine can carry live ~ ~ Hey continue to optimize it! This time the interface also changed, the above generated in a separate thread, That the main interface is not as dead as it is, and there is a lable in the main interface that shows the state of the build. This thing really works. ~ two times after the experiment, looked at the number of generated more than the previous two crashes ~ ~ Tense mood relieved ~ again look more than 100~~ happy ~ Finally slowly close to the completion of the number of ~ ~ So excited to be mad!!!!! Ah ~ ~
Hey ~ nonsense a bit more ~ useful on a few lines of code ~