C#操作WORD文档.docx

上传人:b****7 文档编号:10777496 上传时间:2023-02-22 格式:DOCX 页数:48 大小:27.02KB
下载 相关 举报
C#操作WORD文档.docx_第1页
第1页 / 共48页
C#操作WORD文档.docx_第2页
第2页 / 共48页
C#操作WORD文档.docx_第3页
第3页 / 共48页
C#操作WORD文档.docx_第4页
第4页 / 共48页
C#操作WORD文档.docx_第5页
第5页 / 共48页
点击查看更多>>
下载资源
资源描述

C#操作WORD文档.docx

《C#操作WORD文档.docx》由会员分享,可在线阅读,更多相关《C#操作WORD文档.docx(48页珍藏版)》请在冰豆网上搜索。

C#操作WORD文档.docx

C#操作WORD文档

1.c#操作word在指订书签拔进文字或许图片

usingWord=Microsoft.Office.Interop.Word;

objectNothing=System.Reflection.Missing.Value;

objectformat=Word.WdSaveFormat.wdFormatDocument;

Word.ApplicationwordApp=newWord.ApplicationClass();

//打开网页挑选内容

objectsrcFileName=@"c:

\new1.doc";//里里有图片

Word.DocumentwordDoc2=wordApp.Documents.Open(refsrcFileName,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);

try

{

objectbookmarkName="jlr";

//Word.Rangerng=wordDoc2.Bookmarks.get_Item(refbookmarkName).Range;

//rng.Text="newText";

//objectrange=rng;

//wordDoc2.Bookmarks.Add("jlr",refrange);

wordDoc2.Bookmarks.get_Item(refbookmarkName).Select();

wordApp.Selection.InlineShapes.AddPicture("c:

\\1.jpg",refNothing,refNothing,refNothing);

wordDoc2.Save();

}

catch{}

finally

{

//封闭网页wordDoc2

wordDoc2.Close(refNothing,refNothing,refNothing);

if(wordDoc2!

=null)

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc2);

wordDoc2=null;

}

//闭闭wordApp

wordApp.Quit(refNothing,400电话,refNothing,refNothing);

if(wordApp!

=null)

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);

wordApp=null;

}

}

GC.Collect();

2.C#替代Word模版中的本签内容的例子

//open

objectomissing=system.reflection.missing.value;

word.applicationclasswordapp=newmicrosoft.office.interop.word.applicationclass();

objectreadonly=false;

objecttemplate=templatepath;

word._documentdoc=wordapp.documents.open(reftemplate,refomissing,refreadonly,

refomissing,refomissing,refomissing,refomissing,refomissing,refomissing,

refomissing,refomissing,refomissing,refomissing,refomissing,refomissing,refomissing);

//modify

for(inti=1;i<=doc.bookmarks.count;i++)

{

objectj=i;

word.rangewordrng=doc.bookmarks.get_item(refj).range;

wordrng.text="这是第"+i+"个本签,称号为"+doc.bookmarks.get_item(refj).name;

}

//save

objectsavefilename=mappath(request.applicationpath+"/document")+"/"+guid.newguid().tostring()+".doc";

doc.saveas(refsavefilename,refomissing,refomissing,refomissing,refomissing,BB霜,refomissing,refomissing,refomissing,refomissing,

refomissing,refomissing,refomissing,refomissing,refomissing,refomissing,refomissing);

doc.close(refomissing,refomissing,refomissing);

wordapp.quit(refomissing,refomissing,refomissing);

3.用C#完成正在Word文档中搜寻文原

objectfilename="";//要打开的文档道径

stringstrKey="";//要搜寻的文原

objectMissingValue=Type.Missing;

Word.Applicationwp=newWord.ApplicationClass();

Word.Documentwd=wp.Documents.Open(reffilename,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue);

inti=0,iCount=0;

Word.Findwfnd;

if(wd.Paragraphs!

=null&&wd.Paragraphs.Count>0)

{

iCount=wd.Paragraphs.Count;

for(i=1;i<=iCount;i++)

{

wfnd=wd.Paragraphs.Range.Find;

wfnd.ClearFormatting();

wfnd.Text=strKey;

if(wfnd.Execute(refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue,refMissingValue,

refMissingValue))

{

MessageBox.Show("文档中包括指定的要害字!

","搜寻成果",MessageBoxButtons.OK);

break;

}

}

}

4.C#.net操作Word文档:

以Office2007为例

首先引进类库,Microsoft.Office.Interop.Word,然后入行编程。

代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingMicrosoft.Office.Interop.Word;

namespaceWordTest

{

publicpartialclassForm1:

Form

{

objectstrFileName;

ObjectNothing;

Microsoft.Office.Interop.Word.ApplicationmyWordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();

DocumentmyWordDoc;

stringstrContent="";

publicForm1()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

createWord();

//openWord();

}

privatevoidcreateWord()

{

strFileName=System.Windows.Forms.Application.StartupPath+"test.doc";

if(System.IO.File.Exists((string)strFileName))

System.IO.File.Delete((string)strFileName);

ObjectNothing=System.Reflection.Missing.Value;

myWordDoc=myWordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);

#region将数据库中读获得数据写进到word文件中

strContent="您佳\n\n\r";

myWordDoc.Paragraphs.Last.Range.Text=strContent;

strContent="这是测试程序";

myWordDoc.Paragraphs.Last.Range.Text=strContent;

#endregion

//将WordDoc文档对象的内容保管为DOC文档

myWordDoc.SaveAs(refstrFileName,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);

//封闭WordDoc文档对象

myWordDoc.Close(refNothing,refNothing,refNothing);

//封闭WordApp组件对于象

myWordApp.Quit(refNothing,refNothing,refNothing);

this.richTextBox1.Text=strFileName+"\r\n"+"创立胜利";

}

privatevoidopenWord()

{

fontDialog1.ShowDialog();

System.Drawing.Fontfont=fontDialog1.Font;

objectfilepath="D:

\\asp.docx";

objectoMissing=System.Reflection.Missing.Value;

myWordDoc=myWordApp.Documents.Open(reffilepath,refoMissing,refoMissing,refoMissing,refoMissing,

refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,

refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);

myWordDoc.Content.Font.Size=font.Size;

myWordDoc.Content.Font.Name=font.Name;

myWordDoc.Save();

richTextBox1.Text=myWordDoc.Content.Text;

myWordDoc.Close(refoMissing,refoMissing,refoMissing);

myWordApp.Quit(refoMissing,refoMissing,refoMissing);

}

}

