C# 编写Word文档
生活随笔
收集整理的這篇文章主要介紹了
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);#endregionwordApp.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;}}}}總結
以上是生活随笔為你收集整理的C# 编写Word文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农历2017年8月初4_2017年8月4
- 下一篇: AutoML 学习