代码如下所示:
代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingMicrosoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Web;
usingSystem.Data;
usingSystem.Reflection;
usingMicrosoft.Win32;
usingSystem.Text.RegularExpressions;
usingSystem.Net;
namespaceOfficeOperate
{
publicclassWordOperate
{
#region动态生成Word文档并填充数据
/**////<summary>
///动态生成Word文档并填充数据
///</summary>
///<returns>返回自定义信息</returns>
publicstaticstringCreateWordFile()
{
stringmessage=””;
try
{
ObjectoMissing=System.Reflection.Missing.Value;
stringdir=System.Web.HttpContext.Current.Server.MapPath(“”);//首先在类库添加usingSystem.web的引用
if(!Directory.Exists(dir+”//file”))
{
Directory.CreateDirectory(dir+”//file”); //创建文件所在目录
}
stringname=DateTime.Now.ToLongDateString()+”.doc”;
objectfilename=dir+”//file//”+name; //文件保存路径
//创建Word文档
Microsoft.Office.Interop.Word.ApplicationWordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.DocumentWordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
/**/////添加页眉方法一:
//WordApp.ActiveWindow.View.Type=WdViewType.wdOutlineView;
//WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekPrimaryHeader;
//WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(“无锡全真通科技有限公司”);//页眉内容
//添加页眉方法二:
if(WordApp.ActiveWindow.ActivePane.View.Type==Microsoft.Office.Interop.Word.WdViewType.wdNormalView||WordApp.ActiveWindow.ActivePane.View.Type==Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type=Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
stringsHeader=”页眉内容”;
WordApp.Selection.HeaderFooter.LinkToPrevious=false;
WordApp.Selection.HeaderFooter.Range.Text=sHeader;
WordApp.ActiveWindow.View.SeekView=Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
//WordApp.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐
WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekMainDocument;//跳出页眉设置
WordApp.Selection.ParagraphFormat.LineSpacing=15f;//设置文档的行间距
//移动焦点并换行
objectcount=14;
objectWdLine=Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
WordApp.Selection.MoveDown(refWdLine,refcount,refoMissing);//移动焦点
WordApp.Selection.TypeParagraph();//插入段落
//文档中创建表格
Microsoft.Office.Interop.Word.TablenewTable=WordDoc.Tables.Add(WordApp.Selection.Range,12,3,refoMissing,refoMissing);
//设置表格样式
newTable.Borders.OutsideLineStyle=Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle=Microsoft.Office.Interop.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=Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//填充表格内容
&nb sp; newTable.Cell(2,1).Range.Text=”产品基本信息”;
newTable.Cell(2,1).Range.Font.Color=Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(2,1).Merge(newTable.Cell(2,3));
WordApp.Selection.Cells.VerticalAlignment=Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//填充表格内容
newTable.Cell(3,1).Range.Text=”品牌名称:”;
newTable.Cell(3,2).Range.Text=”BrandName”;
//纵向合并单元格
newTable.Cell(3,3).Select();//选中一行
objectmoveUnit=Microsoft.Office.Interop.Word.WdUnits.wdLine;
objectmoveCount=5;
objectmoveExtend=Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(refmoveUnit,refmoveCount,refmoveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(“images//picture.jpg”)))
{
stringFileName=System.Web.HttpContext.Current.Server.MapPath(“images//picture.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;//图片高度
}
/ /将图片设置为四周环绕型
Microsoft.Office.Interop.Word.Shapes=WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type=Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
newTable.Cell(12,1).Range.Text=”产品特殊属性”;
newTable.Cell(12,1).Merge(newTable.Cell(12,3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(refoMissing);
WordDoc.Paragraphs.Last.Range.Text=”文档创建时间:”+DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//文件保存
WordDoc.SaveAs(reffilename,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);
WordDoc.Close(refoMissing,refoMissing,refoMissing);
WordApp.Quit(refoMissing,refoMissing,refoMissing);
message=name+”文档生成成功”;
}
catch
{
message=”文件导出异常!”;
}
returnmessage;
}
#endregion
#region创建并打开一个空的word文档进行编辑
/**////<summary>
///创建并打开一个空的word文档进行编辑
///</summary>
publicstaticvoidOpenNewWordFileToEdit()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
}< br> #endregion
#region创建word文档
/**////<summary>
///创建word文档
///</summary>
///<returns></returns>
publicstaticstringcreateWord()
{
Microsoft.Office.Interop.Word.ApplicationWordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
DocumentWordDoc;
stringstrContent=””;
objectstrFileName=System.Web.HttpContext.Current.Server.MapPath(“test.doc”);
if(System.IO.File.Exists((string)strFileName))
System.IO.File.Delete((string)strFileName);
ObjectoMissing=System.Reflection.Missing.Value;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
#region 将数据库中读取得数据写入到word文件中
strContent=”你好/n/n/r”;
WordDoc.Paragraphs.Last.Range.Text=strContent;
strContent=”这是测试程序”;
WordDoc.Paragraphs.Last.Range.Text=strContent;
#endregion
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(refstrFileName,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,ref oMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);
//关闭WordDoc文档对象
WordDoc.Close(refoMissing,refoMissing,refoMissing);
//关闭WordApp组件对象
WordApp.Quit(refoMissing,refoMissing,refoMissing);
stringmessage=strFileName+”/r/n”+”创建成功”;
returnmessage;
}
#endregion
#region把Word文档装化为Html文件
/**////<summary>
///把Word文档装化为Html文件
///</summary>
///<paramname=”strFileName”>要转换的Word文档</param>
publicstaticvoidWordToHtml(stringstrFileName)
{
stringsaveFileName=strFileN ame+DateTime.Now.ToString(“yyyy-MM-dd-HH-mm-ss”)+”.html”;
WordToHtml(strFileName,saveFileName);
}
/**////<summary>
///把Word文档装化为Html文件
///</summary>
///<paramname=”strFileName”>要转换的Word文档</param>
///<paramname=”strSaveFileName”>要生成的具体的Html页面</param>
publicstaticvoidWordToHtml(stringstrFileName,stringstrSaveFileName)
{
Microsoft.Office.Interop.Word.ApplicationClassWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
ObjectoMissing=System.Reflection.Missing.Value;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
objectfileName=strFileName;
WordDoc=WordApp.Documents.Open(reffileName,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);
TypewordType=WordApp.GetType();
//打开文件
TypedocsType=WordApp.Documents.GetType();
//转换格式,另存为
TypedocType=WordDoc.GetType();
objectsaveFileName=strSaveFileName;
docType.InvokeMember(“SaveAs”,System.Reflection.BindingFlags.InvokeMethod,null,WordDoc,newobject[]{saveFileName,Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML});
#region其它格式:
/**//**/
/**////wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
//———————————————————- —————- // docType.InvokeMember(“SaveAs”,System.Reflection.BindingFlags.InvokeMethod,
// null,WordDoc,newobject[]{saveFileName,Word.WdSaveFormat.wdFormatHTML});
//退出Word
//wordType.InvokeMember(“Quit”,System.Reflection.BindingFlags.InvokeMethod,
// null,WordApp,null);
#endregion
WordDoc.Close(refoMissing,refoMissing,refoMissing);
WordApp.Quit(refoMissing,refoMissing,refoMissing);
}
#endregion
#region导入模板
/**////<summary>
///导入模板
///</summary>
///<paramname=”filePath”>模板文档路径</param>
publicstaticvoidImportTemplate(stringfilePath)
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
objectfileName=filePath;
WordDoc=WordApp.Documents.Add(reffileName,refoMissing,refoMissing,refoMissing);
}
#endregion
#regionword中添加新表
/**////<summary>
///word中添加新表
///</summary>
publicstaticvoidAddTable()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
objectstart=0;
objectend=0;
Microsoft.Office.Interop.Word.RangetableLocation=WordDoc.Range(refsta rt,refend);
WordDoc.Tables.Add(tableLocation,3,4,refoMissing,refoMissing);//3行4列的表
}
#endregion
#region在表中插入新行
/**////<summary>
///在表中插入新的1行
///</summary>
publicstaticvoidAddRow()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
objectstart=0;
objectend=0;
Microsoft.Office.Interop.Word.RangetableLocation=WordDoc.Range(refstart,refend);
WordDoc.Tables.Add(tableLocation,3,4,refoMissing,refoMissing);
Microsoft.Office.Interop.Word.TablenewTable=WordDoc.Tables[1];
objectbeforeRow=newTable.Rows[1];
newTable.Rows.Add(refbeforeRow);
}
#endregion
#region分离单元格
/**////<summary>
///合并单元格
///</summary>
publicstaticvoidCombinationCell()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
objectstart=0;
objectend=0;
Microsoft.Office.Interop.Word.RangetableLocation=WordDoc.Range(refstart,refend);
WordDoc.Tables.Add(tableLocation,3,4,refoMissing,refoMissing);
Microsoft.Office.Interop.Word.TablenewTable=WordDoc.Tables[1];
objectbeforeRow=newTable.Rows [1];
newTable.Rows.Add(refbeforeRow);
Microsoft.Office.Interop.Word.Cellcell=newTable.Cell(2,1);//2行1列合并2行2列为一起
cell.Merge(newTable.Cell(2,2));
//cell.Merge(newTable.Cell(1,3));
}
#endregion
#region分离单元格
/**////<summary>
///分离单元格
///</summary>
publicstaticvoidSeparateCell()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
objectstart=0;
objectend=0;
Microsoft.Office.Interop.Word.RangetableLocation=WordDoc.Range(refstart,refend);
WordDoc.Tables.Add(tableLocation,3,4,refoMissing,refoMissing);
Microsoft.Office.Interop.Word.TablenewTable=WordDoc.Tables[1];
objectbeforeRow=newTable.Rows[1];
newTable.Rows.Add(refbeforeRow);
Microsoft.Office.Interop.Word.Cellcell=newTable.Cell(1,1);
cell.Merge(newTable.Cell(1,2));
objectRownum=2;
objectColumnnum=2;
cell.Split(refRownum,ref Columnnum);
}
#endregion
#region通过段落控制插入Insertaparagraphatthebeginningofthedocument.
/**////<summary>
///通过段落控制插入Insertaparagraphatthebeginningofthedocument.
///</summary>
publicstaticvoidInsert()
{
objectoMissing=System.Reflection.Missing.Value;
//objectoEndOfDoc=”//endofdoc”;/**//*/endofdocisapredefinedbookmark*/
//StartWordandcreateanewdocument.
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible=true;
WordDoc=WordApp.Documents.Add(refoMissing,refoMissing,refoMissing,refoMissing);
//Insertaparagraphatthebeginningofthedocument.
Microsoft.Office.Interop.Word.ParagraphoPara1;
oPara1=WordDoc.Content.Paragraphs.Add(refoMissing);
oPara1.Range.Text=”Heading1″;
oPara1.Range.Font.Bold=1;
oPara1.Format.SpaceAfter=24; //24ptspacingafterparagraph.
oPara1.Range.InsertParagraphAfter();
}
#endregion
#regionword文档设置及获取光标位置
/**////<summary>
///word文档设置及获取光标位置
///</summary>
publicstaticvoidWordSet()
{
objectoMissing=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.ApplicationWordApp;
Microsoft.Office.Interop.Word.DocumentWordDoc;
WordApp=newMicrosoft.Office.Interop.Word.ApplicationClass();
C语言实现颠倒栈的方法
C语言字符串原地压缩实现方法
上述就是C#学习教程:C# Word 类库的深入理解分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/904808.html