C# 编写Word文档

效果图
在这里插入图片描述
界面设计
添加图片时,将图片复制到粘贴板后,右键picturebox,将图片添加到picturebox
在这里插入图片描述
1:使用之前需要先进行引用 Microsoft.Office.Interop.Word.dll

using MSWord = Microsoft.Office.Interop.Word;

//属性
 MSWord.Application wordApp;//Word应用程序变量 
 MSWord.Document wordDoc;
 object unite1 = MSWord.WdUnits.wdStory;
 object Nothing = Missing.Value;//Word文档变量
 string strContent;
 //方法
  /// <summary>
        /// Word初始化
        /// </summary>
        private void OneWord()
        {
            wordApp = new MSWord.Application(); //初始化                                                             //如果已存在,则删除           
            wordApp.Visible = true;//使文档可见
            #region 页面设置、页眉图片和文字设置,最后跳出页眉设置       
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //页面设置
            wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
            wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
            wordDoc.PageSetup.TopMargin = 57.0f;
            wordDoc.PageSetup.BottomMargin = 57.0f;
            wordDoc.PageSetup.LeftMargin = 57.0f;
            wordDoc.PageSetup.RightMargin = 57.0f;
            wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置         
            #endregion
            #region 页码设置并添加页码

            //为当前页添加页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
            pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;//设置页码的风格,是Dash形还是圆形的
            pns.HeadingLevelForChapter = 0;
            pns.IncludeChapterNumber = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber = 0; //开始页页码?
            object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
            object first = true;
            wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
            #endregion
            wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
        }

        /// <summary>
        /// 添加标题文字
        /// </summary>
        /// <param name="title"></param>
        private void AddTitle(string title)
        {
            try
            {
                //标题的
                wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
                wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";//设置字体
                wordDoc.Paragraphs.Last.Range.Font.Size = 15;//设置字体大小  

                wordApp.Selection.EndKey(ref unite1, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                strContent = title + "\n";
                wordDoc.Paragraphs.Last.Range.Text = strContent;
            }
            catch (Exception err)
            {
                MessageBox.Show("添加失败:" + err.Message);
            }
        }
        /// <summary>
        ///添加正文到Word文档里面 
        /// </summary>
        /// <param name="text"></param>
        private void addText(string text)
        {
            try
            {
                wordDoc.Paragraphs.Last.Range.Font.Bold = 0;
                wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";//设置字体
                wordDoc.Paragraphs.Last.Range.Font.Size = 12;//设置字体大小  
                wordDoc.Paragraphs.LineSpacing = 7;
                wordApp.Selection.EndKey(ref unite1, ref Nothing);//将光标移到文本末尾
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                strContent = text + "\n";
                wordDoc.Paragraphs.Last.Range.Text = strContent;
            }
            catch (Exception err)
            {
                MessageBox.Show("添加失败:" + err.Message);
            }
        }
        /// <summary>
        /// 插入图片
        /// </summary>
        /// <param name="TitlePicturePath"></param>
        private void AddPicture()
        {
            try
            {
                //要想插入的图片大小一样的话,保存图片时要指定好指定的大小
                System.Drawing.Bitmap objNewPic = new System.Drawing.Bitmap(pictureBox1.Image, 300, 300);//图片保存的大小尺寸 
                objNewPic.Save("1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                string TitlePicturePath = Path.Combine(Application.StartupPath, "1.jpg");

                wordApp.Selection.EndKey(ref unite1, ref Nothing); //将光标移动到文档末尾                                             
                object range = wordDoc.Paragraphs.Last.Range;
                //定义该插入的图片是否为外部链接
                Object linkToFile = false;//默认,这里貌似设置为bool类型更清晰一些 //定义要插入的图片是否随Word文档一起保存
                Object saveWithDocument = true;//默认 //使用InlineShapes.AddPicture方法(【即“嵌入型”】)插入图片
                wordDoc.InlineShapes.AddPicture(TitlePicturePath, ref linkToFile, ref saveWithDocument, ref range);
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片                                                                                                                           //在图下方居中添加图片标题
                wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
                wordApp.Selection.EndKey(ref unite1, ref Nothing);
                wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                wordApp.Selection.Font.Size = 10;//字体大小                               
            }
            catch (Exception err)
            {
                MessageBox.Show("添加失败:" + err.Message);
            }
        }

 /// <summary>
        /// 复制图片到粘贴板上后,右键picturebox,图片粘贴过去
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (Clipboard.ContainsImage())
                {
                    Image im = Clipboard.GetImage();
                    if (im != null)
                    {
                        pictureBox1.Image = im;
                    }
                }
            }
        }
  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值