5.C#动态天生Word文档并挖充数据

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.IO;

usingWord;

namespaceCreateWordFile

{

classProgram

{

staticvoidMain(string[]args)

{

CreateWordFile("");

}

//下里的例子中包含C#对Word文档的创修、拔出表格、设置样式等操做:

//(例子中代码有些触及数据疑作部门被省详,主要是引见一些C#操作word文档的方式)

publicstaticstringCreateWordFile(stringCheckedInfo)

{

stringmessage="";

try

{

ObjectNothing=System.Reflection.Missing.Value;

Directory.CreateDirectory("C:

/CNSI");//创修文件所在纲录

stringname="CNSI_"+"53asdf"+".doc";

objectfilename="C:

//CNSI//"+name;//文件保存路径

//创立Word文档

Word.ApplicationWordApp=newWord.ApplicationClass();

Word.DocumentWordDoc=WordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);

//添加页眉

WordApp.ActiveWindow.View.Type=WdViewType.wdOutlineView;

WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekPrimaryHeader;

WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");

WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对全

WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekMainDocument;//跳出页眉设放

WordApp.Selection.ParagraphFormat.LineSpacing=15f;//设放文档的行间距

//挪动焦面并换行

objectcount=14;

objectWdLine=Word.WdUnits.wdLine;//换一行;

WordApp.Selection.MoveDown(refWdLine,refcount,refNothing);//挪动焦面

WordApp.Selection.TypeParagraph();//拔入段降

//文档中创修表格

Word.TablenewTable=WordDoc.Tables.Add(WordApp.Selection.Range,12,3,refNothing,refNothing);

//设置表格样式

newTable.Borders.OutsideLineStyle=Word.WdLineStyle.wdLineStyleThickThinLargeGap;

newTable.Borders.InsideLineStyle=Word.WdLineStyle.wdLineStyleSingle;

newTable.Columns[1].Width=100f;

newTable.Columns[2].Width=220f;

newTable.Columns[3].Width=105f;

//填充表格内容

newTable.Cell(1,1).Range.Text="产品具体疑作表";

newTable.Cell(1,1).Range.Bold=2;//设放双元格中字体为粗体

//合并单元格

newTable.Cell(1,1).Merge(newTable.Cell(1,3));

WordApp.Selection.Cells.VerticalAlignment=Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;//程度居中

//挖充表格内容

newTable.Cell(2,1).Range.Text="产品基础疑作";

newTable.Cell(2,1).Range.Font.Color=Word.WdColor.wdColorDarkBlue;//设置双元格内字体色彩

//合并单元格

newTable.Cell(2,1).Merge(newTable.Cell(2,3));

WordApp.Selection.Cells.VerticalAlignment=Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//挖充表格内容

newTable.Cell(3,1).Range.Text="品牌称号:

";

newTable.Cell(3,2).Range.Text="BrandName";

//纵背合并双元格

newTable.Cell(3,3).Select();//选中一行

objectmoveUnit=Word.WdUnits.wdLine;

objectmoveCount=5;

objectmoveExtend=Word.WdMovementType.wdExtend;

WordApp.Selection.MoveDown(refmoveUnit,refmoveCount,refmoveExtend);

WordApp.Selection.Cells.Merge();

//拔出图片

stringFileName="c:

\\Winter.jpg";//图片所在道径

objectLinkToFile=false;

objectSaveWithDocument=true;

objectAnchor=WordDoc.Application.Selection.Range;

WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName,refLinkToFile,refSaveWithDocument,refAnchor);

WordDoc.Application.ActiveDocument.InlineShapes[1].Width=100f;//图片阔度

WordDoc.Application.ActiveDocument.InlineShapes[1].Height=100f;//图片高度

//将图片设置为周围围绕型

Word.Shapes=WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();

s.WrapFormat.Type=Word.WdWrapType.wdWrapSquare;

newTable.Cell(12,1).Range.Text="产品特别属性";

newTable.Cell(12,1).Merge(newTable.Cell(12,3));

//在表格中增添行

WordDoc.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 日语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1