word模板生成C#源码Word文件下载.docx
- 文档编号:17526469
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:15
- 大小:262.58KB
word模板生成C#源码Word文件下载.docx
《word模板生成C#源码Word文件下载.docx》由会员分享,可在线阅读,更多相关《word模板生成C#源码Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
第一部分,Report类的编码
这部分我已经封装好,为文件“Report.cs”,可以直接使用
代码如下:
(有比较详细的注释)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingMicrosoft.Office.Interop.Word;
namespaceMYNAMESPACE//这边需要换成自己的命名空间名
{
classReport
{
private_ApplicationwordApp=null;
private_DocumentwordDoc=null;
public_ApplicationApplication
get
returnwordApp;
}
set
wordApp=value;
public_DocumentDocument
returnwordDoc;
wordDoc=value;
//通过模板创建新文档
publicvoidCreateNewDocument(stringfilePath)
killWinWordProcess();
wordApp=newApplicationClass();
wordApp.DisplayAlerts=WdAlertLevel.wdAlertsNone;
wordApp.Visible=false;
objectmissing=System.Reflection.Missing.Value;
objecttemplateName=filePath;
wordDoc=wordApp.Documents.Open(reftemplateName,refmissing,
refmissing,refmissing,refmissing,refmissing,refmissing,
refmissing,refmissing,refmissing,refmissing);
//保存新文件
publicvoidSaveDocument(stringfilePath)
objectfileName=filePath;
objectformat=WdSaveFormat.wdFormatDocument;
//保存格式
objectmiss=System.Reflection.Missing.Value;
wordDoc.SaveAs(reffileName,refformat,refmiss,
refmiss,refmiss,refmiss,refmiss,
refmiss);
//关闭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);
returntrue;
returnfalse;
//插入表格,bookmark书签
publicTableInsertTable(stringbookmark,introws,intcolumns,floatwidth)
objectoStart=bookmark;
Rangerange=wordDoc.Bookmarks.get_Item(refoStart).Range;
//表格插入位置
TablenewTable=wordDoc.Tables.Add(range,rows,columns,refmiss,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,introw1,intcolumn1,introw2,intcolumn2)
table.Cell(row1,column1).Merge(table.Cell(row2,column2));
//设置表格内容对齐方式Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1)
publicvoidSetParagraph_Table(Microsoft.Office.Interop.Word.Tabletable,intAlign,intVertical)
switch(Align)
case-1:
table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;
break;
//左对齐
case0:
table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphCenter;
//水平居中
case1:
table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;
//右对齐
switch(Vertical)
table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;
//顶端对齐
table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//垂直居中
table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalBottom;
//底端对齐
//设置表格字体
publicvoidSetFont_Table(Microsoft.Office.Interop.Word.Tabletable,stringfontName,doublesize)
if(size!
table.Range.Font.Size=Convert.ToSingle(size);
if(fontName!
="
"
)
table.Range.Font.Name=fontName;
//是否使用边框,n表格的序号,use是或否
publicvoidUseBorder(intn,booluse)
if(use)
wordDoc.Content.Tables[n].Borders.Enable=1;
else
wordDoc.Content.Tables[n].Borders.Enable=2;
//给表格插入一行,n表格的序号从1开始记
publicvoidAddRow(intn)
wordDoc.Content.Tables[n].Rows.Add(refmiss);
//给表格添加一行
publicvoidAddRow(Microsoft.Office.Interop.Word.Tabletable)
table.Rows.Add(refmiss);
//给表格插入rows行,n为表格的序号
publicvoidAddRow(intn,introws)
Microsoft.Office.Interop.Word.Tabletable=wordDoc.Content.Tables[n];
for(inti=0;
i<
rows;
i++)
//给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
publicvoidInsertCell(Microsoft.Office.Interop.Word.Tabletable,introw,intcolumn,stringvalue)
table.Cell(row,column).Range.Text=value;
//给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
publicvoidInsertCell(intn,introw,intcolumn,stringvalue)
wordDoc.Content.Tables[n].Cell(row,column).Range.Text=value;
//给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值
publicvoidInsertCell(intn,introw,intcolumns,string[]values)
columns;
table.Cell(row,i+1).Range.Text=values[i];
//插入图片
publicvoidInsertPicture(stringbookmark,stringpicturePath,floatwidth,floathight)
ObjectlinkToFile=false;
//图片是否为外部链接
ObjectsaveWithDocument=true;
//图片是否随文档一起保存
objectrange=wordDoc.Bookmarks.get_Item(refoStart).Range;
//图片插入位置
wordDoc.InlineShapes.AddPicture(picturePath,reflinkToFile,refsaveWithDocument,refrange);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width=width;
//设置图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height=hight;
//设置图片高度
//插入一段文字,text为文字内容
publicvoidInsertText(stringbookmark,stringtext)
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.Processprocessinprocesses)
boolb=process.MainWindowTitle=="
if(process.MainWindowTitle=="
process.Kill();
}
第二部分,具体生成文档的编码
代码见下文:
1,首先需要载入模板
Reportreport=newReport();
report.CreateNewDocument(TemPath);
//模板路径
2,插入一个值
report.InsertValue("
Bookmark_value"
"
世界杯"
//在书签“Bookmark_value”处插入值
3,创建一个表格
Tabletable=report.InsertTable("
Bookmark_table"
2,3,0);
//在书签“Bookmark_table”处插入2行3列行宽最大的表
4,合并单元格
report.MergeCell(table,1,1,1,3);
//表名,开始行号,开始列号,结束行号,结束列号
5,表格添加一行
report.AddRow(table);
//表名
6,在单元格中插入值
report.InsertCell(table,2,1,"
R2C1"
//表名,行号,列号,值
7,设置表格中文字的对齐方式
report.SetParagraph_Table(table,-1,0);
//水平方向左对齐,垂直方向居中对齐
8,设置表格字体
report.SetFont_Table(table,"
宋体"
9);
//宋体9磅
9,给现有的表格添加一行
report.AddRow
(1);
//给模板中第一个表格添加一行
10,确定现有的表格是否使用边框
report.UseBorder(1,true);
//模板中第一个表格使用实线边框
11,给现有的表格添加多行
report.AddRow(1,2);
//给模板中第一个表格插入2行
12,给现有的表格插入一行数据
string[]values={"
英超"
意甲"
德甲"
西甲"
法甲"
};
report.InsertCell(1,2,5,values);
//给模板中第一个表格的第二行的5列分别插入数据
12,插入图片
stringpicturePath=@"
C:
\DocumentsandSettings\Administrator\桌面\1.jpg"
report.InsertPicture("
Bookmark_picture"
picturePath,150,150);
//书签位置,图片路径,图片宽度,图片高度
13,插入一段文字
stringtext="
长期从事电脑操作者,应多吃一些新鲜的蔬菜和水果,同时增加维生素A、B1、C、E的摄入。
为预防角膜干燥、眼干涩、视力下降、甚至出现夜盲等,电脑操作者应多吃富含维生素A的食物,如豆制品、鱼、牛奶、核桃、青菜、大白菜、空心菜、西红柿及新鲜水果等。
report.InsertText("
Bookmark_text"
text);
14,最后保存文档
report.SaveDocument(RepPath);
//文档路径
第四步,运行程序生成文档,并查看生成的文档
自动生成的文档如下图
测试中遇到的BUG
1.出现两个对话框,一个对话框提示“(前面省略)…被呼叫方拒绝接收呼叫。
(ExceptionfromHRESULT:
0x80010001(RPC_E_CALL_REJECTED))”;
另一个对话框提示““**(此处是模板文件名)”中的拼写或语法错误过多,无法继续显示。
如果要检查此文档的拼写和语法,请选择“工具”菜单中的“拼写和语法”命令。
”。
分析原因:
这可能是由于Word中有对英文的拼写和语法有自动检测的功能,而使某些字母组合被认为是语法错误。
解决方案:
选择“工具”菜单中的“拼写和语法”命令,将所有“不在词典中:
”的红色字体“添加到词典”,如下图所示:
如果程序自动生成报告失败,那么程序中调用的模板的进程不会自动结束,需要在任务管理器中手动删除模板的进程“WINWORD.EXE”。
测试环境
操作系统:
WindowsXPsp3
语言:
c#
编译器:
MicrosoftVisualStudio2005
框架版本:
Microsoft.NETFramework2.0
Office版本:
MicrosoftOffice2003
结束
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- word 模板 生成 C# 源码