最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

比較全的一個(gè)C#操作word文檔示例

 更新時(shí)間:2015年06月05日 10:41:33   投稿:junjie  
這篇文章主要介紹了比較全的一個(gè)C#操作word文檔示例,本文來自己項(xiàng)目心得總結(jié),本文還給出了一個(gè)示例,這個(gè)示例里面包括了一些常用的圖、文、表、公式的編輯與排版以及頁面設(shè)置、頁眉、頁碼的操作,需要的朋友可以參考下

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

1、非常簡單。

2、開發(fā)及運(yùn)行環(huán)境要求。操作系統(tǒng)為:WindowsXP(安裝.net framework2.0)/Vista/Win7;在操作系統(tǒng)必須安裝Word2003完全安裝版。這里必須要強(qiáng)調(diào)是Word2003完全安裝版,因?yàn)檐浖_發(fā)及運(yùn)行都需要一個(gè)com組件:Microsoft word 11.0 Object Library。如果不是Word2003完全安裝版,可以下載這個(gè)com組件,并手動(dòng)的安裝這個(gè)com組件。下載地址為:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=20923,文件不大,只有4M左右。

3、C#工程設(shè)置。這里的工程設(shè)置,就是添加com組件。步驟為:在工程資源管理器中"添加引用"->"com"選項(xiàng)卡->在下拉列表中選Microsoft word 11.0 Object Library。ok了,看上去,跟添加一般的dll一樣,但實(shí)際上vs2008在這個(gè)過程中完成一系列復(fù)雜的關(guān)于.net調(diào)用com組件的操作,不過,幸好我們不用管這個(gè)。

4、接下就是寫代碼了。在這里,使用Word的com對像,跟使用一般的非com對像一樣,非常流暢,好像根本就不管它什么com不com的。為了使代碼比較簡潔,可以在源代碼文件頂添加這樣的一條語句:using Word = Microsoft.Office.Interop.Word;

5、最好是對word對像模型有一定的了解,這樣在寫代碼的時(shí)候就不會(huì)那么“迷?!绷?。wore對像模型中有幾個(gè)比較重要的對像,它們是Application、Document、Selection、Range、Bookmark,以及其它的一些對像,如:Paragraph、Section、Table等級。剛開始學(xué)的時(shí)候,感覺Selection、Range、Bookmark這幾個(gè)對像有點(diǎn)迷惑人,Selection可能好理解,就是表示當(dāng)前的選擇區(qū)域,如果沒有選擇就表示光標(biāo)所在位置。Range和Bookmark,其實(shí)在很多地方很像,不過也有一些區(qū)別,在這里就不多說了,google一下"word.Range"就行了。

6、在寫代碼的過程中,經(jīng)常會(huì)想要實(shí)現(xiàn)的一些操作,但是由于對word對像不熟悉而不知怎么用代碼實(shí)現(xiàn)。比如設(shè)置頁眉、添加頁碼什么的,如果在Word程序里手動(dòng)的操作當(dāng)然很簡單,但是要用代碼來實(shí)現(xiàn),對初學(xué)者來說就可能不那么容易了。遇到這種情況,一般有兩種方法可以選擇:一種是"百度/google法",別一種,也是我所推薦的一種就是,利用Word的“錄制宏”功能把想要實(shí)現(xiàn)的操作錄成宏之后,再看宏里的代碼,宏里的代碼其實(shí)幾乎就是你想要的代碼了(只不過語法有一點(diǎn)不一樣而已)。

7、以下給出一個(gè)示例,這個(gè)示例里面包括了一些常用的圖、文、表、公式的編輯與排版以及頁面設(shè)置、頁眉、頁碼的操作,里面都有注釋,寫得很清楚。

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;//用于作為函數(shù)的默認(rèn)參數(shù)
      newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一個(gè)word文檔
      newapp.Visible = true ;//是否顯示word程序界面

      //頁面設(shè)置
      //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;//頁眉位置

      //設(shè)置頁眉
      newapp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;//視圖樣式。
      newapp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;//進(jìn)入頁眉設(shè)置,其中頁眉邊距在頁面設(shè)置中已完成
      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;//退出頁眉設(shè)置

      //添加頁碼
      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);

      //文字設(shè)置(Selection表示當(dāng)前選擇集,如果當(dāng)前沒有選擇對像,則指對光標(biāo)所在處進(jìn)行設(shè)置)
      newapp.Selection.Font.Size = 14;
      newapp.Selection.Font.Bold = 0;
      newapp.Selection.Font.Color = Word.WdColor.wdColorBlack;
      newapp.Selection.Font.Name = "宋體";

      //段落設(shè)置
      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);

      //將文檔的前三個(gè)字替換成"asdfasdf",并將其顏色設(shè)為藍(lán)色
      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 = "這是一個(gè)";
      rang1.InsertAfter("書");
      //rang1.Select();
      object codirection = Word.WdCollapseDirection.wdCollapseStart;
      rang1.Collapse(ref codirection);//將rang1的起點(diǎn)和終點(diǎn)都定于起點(diǎn)或終點(diǎn)

      //對前三個(gè)字符進(jìn)行加粗
      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);//將光標(biāo)移至文末
      newapp.Selection.Font.Size = 10;
      newapp.Selection.TypeText("...............................(式1)\n");


      //插入圖片
      newapp.Selection.EndKey(ref unite, ref nothing);//將光標(biāo)移至文末
      //newapp.Selection.HomeKey(ref unite, ref nothing);//將光標(biāo)移至文開頭
      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);//將光標(biāo)移至文末
      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 電子產(chǎn)品\n");

      //插入表格
      Word.Table table1 = newdoc.Tables.Add(newapp.Selection.Range, 4, 3, ref nothing, ref nothing);
      newdoc.Tables[1].Cell(1, 1).Range.Text = "產(chǎn)品\n項(xiàng)目";
      newdoc.Tables[1].Cell(1, 2).Range.Text = "電腦";
      newdoc.Tables[1].Cell(1, 3).Range.Text = "手機(jī)";
      newdoc.Tables[1].Cell(2, 1).Range.Text = "重量(kg)";
      newdoc.Tables[1].Cell(3, 1).Range.Text = "價(jià)格(元)";
      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;//整個(gè)表格居中
      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);
      
      //關(guān)閉文檔
      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);
    }
   
  }

}

