C# VS2012操作word文档 (二).插入表格图片
在上一篇文章"C# VS2012創(chuàng)建word文檔.(一)"中我們講述了如何使用VS2012引用COM中Miscrosoft Word 14.0 Object Library實(shí)現(xiàn)創(chuàng)建文檔,而這篇文章將講述如何添加表格和圖片,因?yàn)槲以贑#聯(lián)系數(shù)據(jù)庫(kù)做銷售系統(tǒng)中需要打印表單,我想以圖表形式顯示在word中,同時(shí)生成相應(yīng)的餅狀圖或柱狀圖,所以才有查閱了相關(guān)資料,完成文章,供大家分享.其中使用openFileDialog控件也是希望大家學(xué)習(xí)了解下.
一. 界面設(shè)置
設(shè)計(jì)界面如下圖所示,其中對(duì)用的5個(gè)textBox和2個(gè)button控件在圖中標(biāo)明,同時(shí)添加一個(gè)openFileDialog控件,在插入圖片時(shí)點(diǎn)擊"選擇"按鈕實(shí)現(xiàn)打開(kāi)一個(gè)選擇圖片窗體,選擇后在textBox5只讀中顯示相應(yīng)圖片的路徑.
二. 源代碼
1.引用空間
//引用word對(duì)象類庫(kù)和命名空間 using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection;2.添加外部變量
object path; //聲明文件路徑變量 MSWord.Application wordApp; //聲明word應(yīng)用程序變量 MSWord.Document worddoc; //聲明word文檔變量3.通過(guò)openFileDialog實(shí)現(xiàn)顯示打開(kāi)圖片路徑
點(diǎn)擊"選擇"按鈕在生成的button2_Click(object sender, EventArgs e)函數(shù)中添加如下代碼,其中openFileDialog1.Filter是設(shè)置打開(kāi)文件類型,此處為jpg和bmp型,然后把選擇的圖片路徑賦值給textBox5.代碼如下圖所示:
//點(diǎn)擊"選擇"添加圖片 textBox5為只讀 private void button2_Click(object sender, EventArgs e) {//定義openFileDialog打開(kāi)圖片對(duì)話框文件類型openFileDialog1.Filter = "BMP格式圖片(*.bmp)|*.bmp|JPG格式圖片(*.jpg)|*.jpg";if (openFileDialog1.ShowDialog() == DialogResult.OK) //點(diǎn)擊"確定"按鈕執(zhí)行{if (openFileDialog1.FileName != "") //圖片路徑賦值給textBox5{this.textBox5.Text = openFileDialog1.FileName;}} }運(yùn)行程序后,添加圖片時(shí)openFileDialog的效果如下圖所示,右下角有兩種圖片選擇格式供選擇:
4.插入表格和圖片
點(diǎn)擊"創(chuàng)建"按鈕在生成的函數(shù)button1_Click(object sender, EventArgs e)中添加實(shí)現(xiàn)向word中插入表格和圖片的代碼,如下:
//點(diǎn)擊"創(chuàng)建"按鈕實(shí)現(xiàn)創(chuàng)建word文件 private void button1_Click(object sender, EventArgs e) {if (textBox1.Text == "" || textBox2.Text == ""){MessageBox.Show("請(qǐng)輸入路徑和文檔名信息");}else{ //初始化變量object Nothing = Missing.Value; //表示缺少的值object format = MSWord.WdSaveFormat.wdFormatDocumentDefault; //格式docxwordApp = new MSWord.ApplicationClass(); //聲明一個(gè)wordAPP對(duì)象worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //定義word文檔中表格MSWord.Table table = worddoc.Tables.Add(wordApp.Selection.Range,Convert.ToInt32(textBox3.Text),Convert.ToInt32(textBox4.Text),ref Nothing,ref Nothing); //定義一個(gè)表格對(duì)象table.Borders.Enable = 1; //默認(rèn)表格沒(méi)有邊框//填充表格中內(nèi)容for (int i = 1; i <= Convert.ToInt32(textBox3.Text); i++) //string轉(zhuǎn)換int型{for (int j = 1; j <= Convert.ToInt32(textBox4.Text); j++){table.Cell(i, j).Range.Text= "(" + i + "行," + j + "列)" ;}}//定義插入圖片是否為外部鏈接Object linktofile = false;Object savedocument = true;Object range = worddoc.Paragraphs.Last.Range; //定義圖片插入word位置worddoc.InlineShapes.AddPicture(textBox5.Text,ref linktofile,ref savedocument,ref range);//保存文檔path = textBox2.Text + "\\" + textBox1.Text; //設(shè)置文件保存路勁worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);//關(guān)閉文檔worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉worddoc文檔對(duì)象wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉wordApp組對(duì)象MessageBox.Show("文檔創(chuàng)建成功!");} }三. 運(yùn)行結(jié)果
點(diǎn)擊運(yùn)行,填寫如下圖所示的內(nèi)容,其中插入表格函數(shù)行數(shù)=8,列數(shù)=5并插入圖片:
點(diǎn)擊“創(chuàng)建”后,它會(huì)在E盤下創(chuàng)建一個(gè)test.docx的word文檔,同時(shí)填寫內(nèi)容如下圖所示:
四. 補(bǔ)充知識(shí)
其中在插入圖片中我使用了一個(gè)InlineShapes.AddPicture函數(shù),它相應(yīng)的使用方法如下圖所示,來(lái)自http://technet.microsoft.com/zh-cn/library/ff822636
五. 總結(jié)
這篇文章主要是使用C#向創(chuàng)建word文檔中添加表格和圖片的操作,同時(shí)如果怎樣使用C#創(chuàng)建word還有不明白的可以參考前一篇文章http://blog.csdn.net/eastmount/article/details/11235577同時(shí)該文章有些內(nèi)容思想來(lái)自劉麗霞等編寫的《C#范例開(kāi)發(fā)大全》,感謝作者,同時(shí)希望大家能看看這本書籍,最后希望文章對(duì)大家有幫助,同時(shí)有不足或錯(cuò)誤的地方,見(jiàn)諒!
(By:Eastmount 2013-9-8 夜1點(diǎn)http://blog.csdn.net/eastmount/)
總結(jié)
以上是生活随笔為你收集整理的C# VS2012操作word文档 (二).插入表格图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C# VS2012操作word文档 (一
- 下一篇: C# 数据库dataGridView刷新