国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C#程序通過模板自動創(chuàng)建Word文檔

2019-11-17 02:53:02
字體:
來源:轉載
供稿:網友
C#程序通過模板自動創(chuàng)建Word文檔

引言:前段時間有項目要用c#生成Word格式的計算報告,通過網絡查找到很多內容,但是都很凌亂,于是自己決定將具體的步驟總結整理出來,以便于更好的交流和以后相似問題可以迅速的解決!

現(xiàn)通過具體的示例演示具體的步驟:

第一步,制作模板

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.*.*.*”,“*”代表數(shù)字

第三步,編碼

這一步分成兩個部分

第一部分,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

{

get

{

returnwordDoc;

}

set

{

wordDoc=value;

}

}

//通過模板創(chuàng)建新文檔

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為虛線邊框,以后的數(shù)字沒試過)

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為虛線邊框,以后的數(shù)字沒試過)

}

else

{

wordDoc.Content.Tables[n].Borders.Enable=2;//允許有邊框,默認沒有邊框(為0時無邊框,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過)

}

}

//給表格插入一行,n表格的序號從1開始記

publicvoidAddRow(intn)

{

objectmiss=System.Reflection.Missing.Value;

wordDoc.Content.Tables[n].Rows.Add(refmiss);

}

//給表格添加一行

publicvoidAddRow(Microsoft.Office.Interop.Word.Tabletable)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 永丰县| 土默特右旗| 堆龙德庆县| 三台县| 宜阳县| 开阳县| 平湖市| 合川市| 福清市| 内江市| 铜陵市| 泰州市| 深泽县| 山阳县| 昭通市| 尉氏县| 泊头市| 洛扎县| 大石桥市| 泾川县| 鹤岗市| 九龙县| 呼和浩特市| 政和县| 余姚市| 贵南县| 栖霞市| 澄江县| 海林市| 曲周县| 霸州市| 巴青县| 新龙县| 海宁市| 海兴县| 新宁县| 弥渡县| 白玉县| 弥渡县| 南郑县| 昌江|