c#操作Word总结

制作无纺布手提袋c#操作Word总结
Word对象模型(.Net Perspective)
  五⼤对象
Application :代表Microsoft Word应⽤程序本⾝
  是Document和Selection的基类。通过Application的属性和⽅法,我们可以控制Word的⼤环境。
Document :代表⼀个Word⽂档
  当你新建⼀个Word⽂档或者打开⼀个已有的Word⽂档,你将创建⼀个Document对象,该对象被加⼊到Words Documents Collection 中。拥有焦点的Document称为ActiveDocument,可以通过Application对象的ActiveDocument属性获得当前⽂档对象
Selection :代表当前选中的区域(⾼亮),没有选中区域时代表光标点
  它通常是⾼亮显⽰的(例如,你要改变⼀段⽂字的字体,你⾸先得选中这段⽂字,那么选中的这块区域就是当前⽂档的Selection对象所包含的区域)
Bookmarks :书签
  1>书签⼀般有名字
  2>Saved with the document,且⽂档关闭了之后书签继续存在
  3>书签通常是隐藏的,但也可以通过代码设置其为可见
Range :代表⼀块区域,与Selection类似,不过⼀般不可见
  1>包含⼀个起始位置和⼀个结束位置
  2>它可以包含光标点,⼀段⽂本或者整个⽂档
  3>它包含空格,tab以及paragraph marks
  4>它可以是当前选中的区域,当然也可以不是当前选中区域
  5>它被动态创建
  6>当你在⼀个Range的末尾插⼊⽂本,这将扩展该Range
  word⽂档对象的结构图
关于对象的详细使⽤,可以参考msdn api
实例使⽤ 
创建Word ⽂档所使⽤的主要⽅法是通过微软公司提供的Microsoft Word X Object Library,
其中X 为版本号。Word2010对应14.0, Word 2007 对应12.0,Word 2003 对应11.0。
通过在项⽬中添加该组件,即可使⽤微软公司提供的⽅法创建相应版本的Word ⽂档。
在实例中我将所要⽣成word的格式设置为2003版本
新建⼀个winForm项⽬⽂件,
Com组件中添加 Microsoft Word 12.0 Object Library,引⽤⾯板中多出Microsoft.Office.Core、Microsoft.Office.Interop.Word两个引⽤。
在类⽂件中添加应⽤如下:
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
  下⾯从word创建、格式设置、⽂本添加、图⽚添加、表格添加展⽰部分代码:
void CreateWord()
{
object path;//⽂件路径
string strContent;//⽂件内容
MSWord.Application wordApp;//Word应⽤程序变量
MSWord.Document wordDoc;//Word⽂档变量
数据监控
path = "d:\\myWord.doc";//保存为Word2003⽂档
// path = "d:\\myWord.doc";//保存为Word2007⽂档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使⽤的是COM 库,因此有许多变量需要⽤Missing.Value 代替
Object Nothing = Missing.Value;
//新建⼀个word对象
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//WdSaveDocument为Word2003⽂档的保存格式(⽂档后缀.doc)\wdFormatDocumentDefault为Word2007的保存格式(⽂档后缀.docx)
object format = MSWord.WdSaveFormat.wdFormatDocument;
//将wordDoc ⽂档对象的内容保存为DOC ⽂档,并保存到path指定的路径
wordDoc.SaveAs(ref path, ref format, 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, re
f Nothing);
//关闭wordDoc⽂档
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word⽂档创建完毕!');</script>");
}
频率补偿电路
private void SetWordStyle()
{
object path;//⽂件路径
string strContent;//⽂件内容
MSWord.Application wordApp;//Word应⽤程序变量
MSWord.Document wordDoc;//Word⽂档变量
path = "d:\\myWord.doc";//保存为Word2003⽂档
// path = "d:\\myWord.doc";//保存为Word2007⽂档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//页⾯设置
wordDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
wordDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.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;//页眉位置
//设置页眉
wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;//视图样式
wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;//视图样式
wordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;//进⼊页眉设置,其中页眉边距在页⾯设置中已完成
wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//插⼊页眉图⽚(测试结果图⽚未插⼊成功)
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
string headerfile = "d:\\header.jpg";
Microsoft.Office.Interop.Word.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
shape1.Height = 20;
shape1.Width = 80;
wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("  ⽂档页眉");
//去掉页眉的横线
wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
wordApp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false;
wordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//退出页眉设置
//为当前页添加页码
Microsoft.Office.Interop.Word.PageNumbers pns =
wordApp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
pns.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
pns.HeadingLevelForChapter = 0;
pns.IncludeChapterNumber = false;
pns.RestartNumberingAtSection = false;
pns.StartingNumber = 0;
object pagenmbetal = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
object first = true;
折叠篮wordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, 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);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word⽂档格式设置完毕!');</script>");
}
效果图:
private void AddWordText()
{
object path;//⽂件路径
string strContent;//⽂件内容
MSWord.Application wordApp;//Word应⽤程序变量
MSWord.Document wordDoc;//Word⽂档变量
path = "d:\\myWord.doc";//保存为Word2003⽂档
// path = "d:\\myWord.doc";//保存为Word2007⽂档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Selection.ParagraphFormat.LineSpacing = 35f;//设置⽂档的⾏间距
//写⼊普通⽂本
wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//⾸⾏缩进的长度
strContent = "c#向Word写⼊⽂本普通⽂本:\n";
wordDoc.Paragraphs.Last.Range.Text = strContent;
//将⽂档的前三个字替换成"asdfasdf",并将其颜⾊设为蓝⾊
object start = 0;
object end = 3;
Microsoft.Office.Interop.Word.Range rang = wordDoc.Range(ref start, ref end);
rang.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrightGreen;
rang.Text = "我是替换⽂字";
wordDoc.Range(ref start, ref end);
//写⼊⿊体⽂本
object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消⾸⾏缩进的长度
strContent = "⿊体⽂本\n ";//在⽂本中使⽤'\n'换⾏
wordDoc.Paragraphs.Last.Range.Font.Name = "⿊体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
// wordApp.Selection.Text = strContent;
//写⼊加粗⽂本
strContent = "加粗⽂本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;//Bold=0为不加粗
wordDoc.Paragraphs.Last.Range.Text = strContent;
//  wordApp.Selection.Text = strContent;
//写⼊15号字体⽂本
strContent = "15号字体⽂本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写⼊斜体⽂本
strContent = "斜体⽂本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写⼊蓝⾊⽂本
strContent = "蓝⾊⽂本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
betal
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写⼊下划线⽂本
strContent = "下划线⽂本\n ";
辐射取暖器wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, 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);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script> alert('" + path + ": Word⽂档写⼊⽂本完毕!');</script>");
}
效果图:

本文发布于:2024-09-21 20:34:42,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/349168.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   对象   页眉   添加   区域
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议