C#使用spire.doc对word文档表格进行数据填充(主要针对word插入图像)(C # use spire.doc to fill the word document table with data (mainly for inserting images into word))

之前在做一个C#项目需要对word文档的表格进行数据填充,然后代码调用打印。如何用C#对word文档填充文字网上已经很多实例了,我就不多重复了,只提一点,找不到“文字型窗体域”的(我反正是找一下午没找到),可以网上下一个有文字型窗体域的word模板,复制黏贴就可以了。这里重点说针对表格的插入图片,这个网上找不到,完全我自己摸索出来的。有更好的方法欢迎指正。

    using Spire.Doc;     using Spire.Doc.Documents;     using Spire.Doc.Fields;//需要引用spire.doc的dll文件,网上很容易搜到                 class Docx         {             private string filePath=”C:\\Users\Administrator\\Desktop\\机动车考试成绩单.doc”;             private DocPicture dp;//doc图片             private Document Doc;//模板文件             private byte[] head;//存储人物头像的byte数组             //读取word文档             private void ReadDoc()             {                  Doc = new Document(filePath, FileFormat.Docx);//读取word文档模板                  head=ReadHead();//如何读取图片获取byte[]此处就不写详细代码了,网上有范例,我的另一遍博客里也有教程             }             //填充word文档             private void FillDoc()             {                 dp = Doc.Sections[0].Tables[0].Rows[0].Cells[4].Paragraphs[0].AppendPicture(head);                 //此处说说具体的参数                 //Section[0]————word文档页数列表, [0]表示取第一页                 //Tables[0]————当前页的表格列表,当一页存在多个表格时,按照从上往下的顺序排列,[0]表示取第一个表格                 //Rows[0]————当前所取表格的行的列表,按照从上往下的顺序排列,[0]表示取第一行                 //Cell[4]————当前表格所取行的格子列表,按照从左往右顺序排列,[4]表示取第五格,对于一个大格横跨多行的,取该格子最上部所在行的行数                 //Paragraphs[0]————取当前格的段落,默认0不用管它                 //终上所述,上面代码表意思就是,取第一页的第一个表格的第一行的第五个格子,将头像图片插入进去                 dp1.Height = 122; //设置图像的高                 dp1.Width = 100;//设置图像的宽                 dp1.VerticalPosition = 2; //设置图像的停靠位置方式                 dp1.TextWrappingStyle = TextWrappingStyle.InFrontOfText;//设置图像的文字环绕方式             }             //打印word文档             private void Print()             {                 PrintDialog dialog = new PrintDialog();                 dialog.AllowCurrentPage = true;                 dialog.AllowSomePages = true;                 dialog.UseEXDialog = true;                 try                 {                     Doc.PrintDialog = dialog;                     dialog.Document = Doc.PrintDocument;                     dialog.Document.Print();                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);                 }             }         }     题外话,如果涉及批量的填充打印的话,反复创建Document实例再读取模板其实是一件很耗费性能的事,这里推荐使用document.clone();方法     开局只需读取一次doc模板,每次需要填充打印时,只需对doc模板调用.clone()方法,然后对克隆的模板进行填充,打印,释放。     不过要注意,当文档里的一页上的表格超过五个时,无法克隆,调用.clone()方法时会失败,也许是因为用的spire.doc是免费版的关系?

————————

Before doing a c# project, you need to fill in the data of the table of the word document, and then call the code to print. There are many examples of how to use c# to fill text in word documents on the Internet. I won’t repeat it. Just mention that if I can’t find the “text form field” (I didn’t find it all afternoon anyway), I can copy and paste the next word template with text form field on the Internet. Here is the emphasis on the insertion picture of the form. I can’t find it on the Internet. I found it all by myself. There is a better way to welcome correction.

using Spire.Doc;     using Spire.Doc.Documents;     using Spire.Doc.Fields;// You need to reference the DLL file of spire.doc, which is easy to find on the Internet                 class Docx         {             Private string filepath = “C: \ \ users \ administrator \ \ desktop \ \ motor vehicle test transcript. Doc”;             Private docpicture DP; / / doc picture             Private document doc; / / template file             Private byte [] head; / / store the byte array of character avatars             // Read word document             private void ReadDoc()             {                  Doc = new document (filepath, fileformat. Docx); / / read the word document template                  Head = readhead(); / / how to read a picture and get a byte [] there is no detailed code here. There are examples on the Internet and tutorials in my blog             }             // Fill word document             private void FillDoc()             {                 dp = Doc.Sections[0].Tables[0].Rows[0].Cells[4].Paragraphs[0].AppendPicture(head);                 // Here are the specific parameters                 // Section [0] – list of pages of word document, [0] means to take the first page                 // Tables [0] – the table list of the current page. When there are multiple tables on a page, they are arranged from top to bottom, [0] means to take the first table                 // Rows [0] – the list of rows of the currently selected table, arranged from top to bottom, [0] means to take the first row                 // Cell [4] – the grid list of the rows taken in the current table, arranged from left to right, [4] means to take the fifth grid. For a large grid spanning multiple rows, take the number of rows in the top row of the grid                 // Paragraphs [0] – take the paragraph of the current grid, default 0, and ignore it                 // Finally, the above code table means to take the fifth grid of the first row of the first table on the first page and insert the avatar picture                 DP1. Height = 122; / / set the height of the image                 DP1. Width = 100; / / set the width of the image                 DP1. Verticalposition = 2; / / set the image docking position mode                 DP1. Textwrappingstyle = textwrappingstyle. Infrontoftext; / / sets the text wrapping mode of the image             }             // Print word document             private void Print()             {                 PrintDialog dialog = new PrintDialog();                 dialog.AllowCurrentPage = true;                 dialog.AllowSomePages = true;                 dialog.UseEXDialog = true;                 try                 {                     Doc.PrintDialog = dialog;                     dialog.Document = Doc.PrintDocument;                     dialog.Document.Print();                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);                 }             }         }     Incidentally, if batch filling and printing are involved, repeatedly creating document instances and then reading templates is actually a very performance-consuming task. The document. Clone(); method is recommended here     At the beginning, you only need to read the doc template once. Each time you need to fill and print, you only need to call the. Clone () method on the doc template, and then fill, print and release the cloned template.     However, it should be noted that when there are more than five tables on a page in the document, it cannot be cloned. Calling the. Clone () method will fail, perhaps because the spire.doc used is a free version?