C#生成Word文件(图片、文字)
本文实例为大家分享了C#生成Word文件的具体代码,供大家参考,具体内容如下
通过Microsoft.Office.Interop.Word生成Word文档
1.引用类WordReport.cs,代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingMicrosoft.Office.Interop.Word;
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.Reflection;
usingSystem.IO;
namespaceCRM.Common
{
publicclassWordReport
{
private_ApplicationwordApp=null;
private_DocumentwordDoc=null;
objectunite=MSWord.WdUnits.wdStory;
ObjectNothing=Missing.Value;
public_ApplicationApplication
{
get
{
returnwordApp;
}
set
{
wordApp=value;
}
}
public_DocumentDocument
{
get
{
returnwordDoc;
}
set
{
wordDoc=value;
}
}
//通过模板创建新文档
publicvoidCreateNewDocument(stringfilePath)
{
try
{
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,refmissing,
refmissing,refmissing,refmissing,refmissing);
}
catch(Exceptionex)
{
}
}
publicvoidCreateNewDocument()
{
try
{
//killWinWordProcess();
wordApp=newApplicationClass();
wordApp.DisplayAlerts=WdAlertLevel.wdAlertsNone;
wordApp.Visible=false;
ObjectNothing=Missing.Value;
wordDoc=wordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
}
catch(Exceptionex)
{
}
}
//保存新文件
publicvoidSaveDocument(stringfilePath)
{
if(File.Exists((string)filePath))
{
File.Delete((string)filePath);
}
objectfileName=filePath;
objectformat=WdSaveFormat.wdFormatDocument;//保存格式
objectmiss=System.Reflection.Missing.Value;
wordDoc.SaveAs(reffileName,refformat,refmiss,
refmiss,refmiss,refmiss,refmiss,
refmiss,refmiss,refmiss,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);
}
publicvoidInsertText(stringstrContent)
{
//写入普通文本
wordDoc.Content.InsertAfter(strContent);
wordApp.Selection.EndKey(refunite,refNothing);//将光标移动到文档末尾
}
publicvoidInsertTitle(stringstrContent)
{
//写入普通文本
wordApp.Selection.EndKey(refunite,refNothing);//将光标移动到文档末尾
try
{
wordDoc.Paragraphs.Last.Range.set_Style("标题5");
}
catch(Exceptionex)
{
wordDoc.Paragraphs.Last.Range.set_Style("Heading5");
}
wordDoc.Paragraphs.Last.Range.Font.Color=MSWord.WdColor.wdColorBlack;
wordDoc.Paragraphs.Last.Range.Font.Size=11;
wordDoc.Paragraphs.Last.Range.Font.Name="宋体";
wordDoc.Paragraphs.Last.Range.Text=strContent;
wordApp.Selection.EndKey(refunite,refNothing);//将光标移动到文档末尾
try
{
wordDoc.Paragraphs.Last.Range.set_Style("Normal");
}
catch(Exceptionex)
{
wordDoc.Paragraphs.Last.Range.set_Style("正文");
}
}
//在书签处插入值
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)
{
objectmiss=System.Reflection.Missing.Value;
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;
}
//合并单元格表id,开始行号,开始列号,结束行号,结束列号
publicvoidMergeCell(intn,introw1,intcolumn1,introw2,intcolumn2)
{
wordDoc.Content.Tables[n].Cell(row1,column1).Merge(wordDoc.Content.Tables[n].Cell(row2,column2));
}
//合并单元格表名,开始行号,开始列号,结束行号,结束列号
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)Microsoft.Office.Interop.Word.Tabletable
publicvoidSetParagraph_Table(intn,intAlign,intVertical)
{
switch(Align)
{
case-1:wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;break;//左对齐
case0:wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphCenter;break;//水平居中
case1:wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;break;//右对齐
}
switch(Vertical)
{
case-1:wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;break;//顶端对齐
case0:wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;break;//垂直居中
case1:wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalBottom;break;//底端对齐
}
}
//设置单元格内容对齐方式
publicvoidSetParagraph_Table(intn,introw,intcolumn,intAlign,intVertical)
{
switch(Align)
{
case-1:wordDoc.Content.Tables[n].Cell(row,column).Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;break;//左对齐
case0:wordDoc.Content.Tables[n].Cell(row,column).Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphCenter;break;//水平居中
case1:wordDoc.Content.Tables[n].Cell(row,column).Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;break;//右对齐
}
switch(Vertical)
{
case-1:wordDoc.Content.Tables[n].Cell(row,column).Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;break;//顶端对齐
case0:wordDoc.Content.Tables[n].Cell(row,column).Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;break;//垂直居中
case1:wordDoc.Content.Tables[n].Cell(row,column).Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalBottom;break;//底端对齐
}
}
//设置表格字体
publicvoidSetFont_Table(Microsoft.Office.Interop.Word.Tabletable,stringfontName,doublesize)
{
if(size!=0)
{
table.Range.Font.Size=Convert.ToSingle(size);
}
if(fontName!="")
{
table.Range.Font.Name=fontName;
}
}
//设置单元格字体
publicvoidSetFont_Table(intn,introw,intcolumn,stringfontName,doublesize,intbold)
{
if(size!=0)
{
wordDoc.Content.Tables[n].Cell(row,column).Range.Font.Size=Convert.ToSingle(size);
}
if(fontName!="")
{
wordDoc.Content.Tables[n].Cell(row,column).Range.Font.Name=fontName;
}
wordDoc.Content.Tables[n].Cell(row,column).Range.Font.Bold=bold;//0表示不是粗体,其他值都是
}
//是否使用边框,n表格的序号,use是或否
//该处边框参数可以用int代替bool可以让方法更全面
//具体值方法中介绍
publicvoidUseBorder(intn,booluse)
{
if(use)
{
wordDoc.Content.Tables[n].Borders.Enable=1;
//允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
}
else
{
wordDoc.Content.Tables[n].Borders.Enable=0;
}
}
//给表格插入一行,n表格的序号从1开始记
publicvoidAddRow(intn)
{
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(intn,introws)
{
objectmiss=System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Tabletable=wordDoc.Content.Tables[n];
for(inti=0;iimagePaths)
{
wordApp.Selection.EndKey(refunite,refNothing);//将光标移动到文档末尾
MSWord.Tabletable=wordDoc.Tables.Add(wordApp.Selection.Range,
tableRow,tableColumn,refNothing,refNothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable=0;//这个值可以设置得很大,例如5、13等等
//表格的索引是从1开始的。
for(inti=1;i<=tableRow;i++)
{
for(intj=1;j<=tableColumn;j++)
{
intindex=(i-1)*tableColumn+j-1;
if(index
2.生成Word文档
///
///
///
///likestringpath=@"c:\\temp\\"
publicvoidGengerateWord(stringpath)
{
stringfileName="test.doc";
WordReportreport=newWordReport();
report.CreateNewDocument();//创建文件
report.InsertTitle("标题");
floatwidth=420;//图片宽度
floatheight=200;//图片高度
report.InsertPicture("图片地址",width,height,WdParagraphAlignment.wdAlignParagraphCenter);
report.SaveDocument(path+fileName);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。