相關(guān)文章

  • C# ToolStrip制作四邊??扛?dòng)工具欄

    C# ToolStrip制作四邊??扛?dòng)工具欄

    這篇文章主要介紹了C# ToolStrip浮動(dòng)工具欄的制作,可以上/下/左/右停靠,代碼在下面
    2013-12-12
  • 一文搞懂c# await,async執(zhí)行流

    一文搞懂c# await,async執(zhí)行流

    這篇文章主要介紹了c# await,async執(zhí)行流的相關(guān)知識(shí),文章講解的非常細(xì)致,代碼幫助理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#中神器類BlockingCollection的實(shí)現(xiàn)詳解

    C#中神器類BlockingCollection的實(shí)現(xiàn)詳解

    如果你想玩轉(zhuǎn)C#?里面多線程,工廠模式,生產(chǎn)者/消費(fèi)者,隊(duì)列等高級操作,就可以和我一起探索這個(gè)強(qiáng)大的線程安全提供阻塞和限制功能的C#神器類BlockingCollection吧
    2023-02-02
  • C#使用融合通信API發(fā)送手機(jī)短信息

    C#使用融合通信API發(fā)送手機(jī)短信息

    融合云通信服務(wù)平臺(tái),為企業(yè)提供全方位通信服務(wù),發(fā)送手機(jī)短信是其一項(xiàng)核心功能,本文將講述如何使用融合云服務(wù)API為終端手機(jī)用戶發(fā)送短信信息,并使用?C#?進(jìn)行實(shí)現(xiàn),感興趣的可以了解下
    2024-11-11
  • 詳解C#操作XML的方法總結(jié)

    詳解C#操作XML的方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了C#對XML文件進(jìn)行一些基本操作的方法,譬如:生成xml文件、修改xml文件的節(jié)點(diǎn)信息等,需要的可以參考一下
    2022-11-11
  • C# 引入委托的目的是什么

    C# 引入委托的目的是什么

    這篇文章主要介紹了C# 引入委托的目的是什么,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • c# 實(shí)現(xiàn)漢諾塔游戲

    c# 實(shí)現(xiàn)漢諾塔游戲

    這篇文章主要介紹了c# 實(shí)現(xiàn)漢諾塔游戲的示例,幫助大家更好的理解和使用c# 編程語言,感興趣的朋友可以了解下
    2020-11-11
  • C#中DataTable刪除行的方法分析

    C#中DataTable刪除行的方法分析

    這篇文章主要介紹了C#中DataTable刪除行的方法,包括了常見的幾種刪除方法的分析,需要的朋友可以參考下
    2014-09-09
  • C#實(shí)現(xiàn)滑動(dòng)開關(guān)效果

    C#實(shí)現(xiàn)滑動(dòng)開關(guān)效果

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)滑動(dòng)開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C#?WPF編程之元素綁定詳解

    C#?WPF編程之元素綁定詳解

    數(shù)據(jù)綁定是一種關(guān)系,該關(guān)系告訴WPF從源對象提取一下信息,并用這些信息設(shè)置目標(biāo)對象的屬性,下面我們就來了解一下WPF編程中元素綁定的相關(guān)知識(shí)吧
    2024-04-04

最新評論

库尔勒市| 习水县| 商水县| 青河县| 霸州市| 筠连县| 自贡市| 正蓝旗| 益阳市| 观塘区| 宜章县| 类乌齐县| 修武县| 新河县| 郑州市| 刚察县| 炎陵县| 施秉县| 金平| 永清县| 安义县| 平果县| 峨眉山市| 柞水县| 兴化市| 山东省| 昂仁县| 馆陶县| 唐海县| 涞水县| 墨江| 太原市| 马公市| 民勤县| 长白| 舟曲县| 隆尧县| 贡觉县| 城口县| 湛江市| 寿光市|