制作word模版,供C#程序創(chuàng)建基于模版的word文檔_第1頁
制作word模版,供C#程序創(chuàng)建基于模版的word文檔_第2頁
制作word模版,供C#程序創(chuàng)建基于模版的word文檔_第3頁
制作word模版,供C#程序創(chuàng)建基于模版的word文檔_第4頁
制作word模版,供C#程序創(chuàng)建基于模版的word文檔_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、C耀序通過模板自動創(chuàng)建Wor收檔2引言:前段時間有項目要用c#生成Wor正式的計算報告,通過網(wǎng)絡查找到很多內(nèi)容,但是 都很凌亂,于是自己決定將具體的步驟總結(jié)整理出來,以便于更好的交流和以后相似問題可以 迅速的解決!現(xiàn)通過具體的示例演示具體的步驟:第一步,制作模板1,新建一個文檔,文檔內(nèi)容如下:歐洲五孔胴鼻aI-¥ii插入圖片十3-插入一段又于.+14. 口圖12,在相應位置插入書簽;將鼠標定位到要插入書簽的位置,點擊“插入”> “書簽”,彈出對話框,輸入書簽名,點擊“添加”按鈕,書簽位置如圖3所示回洲五大聯(lián)事"鏟和k中L 插入值:(書釜 *' B ookn a

2、r k_v J. ueh:在直 J +'2,插入至片,(書簽olnOrk_pi cluru”化置1 +,3,插入TX字,(書簽 LiBc >ki' C)位置)一4,插樹“, vC 書卷"Itctanu-k ).圖33,保存模板,命名為“模板 l.dot ”或者“模板1.doc橫板1. dotHi er 05*?ft Word 棉板E5 O3稹槌】i三icro工/Wn-d文榛=lgQ 班, - :IIS_BI Ilk-S : -Ii:l llJIlia-Ji :- 4IIIS-li: 1- dl.第二步,設置項目中的引用1,右擊“解決方案資源管理器”中的項目目錄下

3、的“引用”,選擇“添加引用”,打 開“添加引用”對話框團V3V4V5,V6取消圖52,在"添加引用”對話框中,選擇" COM >" Microsoft Word 11.0 Object Library ", 點擊“確定”按鈕添加引用巨兇.HET COfl .項目|瀏跑 量近Mi crosoft n indowe Image Acqui siti on 1.01 Type LibraryMicrosoft W i ndow s Inst all er Object LibraryMi crosoft WinMTTF Service version S

4、. 1Mi crosoft WMI Scripting VI. 2 LibraryMicrosoft g0rd 11.0 Object Li'bruryMicrosoft XML, Microsoft 3(叫 Hicrosoft XML, Microsoft XML Microsoft XML,Mi crosoft XML, ver si oa 2.0Micrfisoft遠握數(shù)據(jù)對象2.0Microsoft _JScr iptMiicrosoft_Vi suilStudioOffictToclsCcntrels_ConliinsrC.確定圖63,相同操作打開“添加引用”對話框中,選擇“

5、瀏覽”項,查找到"Microsoft.Office.Interop.Word.dll”文件,選中它,點擊"確定"按鈕,.版本必須為" 11.*.*.*注意:此處要查找的“Microsoft.Office.Interop.Word.dll*”代表數(shù)字第三步,編碼這一步分成兩個部分第一部分,Report類的編碼這部分我已經(jīng)封裝好,為文件“ Report.cs ",可以直接使用代碼如下:(有比較詳細的注釋)using System;using System.Collections.Generic;using System.Text;using Mic

6、rosoft.Office.Interop.Word;namespace MYNAMESPACE/這邊需要換成自己的命名空間名class Reportprivate _Application wordApp = null ;private _Document wordDoc = null ;public _Application Applicationgetreturn wordApp;set wordApp =value ; public _Document Documentget return wordDoc; set wordDoc =value ;/通過模板創(chuàng)建新文檔public vo

7、id CreateNewDocument( string filePath)killWinWordProcess();wordApp = new ApplicationClass ();wordApp.DisplayAlerts =WdAlertLevel .wdAlertsNone;wordApp.Visible =false ;object missing = System.Reflection. Missing .Value;object templateName = filePath;wordDoc = wordApp.Documents.Open(ref templateName,

8、ref missing,ref missing, ref missing,ref missing, ref missing, ref missing,ref missing, ref missing,ref missing, ref missing, ref missing,ref missing, ref missing,ref missing, ref missing);/保存新文件public void SaveDocument( string filePath)object fileName = filePath;object format = WdSaveFormatwdFormat

