亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

比較全的一個C#操作word文檔示例

2020-01-24 01:46:52
字體:
來源:轉載
供稿:網友

最近兩天研究了一下如何使用VS2008(C#語言)輸出Word文檔。以下是幾點總結:

1、非常簡單。

2、開發及運行環境要求。操作系統為:WindowsXP(安裝.net framework2.0)/Vista/Win7;在操作系統必須安裝Word2003完全安裝版。這里必須要強調是Word2003完全安裝版,因為軟件開發及運行都需要一個com組件:Microsoft word 11.0 Object Library。如果不是Word2003完全安裝版,可以下載這個com組件,并手動的安裝這個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,其實在很多地方很像,不過也有一些區別,在這里就不多說了,google一下"word.Range"就行了。

6、在寫代碼的過程中,經常會想要實現的一些操作,但是由于對word對像不熟悉而不知怎么用代碼實現。比如設置頁眉、添加頁碼什么的,如果在Word程序里手動的操作當然很簡單,但是要用代碼來實現,對初學者來說就可能不那么容易了。遇到這種情況,一般有兩種方法可以選擇:一種是"百度/google法",別一種,也是我所推薦的一種就是,利用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="http://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);    }     }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美激情久久久| 欧美日韩一区二区三区在线免费观看| 亚洲免费一在线| 欧美床上激情在线观看| 亚洲精品欧美极品| 成人黄色短视频在线观看| 亚洲人成在线观| 中文字幕日韩综合av| 亚洲男人的天堂在线| 日韩欧美国产骚| 国内精品久久久久久影视8| 日韩中文在线中文网在线观看| 6080yy精品一区二区三区| 欧美成人一区二区三区电影| 欧美高清激情视频| 国产原创欧美精品| 精品国产一区二区三区久久久| 热久久视久久精品18亚洲精品| 国产欧美一区二区三区在线| 久久久伊人日本| 欧美性猛交99久久久久99按摩| 久久精品福利视频| 久热国产精品视频| 欧美日韩国产成人在线| 欧美高清视频在线| 精品久久久久久中文字幕大豆网| 亚洲女在线观看| 九九热这里只有精品6| 不卡毛片在线看| 久久99视频精品| 日本一区二区三区在线播放| 国产成人精品一区二区在线| 亚洲乱码av中文一区二区| 97成人超碰免| 日韩精品视频免费专区在线播放| 日韩中文字在线| 日韩精品在线视频观看| 国产精品∨欧美精品v日韩精品| 欧美福利在线观看| 久久精品久久精品亚洲人| 日韩av影片在线观看| 91国产中文字幕| 亚洲人成在线电影| 2019最新中文字幕| 精品无人国产偷自产在线| 91免费在线视频| 国产视频精品一区二区三区| 美女av一区二区三区| 午夜免费久久久久| 亚洲精品国产综合久久| 久久艳片www.17c.com| 精品视频在线播放免| 91探花福利精品国产自产在线| 国产精品视频久久| 8050国产精品久久久久久| 日韩中文字幕在线| 国产在线98福利播放视频| 亚洲精品久久久久中文字幕二区| 久久6精品影院| 麻豆精品精华液| 国产在线拍偷自揄拍精品| 国产69精品久久久久99| 亚洲欧洲偷拍精品| 亚洲图片欧美午夜| 日本亚洲精品在线观看| 91大神福利视频在线| 91免费高清视频| 欧美激情欧美狂野欧美精品| 欧洲成人免费aa| 久久久久久久91| 91wwwcom在线观看| 日韩av片永久免费网站| 日韩欧美第一页| 一区二区三区国产在线观看| 日韩综合视频在线观看| 亚洲精品videossex少妇| 久久久精品免费视频| 国产精品久久久久久久午夜| 色综合视频一区中文字幕| 国产精品久久久久久五月尺| 1769国内精品视频在线播放| 激情av一区二区| 色噜噜国产精品视频一区二区| 国产精品电影观看| 日本精品中文字幕| 国产精品中文字幕在线| 久久香蕉国产线看观看av| 亚洲香蕉成视频在线观看| 欧美成人免费一级人片100| 欧美激情啊啊啊| 精品无人区乱码1区2区3区在线| 久久成人这里只有精品| 国产精品视频26uuu| 亚洲另类激情图| 亚洲综合自拍一区| 日本中文字幕不卡免费| 精品无人区太爽高潮在线播放| 日韩成人av在线| 亚洲影院色无极综合| 亚洲三级免费看| 国产精品久久久久久五月尺| 国产日韩精品在线播放| 色婷婷综合成人av| 国产一区二区精品丝袜| 国产97色在线|日韩| 日韩欧美在线字幕| 97视频在线观看播放| 久久精品视频在线| 亚洲激情在线观看视频免费| 国产欧美 在线欧美| 国产精品稀缺呦系列在线| 亚洲综合日韩中文字幕v在线| 亚洲天堂网在线观看| 欧美国产精品va在线观看| 日韩欧美视频一区二区三区| 亚洲天堂av在线免费观看| 欧美亚洲视频在线观看| 3344国产精品免费看| 国产性色av一区二区| 成人在线一区二区| 中文亚洲视频在线| 欧美精品免费在线| 国产一区av在线| 日日骚av一区| 久久精品国产久精国产思思| 日韩av观看网址| 国产成人精品在线| 国产精品视频免费观看www| 91青草视频久久| 色多多国产成人永久免费网站| 欧美高清性猛交| 国产这里只有精品| 亚洲精品国产精品久久清纯直播| 精品国产拍在线观看| 国产综合色香蕉精品| 午夜精品久久久久久久99热浪潮| 欧美激情国内偷拍| 日本乱人伦a精品| 啊v视频在线一区二区三区| 欧美一级大片在线观看| 狠狠色狠狠色综合日日小说| 九九久久久久99精品| 日韩av男人的天堂| 一道本无吗dⅴd在线播放一区| 日韩中文综合网| 日韩欧美在线观看视频| 在线观看成人黄色| 日韩专区在线播放| 国产999精品久久久影片官网| 亚洲大胆美女视频| 色妞在线综合亚洲欧美| 日韩男女性生活视频| 欧美一区二区三区四区在线| 日韩精品免费在线播放| 久久久国产精彩视频美女艺术照福利| 国产精品久久久久久久久久久久久| 青草青草久热精品视频在线观看| 久久婷婷国产麻豆91天堂| 色无极亚洲影院| 精品视频久久久| 久久偷看各类女兵18女厕嘘嘘| 日韩精品在线观看视频| 日韩av毛片网| 欧美黄色成人网|