




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C#程序通過模板自動創(chuàng)建Word文檔2引言:前段時間有項目要用c#生成Word格式的計算報告,通過網(wǎng)絡查找到很多內(nèi)容,但是都很凌亂,于是自己決定將具體的步驟總結(jié)整理出來,以便于更好的交流和以后相似問題可以迅速的解決! 現(xiàn)通過具體的示例演示具體的步驟:第一步,制作模板1,新建一個文檔,文檔內(nèi)容如下:圖12,在相應位置插入書簽;將鼠標定位到要插入書簽的位置,點擊“插入”>“書簽”,彈出對話框,輸入書簽名,點擊“添加”按鈕,書簽位置如圖3所示圖2圖33,保存模板,命名為“模板1.dot”或者“模板1.doc”圖4第二步,設(shè)置項目中的引用1,右擊“解決方案資源管理器”中的項目目錄下的“引用”,選
2、擇“添加引用”,打開“添加引用”對話框圖52,在“添加引用”對話框中,選擇“COM”>“Microsoft Word 11.0 Object Library”,點擊“確定”按鈕圖6 3,相同操作打開“添加引用”對話框中,選擇“瀏覽”項,查找到”文件,選中它,點擊“確定”按鈕圖7注意:此處要查找的“”版本必須為“11.*.*.*”,“*”代表數(shù)字第三步,編碼這一步分成兩個部分第一部分,Report類的編碼這部分我已經(jīng)封裝好,為文件“Report.cs”,可以直接使用代碼如下:(有比較詳細的注釋)using System;using System.Collections.Generic;us
3、ing System.Text;using Microsoft.Office.Interop.Word;namespace MYNAMESPACE /這邊需要換成自己的命名空間名 class Report private _Application wordApp = null; private _Document wordDoc = null; public _Application Application get return wordApp; set wordApp = value; public _Document Document get return wordDoc; set wor
4、dDoc = value; /通過模板創(chuàng)建新文檔 public void CreateNewDocument(string filePath) killWinWordProcess(); wordApp = new ApplicationClass(); wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; object templateName = filePath; wordDoc = word
5、App.Documents.Open(ref templateName, 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 =
6、filePath; object format = WdSaveFormat.wdFormatDocument;/保存格式 object miss = System.Reflection.Missing.Value; wordDoc.SaveAs(ref fileName, ref format, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); /關(guān)閉wordDo
7、c,wordApp對象 object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
8、/在書簽處插入值 public bool InsertValue(string bookmark, string value) object 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 In
9、sertTable(string bookmark, int rows, int columns, float width) object 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); /設(shè)置表的格式 newTable.B
10、orders.Enable = 1; /允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過) newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;/邊框?qū)挾?if (width != 0) newTable.PreferredWidth = width;/表格寬度 newTable.AllowPageBreaks = false; return newTable; /合并單元格 表名,開始行號,開始列號,結(jié)束行號,結(jié)束列號 public void MergeCell(Microso
11、ft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2) table.Cell(row1, column1).Merge(table.Cell(row2, column2); /設(shè)置表格內(nèi)容對齊方式 Align水平方向,Vertical垂直方向(左對齊,居中對齊,右對齊分別對應Align和Vertical的值為-1,0,1) public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align
12、, int Vertical) switch (Align) case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;/左對齊 case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;/水平居中 case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAl
13、ignment.wdAlignParagraphRight; break;/右對齊 switch (Vertical) case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;/頂端對齊 case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break;/垂直居中 case 1: table.Range.Cell
14、s.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break;/底端對齊 /設(shè)置表格字體 public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size) if (size != 0) table.Range.Font.Size = Convert.ToSingle(size); if (fontName != "") table.Range.Fon
15、t.Name = fontName; /是否使用邊框,n表格的序號,use是或否 public void UseBorder(int n,bool use) if (use) wordDoc.Content.Tablesn.Borders.Enable = 1; /允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過) else wordDoc.Content.Tablesn.Borders.Enable = 2; /允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以后的數(shù)字沒試過) /給表格插入一行,n表格的序號從1開始記 public
16、 void AddRow(int n) object miss = System.Reflection.Missing.Value; wordDoc.Content.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,
17、 int rows) object miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.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,
18、 int row, int column, string value) table.Cell(row, column).Range.Text = value; /給表格中單元格插入元素,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插
19、入的值 public void InsertCell(int n, int row, int columns, string values) Microsoft.Office.Interop.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 widt
20、h, float hight) object miss = System.Reflection.Missing.Value; object oStart = bookmark; Object linkToFile = false; /圖片是否為外部鏈接 Object saveWithDocument = true; /圖片是否隨文檔一起保存 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;/圖片插入位置 wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile,
21、 ref saveWithDocument, ref range); wordDoc.Application.ActiveDocument.InlineShapes1.Width = width; /設(shè)置圖片寬度 wordDoc.Application.ActiveDocument.InlineShapes1.Height = hight; /設(shè)置圖片高度 /插入一段文字,text為文字內(nèi)容 public void InsertText(string bookmark, string text) object oStart = bookmark; object range = wordDoc.
22、Bookmarks.get_Item(ref oStart).Range; Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range); wp.Format.SpaceBefore = 6; wp.Range.Text = text; wp.Format.SpaceAfter = 24; wp.Range.InsertParagraphAfter(); wordDoc.Paragraphs.Last.Range.Text = "n" / 殺掉winword.exe進程 public void killWinWordPro
23、cess() System.Diagnostics.Process processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); foreach (System.Diagnostics.Process process in processes) bool b = process.MainWindowTitle = "" if (process.MainWindowTitle = "") process.Kill(); 第二部分,具體生成文檔的編碼代碼見下文:
24、1,首先需要載入模板Report report = new Report();report.CreateNewDocument(TemPath); /模板路徑2,插入一個值report.InsertValue("Bookmark_value", "世界杯");/在書簽“Bookmark_value”處插入值3,創(chuàng)建一個表格Table table = report.InsertTable("Bookmark_table", 2, 3, 0); /在書簽“Bookmark_table”處插入2行3列行寬最大的表4,合并單元格report.
25、MergeCell(table, 1, 1, 1, 3); /表名,開始行號,開始列號,結(jié)束行號,結(jié)束列號5,表格添加一行report.AddRow(table); /表名6,在單元格中插入值report.InsertCell(table, 2, 1, "R2C1");/表名,行號,列號,值7,設(shè)置表格中文字的對齊方式report.SetParagraph_Table(table, -1, 0);/水平方向左對齊,垂直方向居中對齊8,設(shè)置表格字體report.SetFont_Table(table, "宋體", 9);/宋體9磅9,給現(xiàn)有的表格添加一行r
26、eport.AddRow(1); /給模板中第一個表格添加一行10,確定現(xiàn)有的表格是否使用邊框report.UseBorder(1, true); /模板中第一個表格使用實線邊框11,給現(xiàn)有的表格添加多行report.AddRow(1, 2); /給模板中第一個表格插入2行12,給現(xiàn)有的表格插入一行數(shù)據(jù)string values= "英超", "意甲", "德甲", "西甲", "法甲" ;report.InsertCell(1, 2, 5, values); /給模板中第一個表格的第二行的5列分別插入數(shù)據(jù)12,插入圖片string picturePath = "C:Documents and SettingsAdministra
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 氣道吸入性損傷的護理
- 科學活動《小樹排排隊》設(shè)計大綱
- 招商人員年終工作總結(jié)
- 關(guān)于《玉堂春》的舞臺表演藝術(shù)探究
- 生產(chǎn)總監(jiān)年度工作總結(jié)
- 元旦的節(jié)日教育
- 沉井施工環(huán)保措施及責任協(xié)議書
- 綠色能源營銷團隊勞動合同規(guī)范解讀
- 企業(yè)核心財務數(shù)據(jù)保密及財務總監(jiān)責任合同書
- 餐飲連鎖品牌加盟權(quán)及管理權(quán)轉(zhuǎn)讓合同
- AS9100內(nèi)審員培訓教材
- 新老物業(yè)移交表格(全套)
- 人教版七年級下冊英語單詞辨音訓練題(一)
- 農(nóng)村公路安全防護工程施工組織設(shè)計
- 企業(yè)培訓邀請函(4篇)
- 精裝房驗房項目表格
- 浙江省財政支出專項項目績效評價綜合報告
- 《紅樓夢》PPT課件(優(yōu)秀)
- 新高考英語讀后續(xù)寫——故事編寫思路
- 最新煙葉儲存保管方法標準
- 帶式輸送機傳動裝置二級斜齒圓柱齒輪減速器設(shè)計(全套圖紙)
評論
0/150
提交評論