生活随笔
收集整理的這篇文章主要介紹了
C#实现动态生成Word
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 一個(gè)控制臺(tái)例子,實(shí)現(xiàn)動(dòng)態(tài)生成Word。
首先,添加引用:COM->Microsoft Word 11.0 Object Library。
| using System.Collections.Generic; |
| using Microsoft.Office.Interop.Word; |
| ????????static void Main(string[] args) |
| ????????????string message = CreateWordFile(); |
| ????????????Console.WriteLine(message); |
| ????????????Console.ReadLine(); |
| ????????/// 創(chuàng)建Word文件 |
| ????????/// <returns></returns> |
| ?????????public static string CreateWordFile() |
| ????????????string message = ""; |
| ????????????????Object Nothing = System.Reflection.Missing.Value; |
| ????????????????string name = DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc"; |
| ????????????????object filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);? //文件保存路徑 |
| ????????????????//創(chuàng)建Word文檔 |
| ????????????????Application WordApp = new ApplicationClass(); |
| ????????????????Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); |
| ????????????????//添加頁(yè)眉 |
| ????????????????WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; |
| ????????????????WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; |
| ????????????????WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁(yè)眉內(nèi)容]"); |
| ????????????????WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;//設(shè)置右對(duì)齊 |
| ????????????????WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出頁(yè)眉設(shè)置 |
| ????????????????WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設(shè)置文檔的行間距 |
| ????????????????//移動(dòng)焦點(diǎn)并換行 |
| ????????????????object count = 14; |
| ????????????????object WdLine = WdUnits.wdLine;//換一行; |
| ????????????????WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動(dòng)焦點(diǎn) |
| ????????????????WordApp.Selection.TypeParagraph();//插入段落 |
| ????????????????//文檔中創(chuàng)建表格 |
| ????????????????Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); |
| ????????????????//設(shè)置表格樣式 |
| ????????????????newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap; |
| ????????????????newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle; |
| ????????????????newTable.Columns[1].Width = 100f; |
| ????????????????newTable.Columns[2].Width = 220f; |
| ????????????????newTable.Columns[3].Width = 105f; |
| ????????????????//填充表格內(nèi)容 |
| ????????????????newTable.Cell(1, 1).Range.Text = "產(chǎn)品詳細(xì)信息表"; |
| ????????????????newTable.Cell(1, 1).Range.Bold = 2;//設(shè)置單元格中字體為粗體 |
| ????????????????newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); |
| ????????????????WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 |
| ????????????????WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 |
| ????????????????//填充表格內(nèi)容 |
| ????????????????newTable.Cell(2, 1).Range.Text = "產(chǎn)品基本信息"; |
| ????????????????newTable.Cell(2, 1).Range.Font.Color = WdColor.wdColorDarkBlue;//設(shè)置單元格內(nèi)字體顏色 |
| ????????????????newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); |
| ????????????????WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; |
| ????????????????//填充表格內(nèi)容 |
| ????????????????newTable.Cell(3, 1).Range.Text = "品牌名稱(chēng):"; |
| ????????????????newTable.Cell(3, 2).Range.Text = "HTC"; |
| ????????????????//縱向合并單元格 |
| ????????????????newTable.Cell(3, 3).Select();//選中一行 |
| ????????????????object moveUnit = WdUnits.wdLine; |
| ????????????????object moveCount = 5; |
| ????????????????object moveExtend = WdMovementType.wdExtend; |
| ????????????????WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); |
| ????????????????WordApp.Selection.Cells.Merge(); |
| ????????????????string FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.gif");//圖片所在路徑 |
| ????????????????object LinkToFile = false; |
| ????????????????object SaveWithDocument = true; |
| ????????????????object Anchor = WordDoc.Application.Selection.Range; |
| ????????????????WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); |
| ????????????????WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//圖片寬度 |
| ????????????????WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//圖片高度 |
| ????????????????//將圖片設(shè)置為四周環(huán)繞型 |
| ????????????????Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); |
| ????????????????s.WrapFormat.Type = WdWrapType.wdWrapSquare; |
| ????????????????newTable.Cell(12, 1).Range.Text = "產(chǎn)品特殊屬性"; |
| ????????????????newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); |
| ????????????????//在表格中增加行 |
| ????????????????WordDoc.Content.Tables[1].Rows.Add(ref Nothing); |
| ????????????????WordDoc.Paragraphs.Last.Range.Text = "文檔創(chuàng)建時(shí)間:" + DateTime.Now.ToString();//“落款” |
| ????????????????WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight; |
| ????????????????WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); |
| ????????????????WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); |
| ????????????????WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); |
| ????????????????message = name + "文檔生成成功!"; |
| ????????????????message = "文件導(dǎo)出異常!"; |
| ????????????return message; |
?
2.?操作Word對(duì)象匯總
創(chuàng)建新Word
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
?
打開(kāi)文檔:
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| object fileName = @"E:\CCCXCXX\TestDoc.doc"; |
| oDoc = oWord.Documents.Open(ref fileName, |
| ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, |
| ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, |
| ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); |
?
導(dǎo)入模板
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| object fileName = @"E:\XXXCCX\Test.doc"; |
| oDoc = oWord.Documents.Add(ref fileName, ref oMissing, |
| ????????????????ref oMissing, ref oMissing); |
?
添加新表
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
| Word.Range tableLocation = oDoc.Range(ref start, ref end); |
| oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); |
?
表插入行
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
| Word.Range tableLocation = oDoc.Range(ref start, ref end); |
| oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); |
| Word.Table newTable = oDoc.Tables[1]; |
| object beforeRow = newTable.Rows[1]; |
| newTable.Rows.Add(ref beforeRow); |
?
單元格合并
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
| Word.Range tableLocation = oDoc.Range(ref start, ref end); |
| oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); |
| Word.Table newTable = oDoc.Tables[1]; |
| object beforeRow = newTable.Rows[1]; |
| newTable.Rows.Add(ref beforeRow); |
| Word.Cell cell = newTable.Cell(1, 1); |
| cell.Merge(newTable.Cell(1, 2)); |
?
單元格分離
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
| Word.Range tableLocation = oDoc.Range(ref start, ref end); |
| oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); |
| Word.Table newTable = oDoc.Tables[1]; |
| object beforeRow = newTable.Rows[1]; |
| newTable.Rows.Add(ref beforeRow); |
| Word.Cell cell = newTable.Cell(1, 1); |
| cell.Merge(newTable.Cell(1, 2)); |
| cell.Split(ref Rownum, ref? Columnnum); |
?
通過(guò)段落控制插入
?
view sourceprint?
| object oMissing = System.Reflection.Missing.Value; |
| object oEndOfDoc = "\\endofdoc";? |
| //Start Word and create a new document. |
| oWord = new Word.Application(); |
| oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, |
| ????ref oMissing, ref oMissing); |
| //Insert a paragraph at the beginning of the document. |
| oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); |
| oPara1.Range.Text = "Heading 1"; |
| oPara1.Range.Font.Bold = 1; |
| oPara1.Format.SpaceAfter = 24;??? //24 pt spacing after paragraph. |
oPara1.Range.InsertParagraphAfter();
?
?轉(zhuǎn)自:
http://www.soaspx.com/dotnet/csharp/csharp_20110707_7822.html
轉(zhuǎn)載于:https://www.cnblogs.com/mrchenzh/archive/2012/04/09/2438641.html
總結(jié)
以上是生活随笔為你收集整理的C#实现动态生成Word的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。