using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace MYNAMESPACE //这边需要换成自己的命名空间名
{
classReport
{
private_ApplicationwordApp= null;
private_DocumentwordDoc= null;
public_ApplicationApplication
{
get
{
return wordApp;
}
set
{
wordApp = value;
}
}
public_DocumentDocument
{
get
{
return wordDoc;
}
set
{
wordDoc = value;
}
}
//通过模板创建新文档
publicvoidCreateNewDocument(stringfilePath)
{
killWinWordProcess();
wordApp= new ApplicationClass();
wordApp.DisplayAlerts =WdAlertLevel.wdAlertsNone;
wordApp.Visible =false;
objectmissing =System.Reflection.Missing.Value;
objecttemplateName =filePath;
wordDoc= wordApp.Documents.Open(reftemplateName, refmissing,
ref missing, ref missing,ref missing, ref missing, refmissing,
ref missing, ref missing,ref missing, ref missing, refmissing,
ref missing, ref missing,ref missing, ref missing);
}
//保存新文件
publicvoidSaveDocument(stringfilePath)
{
objectfileName =filePath;
objectformat =WdSaveFormat.wdFormatDocument;//保存格式
objectmiss =System.Reflection.Missing.Value;
wordDoc.SaveAs(reffileName, ref format, ref miss,
ref miss, ref miss,ref miss, ref miss,
ref miss, ref miss,ref miss, ref miss,
ref miss, ref miss,ref miss, ref miss,
ref miss);
//关闭wordDoc,wordApp对象
objectSaveChanges =WdSaveOptions.wdSaveChanges;
objectOriginalFormat =WdOriginalFormat.wdOriginalDocumentFormat;
objectRouteDocument =false;
wordDoc.Close(refSaveChanges, refOriginalFormat, refRouteDocument);
wordApp.Quit(refSaveChanges, refOriginalFormat, refRouteDocument);
}
//在书签处插入值
publicboolInsertValue(stringbookmark, stringvalue)
{
objectbkObj =bookmark;
if(wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
{
wordApp.ActiveDocument.Bookmarks.get_Item(refbkObj).Select();
wordApp.Selection.TypeText(value);
return true;
}
returnfalse;
}
//插入表格,bookmark书签
publicTableInsertTable(stringbookmark, int rows, int columns,float width)
{
objectmiss =System.Reflection.Missing.Value;
objectoStart =bookmark;
Rangerange =wordDoc.Bookmarks.get_Item(refoStart).Range;//表格插入位置
TablenewTable =wordDoc.Tables.Add(range,rows, columns, ref miss, refmiss);
//设置表的格式
newTable.Borders.Enable =1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过)
newTable.Borders.OutsideLineWidth=WdLineWidth.wdLineWidth050pt;//边框宽度
if(width != 0)
{
newTable.PreferredWidth=width;//表格宽度
}
newTable.AllowPageBreaks =false;
returnnewTable;
}
//合并单元格 表名,开始行号,开始列号,结束行号,结束列号
publicvoidMergeCell(Microsoft.Office.Interop.Word.Tabletable, int row1, int column1,int row2, int column2)
{
table.Cell(row1,column1).Merge(table.Cell(row2,column2));
}
//设置表格内容对齐方式Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1)
publicvoidSetParagraph_Table(Microsoft.Office.Interop.Word.Tabletable, int Align, int Vertical)
{
switch(Align)
{
case -1:table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;break;//左对齐
case 0: table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphCenter;break;//水平居中
case 1: table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;break;//右对齐
}
switch(Vertical)
{
case -1: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;break;//顶端对齐
case 0: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;break;//垂直居中
case 1: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalBottom;break;//底端对齐
}
}
//设置表格字体
publicvoidSetFont_Table(Microsoft.Office.Interop.Word.Tabletable, string fontName, double size)
{
if(size != 0)
{
table.Range.Font.Size =Convert.ToSingle(size);
}
if(fontName !="")
{
table.Range.Font.Name =fontName;
}
}
//是否使用边框,n表格的序号,use是或否
publicvoidUseBorder(int n,bool use)
{
if(use)
{
wordDoc.Content.Tables[n].Borders.Enable =1; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
}
else
{
wordDoc.Content.Tables[n].Borders.Enable =2; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
}
}
//给表格插入一行,n表格的序号从1开始记
publicvoidAddRow(int n)
{
objectmiss =System.Reflection.Missing.Value;
wordDoc.Content.Tables[n].Rows.Add(refmiss);
}
//给表格添加一行
publicvoidAddRow(Microsoft.Office.Interop.Word.Tabletable)
{
objectmiss =System.Reflection.Missing.Value;
table.Rows.Add(refmiss);
}
//给表格插入rows行,n为表格的序号
publicvoidAddRow(int n, int rows)
{
objectmiss =System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Tabletable = wordDoc.Content.Tables[n];
for(inti = 0; i < rows; i++)
{
table.Rows.Add(refmiss);
}
}
//给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
publicvoidInsertCell(Microsoft.Office.Interop.Word.Tabletable, int row, int column,string value)
{
table.Cell(row,column).Range.Text =value;
}
//给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
publicvoidInsertCell(int n, int row,int column, string value)
{
wordDoc.Content.Tables[n].Cell(row,column).Range.Text =value;
}
//给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值
publicvoidInsertCell(int n, int row,int columns, string[] values)
{
Microsoft.Office.Interop.Word.Tabletable = wordDoc.Content.Tables[n];
for(inti = 0; i < columns; i++)
{
table.Cell(row,i + 1).Range.Text =values[i];
}
}
//插入图片
publicvoidInsertPicture(stringbookmark, stringpicturePath, floatwidth, float hight)
{
object miss = System.Reflection.Missing.Value;
objectoStart =bookmark;
ObjectlinkToFile =false; //图片是否为外部链接
ObjectsaveWithDocument =true; //图片是否随文档一起保存
objectrange =wordDoc.Bookmarks.get_Item(refoStart).Range;//图片插入位置
wordDoc.InlineShapes.AddPicture(picturePath,ref linkToFile, ref saveWithDocument, refrange);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width=width; //设置图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height=hight; //设置图片高度
}
//插入一段文字,text为文字内容
publicvoidInsertText(stringbookmark, stringtext)
{
objectoStart =bookmark;
objectrange =wordDoc.Bookmarks.get_Item(refoStart).Range;
Paragraphwp =wordDoc.Content.Paragraphs.Add(refrange);
wp.Format.SpaceBefore= 6;
wp.Range.Text =text;
wp.Format.SpaceAfter =24;
wp.Range.InsertParagraphAfter();
wordDoc.Paragraphs.Last.Range.Text ="\n";
}
//杀掉winword.exe进程
publicvoidkillWinWordProcess()
{
System.Diagnostics.Process[]processes=System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Processprocess in processes)
{
bool b = process.MainWindowTitle=="";
if (process.MainWindowTitle =="")
{
process.Kill();
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有