日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C#实现动态生成Word

發(fā)布時(shí)間:2025/4/5 C# 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#实现动态生成Word 小編覺得挺不錯(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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
??
namespace TestWord
{
????class Program
????{
????????static void Main(string[] args)
????????{
????????????string message = CreateWordFile();
????????????Console.WriteLine(message);
????????????Console.ReadLine();
????????}
??
??
????????/// <summary>
????????/// 創(chuàng)建Word文件
????????/// </summary>
????????/// <returns></returns>
?????????public static string CreateWordFile()
????????{
????????????string message = "";
????????????try
????????????{
????????????????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 = "品牌名稱:";
????????????????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 + "文檔生成成功!";
????????????}
????????????catch
????????????{
????????????????message = "文件導(dǎo)出異常!";
????????????}
????????????return message;
????????}
????}
}

?

2.?操作Word對(duì)象匯總

創(chuàng)建新Word

object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);

?

打開文檔:

?

view sourceprint?
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
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;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
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;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);
?
object start = 0;
object end = 0;
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;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);
?
object start = 0;
object end = 0;
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;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);
?
object start = 0;
object end = 0;
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;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);
?
object start = 0;
object end = 0;
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));
?
object Rownum = 2;
object Columnnum = 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.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
????ref oMissing, ref oMissing);
?
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
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)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。