没有合适的资源?快使用搜索试试~ 我知道了~
首页比较全的一个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,其实在很
多地方很像,不过也有一些区别,在这里就不多说了,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;//退出页眉设置










安全验证
文档复制为VIP权益,开通VIP直接复制

评论1