引言:這段時(shí)間有項(xiàng)目要用到c#生成Word文檔,通過(guò)網(wǎng)絡(luò)查找到很多內(nèi)容,但是功能上滿足不了個(gè)人需求,于是決定借助網(wǎng)友們已經(jīng)寫好的代碼,加以修改完善,以便于更好的交流和以后相似問(wèn)題可以迅速的解決!
備注:本文用到的相關(guān)文件,在日志結(jié)尾提供下載


注意:此處要查找的“Microsoft.Office.Interop.Word.dll”版本必須為“11.*.*.*”,“*”代表數(shù)字
按照個(gè)人編程習(xí)慣,先編寫又滿足程序功能需求的詳細(xì)的類,在主程序中通過(guò)調(diào)用這個(gè)類的方法實(shí)現(xiàn)功能。(一次編寫多次實(shí)現(xiàn))
我把這個(gè)類定義為Report,下面是我用到的Report類代碼,僅供參考,并沒有詳盡的備注和錯(cuò)誤處理,若是您直接使用我的代碼,出現(xiàn)的問(wèn)題可以恢復(fù)本日志或者Q我,空間有我的聯(lián)系方式:
word類using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;namespace TravelAgencyapplication // 根據(jù)自己需要修改命名空間{ 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 { wordDoc = value; } } // 通過(guò)模板創(chuàng)建新文檔 public void CreateNewDocument(string filePath) { try { 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, 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); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } } // 保存新文件 public void SaveDocument(string filePath) { object fileName = 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)閉wordDoc,wordApp對(duì)象 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); } // 在書簽處插入值 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 InsertTable(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.Borders.Enable = 1; //允許有邊框,默認(rèn)沒有邊框(為0時(shí)報(bào)錯(cuò),1為實(shí)線邊框,2、3為虛線邊框,以后的數(shù)字沒試過(guò)) newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;//邊框?qū)挾? if (width != 0) { newTable.PreferredWidth = width;//表格寬度 } newTable.AllowPageBreaks = false; return newTable; } // 合并單元格 表id,開始行號(hào),開始列號(hào),結(jié)束行號(hào),結(jié)束列號(hào) public void MergeCell(int n, int row1, int column1, int row2, int column2) { wordDoc.Content.Tables[n].Cell(row1, column1).Merge(wordDoc.Content.Tables[n].Cell(row2, column2)); } // 合并單元格 表名,開始行號(hào),開始列號(hào),結(jié)束行號(hào),結(jié)束列號(hào) public void MergeCell(Microsoft.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)容對(duì)齊方式 Align水平方向,Vertical垂直方向(左對(duì)齊,居中對(duì)齊,右對(duì)齊分別對(duì)應(yīng)Align和Vertical的值為-1,0,1)Microsoft.Office.Interop.Word.Table table public void SetParagraph_Table(int n, int Align, int Vertical) { switch (Align) { case -1: wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;//左對(duì)齊 case 0: wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;//水平居中 case 1: wordDoc.Content.Tables[n].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;//右對(duì)齊 } switch (Vertical) { case -1: wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;//頂端對(duì)齊 case 0: wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break;//垂直居中 case 1: wordDoc.Content.Tables[n].Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break;//底端對(duì)齊 } } // 設(shè)置單元格內(nèi)容對(duì)齊方式 public void SetParagraph_Table(int n, int row, int column, int Align, int Vertical) { switch (Align) { case -1: wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;//左對(duì)齊 case 0: wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;//水平居中 case 1: wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;//右對(duì)齊 } switch (Vertical) { case -1: wordDoc.Content.Tables[n].Cell(row, column).Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;//頂端對(duì)齊 case 0: wordDoc.Content.Tables[n].Cell(row, column).Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCente
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注