9、Document; / 保存格式 object miss = System.Reflection. Missing .Value;wordDoc.SaveAs(reffileName,refformat, ref miss,ref miss,refmiss, refmiss,ref miss,ref miss,refmiss, refmiss,ref miss,ref miss,refmiss, refmiss,ref miss,ref miss);/ 關閉 wordDoc,wordApp對象object SaveChanges = WdSaveOptions.wdSaveChanges;ob

10、ject OriginalFormat = WdOriginalFormat .wdOriginalDocumentFormat;object RouteDocument = false ;wordDoc.Close( ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);/在書簽處插入值public bool InsertValue( string bookmark, string value)o

11、bject bkObj = bookmark;if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark)wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();wordApp.Selection.TypeText(value);return true ; return false ; /插入表格,bookmark書簽public Table InsertTable( string bookmark, int rows, int columns, float width) obje

12、ct miss = System.Reflection. Missing .Value;object oStart = bookmark;Range range = wordDoc.Bookmarks.get_Item( ref oStart).Range; 表格插入位置 Table newTable = wordDoc.Tables.Add(range, rows, columns,ref miss, ref miss);/設置表的格式newTable.Borders.Enable= 1;允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過)newTable

13、.Borders.OutsideLineWidth =WdLineWidth.wdLineWidth050pt; / 邊框?qū)挾萯f (width != 0) newTable.PreferredWidth = width;/ 表格寬度 newTable.AllowPageBreaks =false ;return newTable; /合并單元格 表名,開始行號,開始列號,結(jié)束行號,結(jié)束列號 public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, introw2, int c

14、olumn2) table.Cell(row1, column1).Merge(table.Cell(row2, column2); 設置表格內(nèi)容對齊方式 Align水平方向,Vertical垂直方向(左對齊,居中對齊,右對齊分別對應 Align 和 Vertical 的值為-1,0,1)public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, intVertical) switch (Align) case -1: table.Range.ParagraphFormat.Align

15、ment =WdParagraphAlignment .wdAlignParagraphLeft; break ; / 左對齊case 0: table.Range.ParagraphFormat.Alignment =WdParagraphAlignment .wdAlignParagraphCenter; break ; / 水平居中case 1: table.Range.ParagraphFormat.Alignment =WdParagraphAlignment .wdAlignParagraphRight; break ; / 右對齊 switch (Vertical) case -

16、1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment .wdCellAlignVerticalTop; break; 頂端對齊 case 0: table.Range.Cells.VerticalAlignment =WdCellVerticalAlignment .wdCellAlignVerticalCenter;break; 垂直居中case 1: table.Range.Cells.VerticalAlignment =WdCellVerticalAlignment .wdCellAlignVerticalBo

17、ttom;break ;/底端對齊/設置表格字體public void SetFont_Table(Microsoft.Office.Interop.Word. double size) if (size != 0)table.Range.Font.Size =Convert .ToSingle(size);if (fontName !="")table.Range.Font.Name = fontName; /是否使用邊框,n表格的序號,use是或否public void UseBorder( int n, bool use) if (use) wordDoc.Conte

