C#编写的Word操作类有换页添加表格文本功能.docx

上传人:b****2 文档编号:2743969 上传时间:2023-05-04 格式:DOCX 页数:14 大小:279.23KB
下载 相关 举报
C#编写的Word操作类有换页添加表格文本功能.docx_第1页
第1页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第2页
第2页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第3页
第3页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第4页
第4页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第5页
第5页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第6页
第6页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第7页
第7页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第8页
第8页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第9页
第9页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第10页
第10页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第11页
第11页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第12页
第12页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第13页
第13页 / 共14页
C#编写的Word操作类有换页添加表格文本功能.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C#编写的Word操作类有换页添加表格文本功能.docx

《C#编写的Word操作类有换页添加表格文本功能.docx》由会员分享,可在线阅读,更多相关《C#编写的Word操作类有换页添加表格文本功能.docx(14页珍藏版)》请在冰点文库上搜索。

C#编写的Word操作类有换页添加表格文本功能.docx

C#编写的Word操作类有换页添加表格文本功能

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Data;

usingSystem.Drawing;

usingWord=Microsoft.Office.Interop.Word;

usingMicrosoft.Office.Interop.Word;

usingSystem.Windows.Forms;

/************************************************************************************************************************************

**文件名:

**声明:

**创建者:

黄聪

**创建日期:

2009.10.8

**修改者:

黄聪

**最新修改日期:

2009.10.8

************************************************************************************************************************************/

namespaceTool

