C#操作word之插入图片

假如我们导出一份简历到word文档,那势必可能要同时导出我们包含的简历,下面就来试一下如何和通过C#代码,将图片插入到word文档中。

为了简便起见,就简单一点。类似下面这样的

姓名张三
照片 
 protected void InsertPtctureToExcel()
        {
            Word.Application app = null;
            Word.Document doc = null;
            try
            {
                object oMissing = System.Reflection.Missing.Value;
                //图片地址
                string fileName =Server.MapPath("photo.jpg");
                object linkToFile = false;
                object saveWithDocument = true;

                app = new Word.Application();
                doc = app.Documents.Add();
                Word.Table table = doc.Tables.Add(app.Selection.Range, 2, 2);
                table.Columns[1].Width = 100f;
                table.Columns[2].Width = 125f;
                table.Cell(1, 1).Range.Text = "姓名";
                table.Cell(1, 2).Range.Text = "张三";
                table.Cell(2,1).Range.Text = "照片";
                table.Cell(2, 2).Select();

                object range = app.Selection.Range;
                Word.InlineShape shape =  app.ActiveDocument.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref range);
                shape.Width = 100f;//图片宽度
                shape.Height = 120f;//图片高度
                shape.ConvertToShape().WrapFormat.Type = Word.WdWrapType.wdWrapSquare;//四周环绕的方式
                string newFile = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                string physicNewFile = Server.MapPath(newFile);
                doc.SaveAs(physicNewFile);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭文档
                }
                if (app != null)
                {
                    app.Quit();//退出应用程序
                }
            }
        }

 

转载于:https://www.cnblogs.com/fuyun2000/p/3139141.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值