版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、如何用C#®程實(shí)現(xiàn)動(dòng)態(tài)生成Word文檔并填充數(shù)據(jù)的效果呢?要使用C斛作word,首先要添加引用:I 、添加引用->COM->MicrosoftWordII ."0ObjectLibrary2、在.cs文件中添加usingWord;下面的例子中包括C#寸Word文檔的創(chuàng)建、插入表格、設(shè)置樣式等操作:(例子中代碼有些涉及數(shù)據(jù)信息部分被省略,重要是介紹一些C#!Mword文檔的方法)publicstringCreateWordFile(stringCheckedInfo).stringmessage=""try.ObjectNothing=Syst
2、em.Reflection.Missing.Value;Directory.CreateDirectory("C:/CNSI");/創(chuàng)建文件所在目錄stringname="CNSI_"+DateTime.Now.ToShortString()+".doc"objectfilename="C:/CNSI/"+name;/文件保存路徑/創(chuàng)建Word文檔Word.ApplicationWordApp=newWord.ApplicationClass();Word.DocumentWordDoc=WordApp.Docum
3、ents.Add(refNothing,refNothing,refNothing,refNothing);/添加頁眉WordApp.ActiveWindow.View.Type=WdViewType.wdOutlineView;WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekPrimaryHeader;WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("頁眉內(nèi)容");WordApp.Selection.ParagraphFormat.Alignment=Wor
4、d.WdParagraphAlignment.wdAlignParagraphRight;/設(shè)置右對(duì)齊WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekMainDocument;/跳出頁眉設(shè)置WordApp.Selection.ParagraphFormat.LineSpacing=15f;/設(shè)置文檔的行間距/移動(dòng)焦點(diǎn)并換行objectcount=14;objectWdLine=Word.WdUnits.wdLine;/換一行;WordApp.Selection.MoveDown(refWdLine,refcount,refNothing)
5、;/移動(dòng)焦點(diǎn)WordApp.Selection.TypeParagraph();/插入段落/文檔中創(chuàng)建表格Word.TablenewTable=WordDoc.Tables.Add(WordApp.Selection.Range,12,3,refNothing,refNothing);/設(shè)置表格樣式newTable.Borders.OutsideLineStyle=Word.WdLineStyle.wdLineStyleThickThinLargeGap;newTable.Borders.InsideLineStyle=Word.WdLineStyle.wdLineStyleSingle;ne
6、wTable.Columns1.Width=100f;newTable.Columns2.Width=220f;newTable.Columns3.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=Word.WdCellVerticalAlig
7、nment.wdCellAlignVerticalCenter;/垂直居中WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;/水平居中/填充表格內(nèi)容newTable.Cell(2,1).Range.Text="產(chǎn)品基本信息"newTable.Cell(2,1).Range.Font.Color=Word.WdColor.wdColorDarkBlue;/設(shè)置單元格內(nèi)字體顏色/合并單元格newTable.Cell(2,1).Merge(new
8、Table.Cell(2,3);WordApp.Selection.Cells.VerticalAlignment=Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;/填充表格內(nèi)容newTable.Cell(3,1).Range.Text="品牌名稱:"newTable.Cell(3,2).Range.Text=BrandName;/縱向合并單元格newTable.Cell(3,3).Select();/選中一行objectmoveUnit=Word.WdUnits.wdLine;objectmoveCount=5
9、;objectmoveExtend=Word.WdMovementType.wdExtend;WordApp.Selection.MoveDown(refmoveUnit,refmoveCount,refmoveExtend);WordApp.Selection.Cells.Merge();/插入圖片stringFileName=Picture;/圖片所在路徑objectLinkToFile=false;objectSaveWithDocument=true;objectAnchor=WordDoc.Application.Selection.Range;WordDoc.Application
10、.ActiveDocument.InlineShapes.AddPicture(FileName,refLinkToFile,refSaveWithDocument,refAnchor);WordDoc.Application.ActiveDocument.InlineShapes1.Width=100f;/圖片寬度WordDoc.Application.ActiveDocument.InlineShapes1.Height=100f;/圖片高度/將圖片設(shè)置為四周環(huán)繞型Word.Shapes=WordDoc.Application.ActiveDocument.InlineShapes1.Co
11、nvertToShape();s.WrapFormat.Type=Word.WdWrapType.wdWrapSquare;newTable.Cell(12,1).Range.Text="產(chǎn)品特殊屬性"newTable.Cell(12,1).Merge(newTable.Cell(12,3);/在表格中增加行WordDoc.Content.Tables1.Rows.Add(refNothing);WordDoc.Paragraphs.Last.Range.Text="文檔創(chuàng)建時(shí)間:"+DateTime.Now.ToString();/落款”“WordDo
12、c.Paragraphs.Last.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;/文件保存WordDoc.SaveAs(reffilename,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);WordDoc.Close(refNothing,r
13、efNothing,refNothing);WordApp.Quit(refNothing,refNothing,refNothing);message=name+"文檔生成成功,以保存到C:CNSI下”;catch.message="文件導(dǎo)出異常!"returnmessage;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usi
14、ngSystem.Windows.Forms;usingWord;usingSystem.IO;namespaceWindowsForms_WordpublicpartialclassForm1:FormpublicForm1()InitializeComponent();privatevoidForm1_Load(objectsender,EventArgse)publicstringCreateWordFile(stringChenkedInfo)stringmessage=""tryobjectNothing=System.Reflection.Missing.Val
15、ue;/創(chuàng)建文件目錄Directory.CreateDirectory("d:/Test");stringname="Test_"+DateTime.Now.ToShortDateString()+".doc"/收件保存路徑objectfilename="d:/Test/"+name;/創(chuàng)建word文檔Word.ApplicationWordApp=newWord.ApplicationClass();Word.DocumentWordDoc=WordApp.Documents.Add(refNothing,ref
16、Nothing,refNothing,refNothing);/添加頁眉WordApp.ActiveWindow.View.Type=WdViewType.wdOutlineView;WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekCurrentPageHeader;WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("頁眉內(nèi)容");/設(shè)置右對(duì)齊WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraph
17、Alignment.wdAlignParagraphRight;/跳出頁眉設(shè)置WordApp.ActiveWindow.View.SeekView=WdSeekView.wdSeekMainDocument;/股置文檔的行間距WordApp.Selection.ParagraphFormat.LineSpacing=15f;/移動(dòng)焦點(diǎn)并換行objectcount=14;objectWdLine=Word.WdUnits.wdLine;/換一行WordApp.Selection.MoveDown(refWdLine,refcount,refNothing);/移動(dòng)焦點(diǎn)WordApp.Select
18、ion.TypeParagraph();/fif入段落/文檔中創(chuàng)建表格Word.TablenewTable=WordDoc.Tables.Add(WordApp.Selection.Range,12,3,refNothing,refNothing);/設(shè)置表格樣式newTable.Borders.OutsideLineStyle=Word.WdLineStyle.wdLineStyleThickThinMedGap;newTable.Borders.InsideLineStyle=Word.WdLineStyle.wdLineStyleSingle;newTable.Columns1.Widt
19、h=100f;newTable.Columns2.Width=220f;newTable.Columns3.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=Word.WdCellVerticalAlignment.wdCellAlignVer
20、ticalCenter;垂直居中WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;水平居中/填充表格內(nèi)容newTable.Cell(2,1).Range.Text=產(chǎn)品基本信息""newTable.Cell(2,1).Range.Font.Color=Word.WdColor.wdColorDarkBlue;設(shè)置單元格內(nèi)字體顏色/合并單元格newTable.Cell(2,1).Merge(newTable.Cell(2,3);WordApp
21、.Selection.Cells.VerticalAlignment=Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;/填充表格內(nèi)容newTable.Cell(3,1).Range.Text=品牌名稱:"H.newTable.Cell(3,2).Range.Text="BrandName"/?/縱向合并單元格newTable.Cell(3,3).Select();/娃中一行objectmoveUnit=Word.WdUnits.wdLine;objectmoveCount=5;objectmoveExt
22、end=Word.WdMovementType.wdExtend;WordApp.Selection.MoveDown(refmoveUnit,refmoveCount,refmoveExtend);WordApp.Selection.Cells.Merge();/插入圖片stringFileName="d:/"/圖片所在路徑objectLinkToFile=false;objectSaveWithDocument=true;objectAnchor=WordDoc.Application.Selection.Range;WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName,refLinkToFile,refSaveWithDocument,refAnchor);WordDoc.Application.ActiveDocument.InlineShapes1.Width=100f;圖片高度/將圖片設(shè)置為四周環(huán)繞型Word.Shapes=WordDoc.Application.ActiveDocument.InlineShapes1.ConvertToShape();s.WrapFormat.Type=Word.WdWrapType.wdWrapSquare;newTable
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年1月南京市雨花臺(tái)區(qū)所屬單位公開招聘編外教師53人筆試備考題庫及答案解析
- 2026年池州現(xiàn)代報(bào)業(yè)出版發(fā)行有限公司公開招聘印刷操作工1名考試備考題庫及答案解析
- 2026年上半年合肥高新區(qū)管委會(huì)公開招聘工作人員45名筆試備考試題及答案解析
- 2026年度馬鞍山市博望區(qū)事業(yè)單位公開招聘工作人員21名考試備考試題及答案解析
- 2026天津市中心婦產(chǎn)科醫(yī)院招錄專職總會(huì)計(jì)師1人考試備考題庫及答案解析
- 2026年甘肅水文地質(zhì)工程地質(zhì)勘察院有限責(zé)任公司面向社會(huì)招聘18人筆試備考試題及答案解析
- 2026年風(fēng)力發(fā)電場布局的流體力學(xué)分析
- 2026年《商務(wù)工作成長與藍(lán)色扁平化啟示》
- 2025年濰坊體育單招學(xué)校筆試及答案
- 2025年教師事業(yè)編無筆試及答案
- 西南交通大學(xué)本科畢業(yè)設(shè)計(jì)(論文)撰寫規(guī)范
- 七上歷史期中??夹≌撐挠^點(diǎn)+范文
- 2025年高中語文必修上冊(cè)《赤壁賦》文言文對(duì)比閱讀訓(xùn)練含答案
- DB31-T 977-2023 戶外招牌設(shè)置技術(shù)規(guī)范
- 國家安全生產(chǎn)十五五規(guī)劃
- 醫(yī)院培訓(xùn)課件:《醫(yī)務(wù)人員不良執(zhí)業(yè)行為記分管理辦法》
- 電力施工流程七步驟電力
- 內(nèi)校員培訓(xùn)課件
- 污水處理廠設(shè)備安裝與調(diào)試方案
- 物體打擊事故培訓(xùn)課件
- 豬場產(chǎn)房技術(shù)員述職報(bào)告
評(píng)論
0/150
提交評(píng)論