C#操作WORD文档文档格式.docx
- 文档编号:6758097
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:48
- 大小:27.02KB
C#操作WORD文档文档格式.docx
《C#操作WORD文档文档格式.docx》由会员分享,可在线阅读,更多相关《C#操作WORD文档文档格式.docx(48页珍藏版)》请在冰点文库上搜索。
,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!
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);
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))
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;
这是测试程序"
#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.IO;
usingWord;
namespaceCreateWordFile
classProgram
staticvoidMain(string[]args)
CreateWordFile("
);
//下里的例子中包含C#对Word文档的创修、拔出表格、设置样式等操做:
//(例子中代码有些触及数据疑作部门被省详,主要是引见一些C#操作word文档的方式)
publicstaticstringCreateWordFile(stringCheckedInfo)
stringmessage="
ObjectNothing=System.Reflection.Missing.Value;
Directory.CreateDirectory("
C:
/CNSI"
//创修文件所在纲录
stringname="
CNSI_"
53asdf"
objectfilename="
//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));
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="
\\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.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 操作 WORD 文档