629
小米
C# VS2012操作word文檔 (二).插入表格圖片
在上一篇文章"C# VS2012創建word文檔.(一)"中我們講述了如何使用VS2012引用COM中Miscrosoft Word 14.0 Object Library實現創建文檔,而這篇文章將講述如何添加表格和圖片,因為我在C#聯係數據庫做銷售係統中需要打印表單,我想以圖表形式顯示在word中,同時生成相應的餅狀圖或柱狀圖,所以才有查閱了相關資料,完成文章,供大家分享.其中使用openFileDialog控件也是希望大家學習了解下.
一. 界麵設置
設計界麵如下圖所示,其中對用的5個textBox和2個button控件在圖中標明,同時添加一個openFileDialog控件,在插入圖片時點擊"選擇"按鈕實現打開一個選擇圖片窗體,選擇後在textBox5隻讀中顯示相應圖片的路徑.

二. 源代碼
1.引用空間
//引用word對象類庫和命名空間 using MSWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection;
2.添加外部變量
object path; //聲明文件路徑變量 MSWord.Application wordApp; //聲明word應用程序變量 MSWord.Document worddoc; //聲明word文檔變量
3.通過openFileDialog實現顯示打開圖片路徑
點擊"選擇"按鈕在生成的button2_Click(object sender, EventArgs e)函數中添加如下代碼,其中openFileDialog1.Filter是設置打開文件類型,此處為jpg和bmp型,然後把選擇的圖片路徑賦值給textBox5.代碼如下圖所示:
//點擊"選擇"添加圖片 textBox5為隻讀
private void button2_Click(object sender, EventArgs e)
{
//定義openFileDialog打開圖片對話框文件類型
openFileDialog1.Filter = "BMP格式圖片(*.bmp)|*.bmp|JPG格式圖片(*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK) //點擊"確定"按鈕執行
{
if (openFileDialog1.FileName != "") //圖片路徑賦值給textBox5
{
this.textBox5.Text = openFileDialog1.FileName;
}
}
}
運行程序後,添加圖片時openFileDialog的效果如下圖所示,右下角有兩種圖片選擇格式供選擇:

4.插入表格和圖片
點擊"創建"按鈕在生成的函數button1_Click(object sender, EventArgs e)中添加實現向word中插入表格和圖片的代碼,如下:
//點擊"創建"按鈕實現創建word文件
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("請輸入路徑和文檔名信息");
}
else
{
//初始化變量
object Nothing = Missing.Value; //表示缺少的值
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault; //格式docx
wordApp = new MSWord.ApplicationClass(); //聲明一個wordAPP對象
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); //定義一個表格對象
table.Borders.Enable = 1; //默認表格沒有邊框
//填充表格中內容
for (int i = 1; i <= Convert.ToInt32(textBox3.Text); i++) //string轉換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; //設置文件保存路勁
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);
//關閉文檔
worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //關閉worddoc文檔對象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //關閉wordApp組對象
MessageBox.Show("文檔創建成功!");
}
}
三. 運行結果
點擊運行,填寫如下圖所示的內容,其中插入表格函數行數=8,列數=5並插入圖片:

點擊“創建”後,它會在E盤下創建一個test.docx的word文檔,同時填寫內容如下圖所示:

四. 補充知識
其中在插入圖片中我使用了一個InlineShapes.AddPicture函數,它相應的使用方法如下圖所示,來自https://technet.microsoft.com/zh-cn/library/ff822636

五. 總結
這篇文章主要是使用C#向創建word文檔中添加表格和圖片的操作,同時如果怎樣使用C#創建word還有不明白的可以參考前一篇文章https://blog.csdn.net/eastmount/article/details/11235577同時該文章有些內容思想來自劉麗霞等編寫的《C#範例開發大全》,感謝作者,同時希望大家能看看這本書籍,最後希望文章對大家有幫助,同時有不足或錯誤的地方,見諒!
(By:Eastmount 2013-9-8 夜1點https://blog.csdn.net/eastmount/)
最後更新:2017-04-03 15:21:43