{

/********************************************************************************************************************************

**类名:

WordPlayer

**声明:

**创建者:

黄聪

**创建日期:

2009.7.15

**修改者:

黄聪

**最新修改日期:

2009.7.15

********************************************************************************************************************************/

publicclassWordPlayer

{

#region-属性-

privatestaticMicrosoft.Office.Interop.Word._ApplicationoWord=null;

privatestaticMicrosoft.Office.Interop.Word._Documentodoc=null;

privatestaticMicrosoft.Office.Interop.Word._DocumentoDoc

{

get

{

if(odoc==null)

{

odoc=oWord.Documents.Add(refNothing,refNothing,refNothing,refNothing);

}

returnodoc;

}

set

{

if(value!

=null)

{

odoc=value;

}

}

}

privatestaticobjectNothing=System.Reflection.Missing.Value;

publicenumOrientation

{

横板,

竖板

}

publicenumAlignment

{

左对齐,

居中,

右对齐

}

#endregion

#region-添加文档-

#region-创建并打开一个空的word文档进行编辑-

publicstaticvoidOpenNewWordFileToEdit()

{

oDoc=oWord.Documents.Add(refNothing,refNothing,refNothing,refNothing);

}

#endregion

#endregion

#region-创建新Word-

publicstaticboolCreateWord(boolisVisible)

{

try

{

oWord=newMicrosoft.Office.Interop.Word.Application();

oWord.Visible=isVisible;

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

publicstaticboolCreateWord()

{

returnCreateWord(false);

}

#endregion

#region-打开文档-

publicstaticboolOpen(stringfilePath,boolisVisible)

{

try

{

oWord.Visible=isVisible;

objectpath=filePath;

oDoc=oWord.Documents.Open(refpath,

refNothing,refNothing,refNothing,refNothing,refNothing,

refNothing,refNothing,refNothing,refNothing,refNothing,

refNothing,refNothing,refNothing,refNothing,refNothing);

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

#endregion

#region-插入表格-

publicstaticboolInsertTable(DataTabledt,boolhaveBorder,double[]colWidths)

{

try

{

objectNothing=System.Reflection.Missing.Value;

intlenght=oDoc.Characters.Count-1;

objectstart=lenght;

objectend=lenght;

//表格起始坐标

Microsoft.Office.Interop.Word.RangetableLocation=oDoc.Range(refstart,refend);

//添加Word表格

Microsoft.Office.Interop.Word.Tabletable=oDoc.Tables.Add(tableLocation,dt.Rows.Count,dt.Columns.Count,refNothing,refNothing);

if(colWidths!

=null)

{

for(inti=0;i

{

table.Columns[i+1].Width=(float)(28.5F*colWidths[i]);

}

}

///设置TABLE的样式

table.Rows.HeightRule=Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;

table.Rows.Height=oWord.CentimetersToPoints(float.Parse("0.8"));

table.Range.Font.Size=10.5F;

table.Range.Font.Name="宋体";

table.Range.Font.Bold=0;

table.Range.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

table.Range.Cells.VerticalAlignment=Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

if(haveBorder==true)

{

//设置外框样式

table.Borders.OutsideLineStyle=Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

table.Borders.InsideLineStyle=Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

//样式设置结束

}

for(introw=0;row

{

for(intcol=0;col

{

table.Cell(row+1,col+1).Range.Text=dt.Rows[row][col].ToString();

}

}

returntrue;

}

catch(Exceptione)

{

MessageBox.Show(e.ToString(),"错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error);

returnfalse;

}

finally

{

}

}

publicstaticboolInsertTable(DataTabledt,boolhaveBorder)

{

returnInsertTable(dt,haveBorder,null);

}

publicstaticboolInsertTable(DataTabledt)

{

returnInsertTable(dt,false,null);

}

#endregion

#region-插入文本-

publicstaticboolInsertText(stringstrText,System.Drawing.Fontfont,Alignmentalignment,boolisAftre)

{

try

{

Word.Rangerng=oDoc.Content;

intlenght=oDoc.Characters.Count-1;

objectstart=lenght;

objectend=lenght;

rng=oDoc.Range(refstart,refend);

if(isAftre==true)

{

strText+="\r\n";

}

rng.Text=strText;

rng.Font.Name=font.Name;

rng.Font.Size=font.Size;

if(font.Style==FontStyle.Bold){rng.Font.Bold=1;}//设置单元格中字体为粗体

SetAlignment(rng,alignment);

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

publicstaticboolInsertText(stringstrText)

{

returnInsertText(strText,newSystem.Drawing.Font("宋体",10.5F,FontStyle.Bold),Alignment.左对齐,false);

}

#endregion

#region-设置对齐方式-

privatestaticMicrosoft.Office.Interop.Word.WdParagraphAlignmentSetAlignment(Rangerng,Alignmentalignment)

{

rng.ParagraphFormat.Alignment=SetAlignment(alignment);

returnSetAlignment(alignment);

}

privatestaticMicrosoft.Office.Interop.Word.WdParagraphAlignmentSetAlignment(Alignmentalignment)

{

if(alignment==Alignment.居中)

{

returnWord.WdParagraphAlignment.wdAlignParagraphCenter;

}

elseif(alignment==Alignment.左对齐)

{

returnWord.WdParagraphAlignment.wdAlignParagraphLeft;

}

else

{returnWord.WdParagraphAlignment.wdAlignParagraphRight;}

}

#endregion

#region-页面设置-

publicstaticvoidSetPage(Orientationorientation,doublewidth,doubleheight,doubletopMargin,doubleleftMargin,doublerightMargin,doublebottomMargin)

{

oDoc.PageSetup.PageWidth=oWord.CentimetersToPoints((float)width);

oDoc.PageSetup.PageHeight=oWord.CentimetersToPoints((float)height);

if(orientation==Orientation.横板)

{

oDoc.PageSetup.Orientation=Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;

}

oDoc.PageSetup.TopMargin=(float)(topMargin*25);//上边距

oDoc.PageSetup.LeftMargin=(float)(leftMargin*25);//左边距

oDoc.PageSetup.RightMargin=(float)(rightMargin*25);//右边距

oDoc.PageSetup.BottomMargin=(float)(bottomMargin*25);//下边距

}

publicstaticvoidSetPage(Orientationorientation,doubletopMargin,doubleleftMargin,doublerightMargin,doublebottomMargin)

{

SetPage(orientation,21,29.7,topMargin,leftMargin,rightMargin,bottomMargin);

}

publicstaticvoidSetPage(doubletopMargin,doubleleftMargin,doublerightMargin,doublebottomMargin)

{

SetPage(Orientation.竖板,21,29.7,topMargin,leftMargin,rightMargin,bottomMargin);

}

#endregion

#region-插入分页符-

publicstaticvoidInsertBreak()

{

Word.Paragraphpara;

para=oDoc.Content.Paragraphs.Add(refNothing);

objectpBreak=(int)WdBreakType.wdSectionBreakNextPage;

para.Range.InsertBreak(refpBreak);

}

#endregion

#region-关闭当前文档-

publicstaticboolCloseDocument()

{

try

{

objectdoNotSaveChanges=Word.WdSaveOptions.wdDoNotSaveChanges;

oDoc.Close(refdoNotSaveChanges,refNothing,refNothing);

oDoc=null;

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

#endregion

#region-关闭程序-

publicstaticboolQuit()

{

try

{

objectsaveOption=Word.WdSaveOptions.wdDoNotSaveChanges;

oWord.Quit(refsaveOption,refNothing,refNothing);

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

#endregion

#region-保存文档-

publicstaticboolSave(stringsavePath)

{

returnSave(savePath,false);

}

publicstaticboolSave(stringsavePath,boolisClose)

{

try

{

objectfileName=savePath;

oDoc.SaveAs(reffileName,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);

if(isClose)

{

returnCloseDocument();

}

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

#endregion

#region-插入页脚-

publicstaticboolInsertPageFooter(stringtext,System.Drawing.Fontfont,WordPlayer.Alignmentalignment)

{

try

{

oWord.ActiveWindow.View.SeekView=Word.WdSeekView.wdSeekCurrentPageFooter;//页脚

oWord.Selection.InsertAfter(text);

GetWordFont(oWord.Selection.Font,font);

SetAlignment(oWord.Selection.Range,alignment);

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

publicstaticboolInsertPageFooterNumber(System.Drawing.Fontfont,WordPlayer.Alignmentalignment)

{

try

{

oWord.ActiveWindow.View.SeekView=WdSeekView.wdSeekCurrentPageHeader;

oWord.Selection.WholeStory();

oWord.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle=WdLineStyle.wdLineStyleNone;

oWord.ActiveWindow.View.SeekView=Word.WdSeekView.wdSeekMainDocument;

oWord.ActiveWindow.View.SeekView=Word.WdSeekView.wdSeekCurrentPageFooter;//页脚

oWord.Selection.TypeText("第");

objectpage=WdFieldType.wdFieldPage;

oWord.Selection.Fields.Add(oWord.Selection.Range,refpage,refNothing,refNothing);

oWord.Selection.TypeText("页/共");

objectpages=WdFieldType.wdFieldNumPages;

oWord.Selection.Fields.Add(oWord.Selection.Range,refpages,refNothing,refNothing);

oWord.Selection.TypeText("页");

GetWordFont(oWord.Selection.Font,font);

SetAlignment(oWord.Selection.Range,alignment);

oWord.ActiveWindow.View.SeekView=Word.WdSeekView.wdSeekMainDocument;

returntrue;

}

catch(Exception)

{

returnfalse;

}

}

#endregion

#region-字体格式设定-

publicstaticvoidGetWordFont(Microsoft.Office.Interop.Word.FontwordFont,System.Drawing.Font

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

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2