zoukankan      html  css  js  c++  java
  • 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();//退出应用程序
                    }
                }
            }
  • 相关阅读:
    JavaScript节点介绍
    JavaScript DOM操作案例tab切换案例
    Binder系列10—总结
    Binder系列9—如何使用AIDL
    Binder系列8—如何使用Binder
    Binder系列7—framework层分析
    Binder系列2—Binder Driver再探
    Binder系列1—Binder Driver初探
    Binder系列3—启动ServiceManager
    Binder系列4—获取ServiceManager
  • 原文地址:https://www.cnblogs.com/fuyun2000/p/3139141.html
Copyright © 2011-2022 走看看