C# VS2012操作word文档 (二).插入表格图片

来源:互联网 发布:python 字符串函数 编辑:程序博客网 时间:2024/04/29 12:14

在上一篇文章"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函数,它相应的使用方法如下图所示,来自http://technet.microsoft.com/zh-cn/library/ff822636

五. 总结

这篇文章主要是使用C#向创建word文档中添加表格和图片的操作,同时如果怎样使用C#创建word还有不明白的可以参考前一篇文章http://blog.csdn.net/eastmount/article/details/11235577同时该文章有些内容思想来自刘丽霞等编写的《C#范例开发大全》,感谢作者,同时希望大家能看看这本书籍,最后希望文章对大家有帮助,同时有不足或错误的地方,见谅!

(By:Eastmount 2013-9-8 夜1点http://blog.csdn.net/eastmount/)