引言:前段時間有項目要用c#生成Word格式的計算報告,通過網絡查找到很多內容,但是都很凌亂,于是自己決定將具體的步驟總結整理出來,以便于更好的交流和以后相似問題可以迅速的解決!
現通過具體的示例演示具體的步驟:
1,新建一個文檔,文檔內容如下:
2,在相應位置插入書簽;將鼠標定位到要插入書簽的位置,點擊“插入”>“書簽”,彈出對話框,輸入書簽名,點擊“添加”按鈕,書簽位置如圖3所示
3,保存模板,命名為“模板1.dot”或者“模板1.doc”
1,右擊“解決方案資源管理器”中的項目目錄下的“引用”,選擇“添加引用”,打開“添加引用”對話框
2,在“添加引用”對話框中,選擇“COM”>“Microsoft Word 11.0 Object Library”,點擊“確定”按鈕
3,相同操作打開“添加引用”對話框中,選擇“瀏覽”項,查找到”Microsoft.Office.Interop.Word.dll”文件,選中它,點擊“確定”按鈕
注意:此處要查找的“Microsoft.Office.Interop.Word.dll”版本必須為“11.*.*.*”,“*”代表數字
這一步分成兩個部分
這部分我已經封裝好,為文件“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
{
get
{
returnwordDoc;
}
set
{
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,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,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);
}
//在書簽處插入值
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;
}
//合并單元格 表名,開始行號,開始列號,結束行號,結束列號
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;break;//水平居中
case1:table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;break;//右對齊
}
switch(Vertical)
{
case-1:table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;break;//頂端對齊
case0:table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;break;//垂直居中
case1:table.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;
}
}
//是否使用邊框,n表格的序號,use是或否
publicvoidUseBorder(intn,booluse)
{
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(intn)
{
objectmiss=System.Reflection.Missing.Value;
wordDoc.Content.Tables[n].Rows.Add(refmiss);
}
//給表格添加一行
publicvoidAddRow(Microsoft.Office.Interop.Word.Tabletable)
新聞熱點
疑難解答