18、nt.Tablesn.Borders.Enable= 1;錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過)elsewordDoc.Content.Tablesn.Borders.Enable= 2;錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過)Table table, string fontName,/允許有邊框,默認沒有邊框(為0時報/允許有邊框,默認沒有邊框(為0時報/給表格插入一行,n表格的序號從1開始記public void AddRow(int n)object miss = System.Reflection. Missing .Value;wordDoc.Conte

19、nt.Tablesn.Rows.Add(ref miss);/給表格添加一行 public void AddRow(Microsoft.Office.Interop.Word. Table table)object miss = System.Reflection. Missing .Value;table.Rows.Add(ref miss);/給表格插入rows行,n為表格的序號 public void AddRow(int n, int rows)object miss = System.Reflection. Missing .Value;Microsoft.Office.Intero

20、p.Word.Table table = wordDoc.Content.Tablesn;for ( int i = 0; i < rows; i+)table.Rows.Add(ref miss);給表格中單元格插入元素,table所在表格,row行號,column列號,value插入的元素public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column,string value) table.Cell(row, column).Range.Text = value; 給表格中單元

21、格插入元素,n表格的序號從1開始記,row行號,column列號,value插入的元素public void InsertCell( int n, int row, int column, string value)wordDoc.Content.Tablesn.Cell(row, column).Range.Text = value; 給表格插入一行數(shù)據(jù),n為表格的序號,row行號,columns列數(shù),values插入的值 public void InsertCell( int n, int row, int columns, string values)Microsoft.Office.I

22、nterop.Word.Table table = wordDoc.Content.Tablesn;for ( int i = 0; i < columns; i+) table.Cell(row, i + 1).Range.Text = valuesi; /插入圖片public void InsertPicture( string bookmark, string picturePath, float width, float hight) object miss = System.Reflection. Missing .Value;object oStart = bookmark;

23、Object linkToFile = false ;/圖片是否為外部鏈接Object saveWithDocument = true ;/ 圖片是否隨文檔一起保存object range = wordDoc.Bookmarks.get_Item( ref oStart).Range;圖片插入位置wordDoc.InlineShapes.AddPicture(picturePath,ref linkToFile, ref saveWithDocument, refrange);wordDoc.Application.ActiveDocument.InlineShapes1.Width = wi

24、dth;/ 設置圖片寬度wordDoc.Application.ActiveDocument.InlineShapes1.Height = hight;/ 設置圖片高度/插入一段文字,text為文字內(nèi)容public void InsertText( string bookmark, string text) object oStart = bookmark;object range = wordDoc.Bookmarks.get_Item( ref oStart).Range;Paragraph wp = wordDoc.Content.Paragraphs.Add( ref range);w

25、p.Format.SpaceBefore = 6;wp.Range.Text = text;wp.Format.SpaceAfter = 24;wp.Range.InsertParagraphAfter();wordDoc.Paragraphs.Last.Range.Text ="n"/ 殺掉 winword.exe 進程 public void killWinWordProcess()System.Diagnostics.Process processes =System.Diagnostics. Process .GetProcessesByName( "WI

26、NWORD" foreach (System.Diagnostics. Process process in processes) bool b = process.MainWindowTitle =""if (process.MainWindowTitle ="")process.Kill();第二部分,具體生成文檔的編碼代碼見下文:1,首先需要載入模板Report report = new Report ();report.CreateNewDocument(TemPath); / 模板路徑2,插入一個值report.InsertValue

27、( "Bookmark_value","世界杯");/ 在書簽"Bookmark_value ”處插入值3,創(chuàng)建一個表格Table table = report.InsertTable( "Bookmark_table” , 2, 3, 0);/ 在書簽"Bookmark_table ” 處才f入 2行3列行寬最大的表4,合并單元格report.MergeCell(table, 1, 1, 1, 3);/表名,開始行號,開始列號,結(jié)束行號,結(jié)束列號5,表格添加一行report.AddRow(table); / 表名6,在單元

28、格中插入值report.InsertCell(table, 2, 1,"R2C1"); / 表名,行號,列號,值7,設置表格中文字的對齊方式report.SetParagraph_Table(table, -1,0);水平方向左對齊,垂直方向居中對齊8,設置表格字體report.SetFont_Table(table,"宋體",9); / 宋體 9磅9,給現(xiàn)有的表格添加一行report.AddRow(1); /給模板中第一個表格添加一行10,確定現(xiàn)有的表格是否使用邊框report.UseBorder(1, true ); /模板中第一個表格使用實線邊框1

29、1,給現(xiàn)有的表格添加多行report.AddRow(1, 2);給模板中第一個表格插入2行12,給現(xiàn)有的表格插入一行數(shù)據(jù)string 口 values="英超","意甲","德甲","西甲","法甲"report.InsertCell(1, 2, 5, values);給模板中第一個表格的第二行的5列分別插入數(shù)據(jù)桌面 1.jpg"/書簽位置,圖片路徑,圖片寬度,12,插入圖片string picturePath =report.InsertPicture( "Bookmark_picture" , picturePath, 150, 150);圖片高度13,插

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論