c#导出包含图片的word文档

本文介绍了一个使用Microsoft Office Interop实现的自动化脚本,用于创建Word文档,包括添加页眉、设置文档样式、插入图片及落款信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 object oMissing = Missing.Value;
         object oEndOfDoc = "\\endofdoc"; //指定编码
         //bool createByMe;
         //开始
         Microsoft.Office.Interop.Word._Application oWord;//声明一个Word应用程序
         Microsoft.Office.Interop.Word._Document oDoc;//声明一个Word文档
         //if (System.IO.File.Exists(fileName.ToString()))
         //{
         //    System.IO.File.Delete(fileName.ToString());
         //}
         //try
         //{
         //    //如果有正在运行的word实例,则直接采用当前的word实例,
         //    //否则,直接创建新实例,在最后退出时会报模板正在使用中等问题
         //    oWord = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");
         //    createByMe = false;
         //}
         //catch
         //{
             oWord = new Microsoft.Office.Interop.Word.Application();
         //    createByMe = true;
         //}
       
         oWord.Visible = true;//显示此文档
          oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
         //添加页眉
         //oWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
         //oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
         //oWord.ActiveWindow.ActivePane.Selection.InsertAfter("导出模板");
         //oWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置右对齐
         //oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置

         oWord.Selection.ParagraphFormat.LineSpacing = 3f;//设置文档的行间距


      
         //Insert a paragraph at the beginning of the document.
         //在文档开始的地方添加一个段落
         Microsoft.Office.Interop.Word.Paragraph oPara1;
         oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);

         //在此段落的行中添加字符串
         oPara1.Range.Text ="";
         //oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
         oPara1.Range.Font.Bold = 1;//加粗1
         oPara1.Range.Font.Size = 17;
         oPara1.Format.SpaceAfter = 24;      //24 pt spacing after paragraph.(在此段落之后添加24磅的行距)
         oPara1.Range.InsertParagraphAfter();

         
         //Insert a paragraph at the end of the document.
         //在文档最后添加一个段落
         Microsoft.Office.Interop.Word.Paragraph oPara2;
         //object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;//实例化一个行
         oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);//将oRng这个行添加到文档中并返回
         oPara2.Range.Text = ""  ;//在此段落的行中添加字符串
         oPara2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
         oPara2.Range.Font.Size = 12;
         oPara2.Format.SpaceAfter = 0;//在此段落之后添加6磅的行距
         //oPara2.Range.Text = dict.CompanyName;
         oPara2.Range.InsertParagraphAfter();

         Microsoft.Office.Interop.Word.Paragraph oPara3;
         //object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;//实例化一个行
         oPara3 = oDoc.Content.Paragraphs.Add(ref oMissing);//将oRng这个行添加到文档中并返回
         oPara3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
         oPara3.Range.Font.Size = 12;
         oPara3.Range.Text ="          ";//在此段落的行中添加字符串
         oPara3.Format.SpaceBefore = 6;
         oPara3.Range.InsertParagraphAfter();

         Microsoft.Office.Interop.Word.Paragraph oPara4;
         //object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;//实例化一个行
         oPara4 = oDoc.Content.Paragraphs.Add(ref oMissing);//将oRng这个行添加到文档中并返回
         oPara4.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
         oPara4.Range.Font.Size = 12;
         oPara4.Range.Text ="  ";//在此段落的行中添加字符串
         oPara4.Format.SpaceBefore = 6;
         oPara4.Range.InsertParagraphAfter();

    

         //插入图片
         string FileName = “”;//图片所在路径
      
                object LinkToFile = false;
                object SaveWithDocument = true;
                object Anchor = oDoc.Application.Selection.Range;
                object top = 80;
                object left = 2;
                object width = 50;
                object height = 40;
                string sourefile = Server.MapPath("\\Content\\File");
                oDoc.Shapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref left, ref top, ref width, ref height, ref Anchor);
       
         //object LinkToFile = false;
         //object SaveWithDocument = true;
         //object Anchor = oDoc.Application.Selection.Range;
         //object top = 84;
         //object left = 2;
         //object width = 50;
         //object height = 40;
         //string sourefile = Server.MapPath("\\Content\\File");
         //oDoc.Shapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref left, ref top, ref width, ref height, ref Anchor);
        }
         //oDoc.Save();
         //oDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
         //oDoc.Application.ActiveDocument.InlineShapes[1].Width = 50;//图片宽度(单位为磅,1磅=0.353毫米=0.0353厘米),这里是把9CM转换为磅
         //oDoc.Application.ActiveDocument.InlineShapes[1].Height = 40;//图片高度
         
         
         //将图片设置为四周环绕型
         //Microsoft.Office.Interop.Word.Shape s = oDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
         //s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline

         //oDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
         //oDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;


         //oWord.PrintPreview = true;//打开文档预览

         //文件保存
         string fileName = string.Format("{0}.doc", DateTime.Now.ToString("yyyyMMddHHmmss"));
         string ss = Server.MapPath(string.Format("\\Content\\File\\{0}", fileName));
         object filename = ss;  //文件保存路径
         //执行保存
         oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
         //关闭文档
         oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
         //关闭文档应用对象
         oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

转载于:https://www.cnblogs.com/syfblog/archive/2012/12/06/2804639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值