比擬全的一個C#操作word文檔示例。本站提示廣大學習愛好者:(比擬全的一個C#操作word文檔示例)文章只能為提供參考,不一定能成為您想要的結果。以下是比擬全的一個C#操作word文檔示例正文
比來兩天研討了一下若何應用VS2008(C#說話)輸入Word文檔。以下是幾點總結:
1、異常簡略。
2、開辟及運轉情況請求。操作體系為:WindowsXP(裝置.net framework2.0)/Vista/Win7;在操作體系必需裝置Word2003完整裝置版。這裡必需要強調是Word2003完整裝置版,由於軟件開辟及運轉都須要一個com組件:Microsoft word 11.0 Object Library。假如不是Word2003完整裝置版,可以下載這個com組件,並手動的裝置這個com組件。下載地址為:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=20923,文件不年夜,只要4M閣下。
3、C#工程設置。這裡的工程設置,就是添加com組件。步調為:在工程資本治理器中"添加援用"->"com"選項卡->鄙人拉列表當選Microsoft word 11.0 Object Library。ok了,看上去,跟添加普通的dll一樣,但現實上vs2008在這個進程中完成一系列龐雜的關於.net挪用com組件的操作,不外,幸虧我們不消管這個。
4、接下就是寫代碼了。在這裡,應用Word的com對像,跟應用普通的非com對像一樣,異常流利,似乎基本就不論它甚麼com不com的。為了使代碼比擬簡練,可以在源代碼文件頂添加如許的一條語句:using Word = Microsoft.Office.Interop.Word;
5、最好是對word對像模子有必定的懂得,如許在寫代碼的時刻就不會那末“渺茫”了。wore對像模子中有幾個比擬主要的對像,它們是Application、Document、Selection、Range、Bookmark,和其它的一些對像,如:Paragraph、Section、Table品級。剛開端學的時刻,感到Selection、Range、Bookmark這幾個對像有點困惑人,Selection能夠好懂得,就是表現以後的選擇區域,假如沒有選擇就表現光標地點地位。Range和Bookmark,其其實許多處所很像,不外也有一些差別,在這裡就不多說了,谷歌一下"word.Range"就好了。
6、在寫代碼的進程中,常常會想要完成的一些操作,然則因為對word對像不熟習而不知怎樣用代碼完成。好比設置頁眉、添加頁碼甚麼的,假如在Word法式內行動的操作固然很簡略,然則要用代碼來完成,對初學者來講便可能不那末輕易了。碰到這類情形,普通有兩種辦法可以選擇:一種是"百度/谷歌法",別一種,也是我所推舉的一種就是,應用Word的“錄制宏”功效把想要完成的操作錄成宏以後,再看宏裡的代碼,宏裡的代碼其實簡直就是你想要的代碼了(只不外語法有一點紛歧樣罷了)。
7、以下給出一個示例,這個示例外面包含了一些經常使用的圖、文、表、公式的編纂與排版和頁面設置、頁眉、頁碼的操作,外面都有正文,寫得很清晰。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Microsoft.Office.Interop; using Word = Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 :System.Windows.Forms. Form { [DllImport("shell32.dll ")] public static extern int ShellExecute(IntPtr hwnd, String lpszOp, String lpszFile, String lpszParams, String lpszDir, int FsShowCmd); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //新建文檔 // Word.Application newapp = new Word.Application();//用這句也能初始化 Word.Application newapp = new Word.ApplicationClass(); Word.Document newdoc; object nothing=System.Reflection.Missing.Value;//用於作為函數的默許參數 newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一個word文檔 newapp.Visible = true ;//能否顯示word法式界面 //頁面設置 //newdoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape ; //newdoc.PageSetup.PageWidth = newapp.CentimetersToPoints(21.0f); //newdoc.PageSetup.PageHeight = newapp.CentimetersToPoints(29.7f); newdoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4; newdoc.PageSetup.Orientation = Word.WdOrientation.wdOrientPortrait; newdoc.PageSetup.TopMargin = 57.0f; newdoc.PageSetup.BottomMargin = 57.0f; newdoc.PageSetup.LeftMargin = 57.0f; newdoc.PageSetup.RightMargin = 57.0f; newdoc.PageSetup.HeaderDistance = 30.0f;//頁眉地位 //設置頁眉 newapp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;//視圖款式。 newapp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;//進入頁眉設置,個中頁眉邊距在頁面設置中已完成 newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; //拔出頁眉圖片 string headerfile = "d:\\header.jpg"; this.outpicture(headerfile, Properties.Resources.header); Word.InlineShape shape1= newapp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref nothing, ref nothing, ref nothing); shape1.Height = 30; shape1.Width = 80; newapp.ActiveWindow.ActivePane.Selection.InsertAfter("中建西南院"); //去失落頁眉的那條橫線 newapp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleNone; newapp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false; newapp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//加入頁眉設置 //添加頁碼 Word.PageNumbers pns= newapp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers; pns.NumberStyle = Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; pns.HeadingLevelForChapter = 0; pns.IncludeChapterNumber = false; pns.ChapterPageSeparator = Word.WdSeparatorType.wdSeparatorHyphen; pns.RestartNumberingAtSection = false; pns.StartingNumber = 0; object pagenmbetal=Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object first=true; newapp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages ].PageNumbers.Add(ref pagenmbetal, ref first); //文字設置(Selection表現以後選擇集,假如以後沒有選擇對像,則指對光標地點處停止設置) newapp.Selection.Font.Size = 14; newapp.Selection.Font.Bold = 0; newapp.Selection.Font.Color = Word.WdColor.wdColorBlack; newapp.Selection.Font.Name = "宋體"; //段落設置 newapp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly; newapp.Selection.ParagraphFormat.LineSpacing = 20; newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; newapp.Selection.ParagraphFormat.FirstLineIndent = 30; newdoc.Content.InsertAfter( WindowsFormsApplication1.Properties.Resources.PreViewWords); //拔出公式 object oEndOfDoc="\\endofdoc"; Word.Range rang1 = newdoc.Bookmarks.get_Item(ref oEndOfDoc).Range; object fieldType = Word.WdFieldType.wdFieldEmpty; object formula = @"eq \i(a,b,ξxdx)"; object presrveFormatting = false; rang1.Text = formula.ToString(); rang1.Font.Size = 14; rang1.Font.Bold = 0; rang1.Font.Subscript = 0; rang1.Font.Color = Word.WdColor.wdColorBlue; rang1.Font.Name = "宋體"; rang1.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle; rang1.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; newdoc.Fields.Add(rang1, ref fieldType, ref formula, ref presrveFormatting); //將文檔的前三個字調換成"asdfasdf",並將其色彩設為藍色 object start=0; object end=3; Word.Range rang2 = newdoc.Range(ref start, ref end); rang2.Font.Color = Word.WdColor.wdColorBlue; rang2.Text = "as簽"; //將文檔開首的"as"調換成"袁波" rang1.Start = 0; rang1.End = 2; rang1.Text = "這是一個"; rang1.InsertAfter("書"); //rang1.Select(); object codirection = Word.WdCollapseDirection.wdCollapseStart; rang1.Collapse(ref codirection);//將rang1的終點和起點都定於終點或起點 //對前三個字符停止加粗 newdoc.Range(ref start, ref end).Bold = 1; object rang = rang2; newdoc.Bookmarks.Add("yb",ref rang); object unite = Word.WdUnits.wdStory; newapp.Selection.EndKey(ref unite, ref nothing);//將光標移至文末 newapp.Selection.Font.Size = 10; newapp.Selection.TypeText("...............................(式1)\n"); //拔出圖片 newapp.Selection.EndKey(ref unite, ref nothing);//將光標移至文末 //newapp.Selection.HomeKey(ref unite, ref nothing);//將光標移至文開首 newapp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle; newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; object LinkToFile = false; object SaveWithDocument = true; object Anchor = newapp.Selection.Range; string picname = "d:\\kk.jpg"; this.outpicture(picname, Properties.Resources.IMG_2169); newdoc.InlineShapes.AddPicture(picname, ref LinkToFile, ref SaveWithDocument, ref Anchor); newdoc.InlineShapes[1].Height = 200; newdoc.InlineShapes[1].Width = 200; newdoc.Content.InsertAfter("\n"); newapp.Selection.EndKey(ref unite, ref nothing);//將光標移至文末 newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; newapp.Selection.Font.Size = 10; newapp.Selection.TypeText("圖1 袁冶\n"); newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; newdoc.Content.InsertAfter("\n"); newdoc.Content.InsertAfter("\n"); //用這類方法也能夠拔出公式,而且這類辦法更簡略 newapp.Selection.Font.Size = 14; newapp.Selection.InsertFormula(ref formula, ref nothing); newapp.Selection.Font.Size = 10; newapp.Selection.TypeText("..............................(式2)\n"); newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; newapp.Selection.TypeText("表1 電子產物\n"); //拔出表格 Word.Table table1 = newdoc.Tables.Add(newapp.Selection.Range, 4, 3, ref nothing, ref nothing); newdoc.Tables[1].Cell(1, 1).Range.Text = "產物\n項目"; newdoc.Tables[1].Cell(1, 2).Range.Text = "電腦"; newdoc.Tables[1].Cell(1, 3).Range.Text = "手機"; newdoc.Tables[1].Cell(2, 1).Range.Text = "分量(kg)"; newdoc.Tables[1].Cell(3, 1).Range.Text = "價錢(元)"; newdoc.Tables[1].Cell(4, 1).Range.Text = "配合信息"; newdoc.Tables[1].Cell(4, 2).Range.Text = "信息A"; newdoc.Tables[1].Cell(4,3).Range.Text = "信息B"; table1.Select(); table1.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;//全部表格居中 newapp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; newapp.Selection.Cells.HeightRule = Word.WdRowHeightRule.wdRowHeightExactly; newapp.Selection.Cells.Height = 40; table1.Rows[2].Height = 20; table1.Rows[3].Height = 20; table1.Rows[4].Height = 20; table1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; newapp.Selection.Cells.Width=150; table1.Columns[1].Width = 75; table1.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; table1.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; //表頭斜線 table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown ].Visible = true; table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color = Word.WdColor.wdColorGreen; table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Word.WdLineWidth.wdLineWidth050pt; //表格邊框 table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal ].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom ].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleDouble; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop ].Visible = true; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].Color = Word.WdColor.wdColorGreen; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth050pt; table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleDouble; //歸並單位格 newdoc.Tables[1].Cell(4, 2).Merge(table1.Cell(4, 3)); //刪除圖片 this.delpictfile(headerfile); this.delpictfile(picname); //保留文檔 object name = "c:\\yb3.doc"; newdoc.SaveAs(ref name, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing); //封閉文檔 object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges; newdoc.Close(ref nothing , ref nothing, ref nothing); newapp.Application.Quit(ref saveOption, ref nothing, ref nothing); newdoc = null; newapp = null; ShellExecute(IntPtr.Zero, "open", "c:\\yb3.doc", "", "", 3); } private void outpicture(string filename,System.Drawing.Bitmap bmap) { bmap.Save(filename); } private void delpictfile(string filename) { System.IO.File.Delete(filename); } } }