資源簡(jiǎn)介
第一步,制作模板
1.新建一個(gè)文檔,設(shè)置文檔內(nèi)容。
2.在相應(yīng)位置插入書(shū)簽;將鼠標(biāo)定位到要插入書(shū)簽的位置,點(diǎn)擊“插入”>“書(shū)簽”,彈出對(duì)話(huà)框,輸入書(shū)簽名,點(diǎn)擊“添加”按鈕。
3.保存模板,命名為“模板1.dot”或者“模板1.doc”
第二步,設(shè)置項(xiàng)目中的引用
1.右擊“解決方案資源管理器”中的項(xiàng)目目錄下的“引用”,選擇“添加引用”,打開(kāi)“添加引用”對(duì)話(huà)框
2.在“添加引用”對(duì)話(huà)框中,選擇“COM”>“Microsoft Word 11.0 Object Library”,點(diǎn)擊“確定”按鈕
3.相同操作打開(kāi)“添加引用”對(duì)話(huà)框中,選擇“瀏覽”項(xiàng),查找到”Microsoft.Office.Interop.Word.dll”文件,選中它,點(diǎn)擊“確定”按鈕
注意:此處要查找的“Microsoft.Office.Interop.Word.dll”版本必須為“11.*.*.*”,“*”代表數(shù)字
第三步,編碼
這一步分成兩個(gè)部分
第一部分,Report類(lèi)的編碼
這部分我已經(jīng)封裝好,為文件“Report.cs”,可以直接使用
第二部分,具體生成文檔的編碼
代碼見(jiàn)下文:
1.首先需要載入模板
Report report =new Report();
report.CreateNewDocument(TemPath); //模板路徑
2.插入一個(gè)值
report.InsertValue("Bookmark_value","世界杯");//在書(shū)簽“Bookmark_value”處插入值
3.創(chuàng)建一個(gè)表格
Table table =report.InsertTable("Bookmark_table", 2, 3, 0); //在書(shū)簽“Bookmark_table”處插入2行3列行寬最大的表
4.合并單元格
report.MergeCell(table, 1, 1, 1, 3); //表名,開(kāi)始行號(hào),開(kāi)始列號(hào),結(jié)束行號(hào),結(jié)束列號(hào)
5.表格添加一行
report.AddRow(table); //表名
6.在單元格中插入值
report.InsertCell(table, 2, 1,"R2C1");//表名,行號(hào),列號(hào),值
7.設(shè)置表格中文字的對(duì)齊方式
report.SetParagraph_Table(table, -1, 0);//水平方向左對(duì)齊,垂直方向居中對(duì)齊
8.設(shè)置表格字體
report.SetFont_Table(table,"宋體", 9);//宋體9磅
9.給現(xiàn)有的表格添加一行
report.AddRow(1);//給模板中第一個(gè)表格添加一行
10.確定現(xiàn)有的表格是否使用邊框
report.UseBorder(1,true); //模板中第一個(gè)表格使用實(shí)線(xiàn)邊框
11.給現(xiàn)有的表格添加多行
report.AddRow(1, 2);//給模板中第一個(gè)表格插入2行
12.給現(xiàn)有的表格插入一行數(shù)據(jù)
string[] values={"英超", "意甲", "德甲","西甲", "法甲" };
report.InsertCell(1, 2, 5,values); //給模板中第一個(gè)表格的第二行的5列分別插入數(shù)據(jù)
13.插入圖片
string picturePath = @"C:\Documents and Settings\Administrator\桌面\1.jpg";
report.InsertPicture("Bookmark_picture",picturePath, 150, 150); //書(shū)簽位置,圖片路徑,圖片寬度,圖片高度
14.插入一段文字
string text = "長(zhǎng)期從事電腦操作者,應(yīng)多吃一些新鮮的蔬菜和水果,同時(shí)增加維生素A、B1、C、E的攝入。為預(yù)防角膜干燥、眼干澀、視力下降、甚至出現(xiàn)夜盲等,電 腦操作者應(yīng)多吃富含維生素A的食物,如豆制品、魚(yú)、牛奶、核桃、青菜、大白菜、空心菜、西紅柿及新鮮水果等。";
report.InsertText("Bookmark_text",text);
15.最后保存文檔
report.SaveDocument(RepPath); //文檔路徑
第四步,運(yùn)行程序生成文檔,并查看生成的文檔
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?MSWord?=?Microsoft.Office.Interop.Word;
namespace?ExportWordTest
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????MSWord.Application?wordApp?=?null;
????????MSWord.Document?wordDoc?=?null;
????????object?nothing?=?System.Reflection.Missing.Value;
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????doExport();
????????}
????????private?void?doExport()
????????{
????????????try
????????????{
????????????????wordApp?=?new?MSWord.ApplicationClass();
???????
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2018-11-11?14:26??ExportWordTest\
?????文件????????4039??2018-11-11?15:31??ExportWordTest\ExportWordTest.csproj
?????文件????????2032??2018-11-11?14:30??ExportWordTest\Form1.Designer.cs
?????文件????????8130??2018-11-11?17:06??ExportWordTest\Form1.cs
?????文件????????5817??2018-11-11?14:30??ExportWordTest\Form1.resx
?????文件?????????495??2018-11-11?14:26??ExportWordTest\Program.cs
?????目錄???????????0??2018-11-11?14:26??ExportWordTest\Properties\
?????文件????????1378??2018-11-11?14:26??ExportWordTest\Properties\AssemblyInfo.cs
?????文件????????2880??2018-11-11?14:26??ExportWordTest\Properties\Resources.Designer.cs
?????文件????????5612??2018-11-11?14:26??ExportWordTest\Properties\Resources.resx
?????文件????????1101??2018-11-11?14:26??ExportWordTest\Properties\Settings.Designer.cs
?????文件?????????249??2018-11-11?14:26??ExportWordTest\Properties\Settings.settings
?????目錄???????????0??2018-11-11?15:41??ExportWordTest\bin\
?????目錄???????????0??2018-11-12?08:11??ExportWordTest\bin\Debug\
?????文件???????12288??2018-11-12?08:11??ExportWordTest\bin\Debug\ExportWordTest.exe
?????文件???????22016??2018-11-12?08:11??ExportWordTest\bin\Debug\ExportWordTest.pdb
?????文件???????11600??2018-11-11?17:06??ExportWordTest\bin\Debug\ExportWordTest.vshost.exe
?????文件?????????490??2010-03-17?22:39??ExportWordTest\bin\Debug\ExportWordTest.vshost.exe.manifest
?????文件???????26789??2018-11-11?15:15??ExportWordTest\bin\Debug\shili.jpg
?????文件???????26789??2018-11-11?15:15??ExportWordTest\bin\Debug\shili1.jpg
?????文件???????40123??2018-11-11?17:06??ExportWordTest\bin\Debug\測(cè)試.docx
?????文件???????39855??2018-11-11?15:40??ExportWordTest\bin\Debug測(cè)試.docx
?????目錄???????????0??2018-11-11?14:26??ExportWordTest\obj\
?????目錄???????????0??2018-11-11?14:26??ExportWordTest\obj\x86\
?????目錄???????????0??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\
?????文件????????2867??2018-11-11?14:26??ExportWordTest\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6364??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????180??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\ExportWordTest.Form1.resources
?????文件?????????180??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\ExportWordTest.Properties.Resources.resources
?????文件?????????547??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\ExportWordTest.csproj.FileListAbsolute.txt
?????文件?????????975??2018-11-12?08:11??ExportWordTest\obj\x86\Debug\ExportWordTest.csproj.GenerateResource.Cache
............此處省略3個(gè)文件信息
評(píng)論
共有 條評(píng)論