資源簡(jiǎn)介
c# 操作word 插入 文字 表格 圖片 并轉(zhuǎn)為pdf 完整實(shí)例

代碼片段和文件信息
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;
using?System.IO;
using?System.Reflection;
using?Microsoft.Office.Interop.Word;
namespace?WordTest
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????object?path;???????????????????????????//文件路徑變量
????????????????string?strContent;?????????????????????//文本內(nèi)容變量
????????????????Microsoft.Office.Interop.Word.ApplicationClass?wordApp;???????????????//Word應(yīng)用程序變量
????????????????Microsoft.Office.Interop.Word.DocumentClass?wordDoc;??????????????//Word文檔變量
????????????????string?application_path?=?System.Windows.Forms.Application.StartupPath;
????????????????path?=?application_path?+?@“/MyWord.doc“;??????????????//路徑
????????????????wordApp?=?new?Microsoft.Office.Interop.Word.ApplicationClass();??//初始化
????????????????//如果已存在,則刪除
????????????????while?(File.Exists((string)path))
????????????????{
????????????????????try
????????????????????{
????????????????????????File.Delete((string)path);
????????????????????}
????????????????????catch
????????????????????{
????????????????????????MessageBox.Show(“文件正在被使用,請(qǐng)先關(guān)閉“);
????????????????????}
????????????????}
????????????????//由于使用的是COM庫(kù),因此有許多變量需要用Missing.Value代替
????????????????object?Nothing?=?Missing.Value;//創(chuàng)建文檔
????????????????wordDoc?=?(Microsoft.Office.Interop.Word.DocumentClass)wordApp.Documents.Add(ref?Nothing?ref?Nothing?ref?Nothing?ref?Nothing);
????????????????//??////////////////////////////////
????????????????//Insert?a?paragraph?at?the?beginning?of?the?document
????????????????Microsoft.Office.Interop.Word.Paragraph?myparagraph1;
????????????????myparagraph1?=?wordDoc.Content.Paragraphs.Add(ref?Nothing);
????????????????myparagraph1.Range.Text?=?“heading1\r\r\r123“;?//換3行顯示123
????????????????myparagraph1.Range.Font.Bold?=?1;
????????????????myparagraph1.Format.SpaceAfter?=?24;?//距離下一段的下邊距(margin-bottom單位是pt)
????????????????myparagraph1.Range.InsertParagraphAfter();
????????????????
????????????????//Insert?a?paragraph?at?the?end?of?the?document.
????????????????object?oEndOfDoc?=?“\\endofdoc“;/*預(yù)置書(shū)簽表示文檔結(jié)尾原文有錯(cuò)誤反斜杠寫(xiě)成了//正斜杠導(dǎo)致下面的myrange有問(wèn)題*/
????????????????Microsoft.Office.Interop.Word.Paragraph?mp2;
????????????????object?myrange?=?wordDoc.Bookmarks.get_Item(ref?oEndOfDoc).Range;
????????????????mp2?=?wordDoc.Content.Paragraphs.Add(ref?myrange);
????????????????mp2.Range.Text?=?“head2居中?愛(ài)的瘋狂了撒嬌發(fā)鏈接撒的發(fā)鏈接拉斯科技大返利卡數(shù)據(jù)庫(kù)來(lái)訪記錄卡升級(jí)到付款鏈接撒開(kāi)了房間盧卡斯剪短發(fā)了卡薩京東方“;
????????????????mp2.Alignment?=?WdParagraphAlignment.wdAlignParagraphCenter;?//段落居中
????????????????mp2.Format.SpaceAfter?=?6;
????????????????mp2.Range.Inse
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????144??2014-10-21?16:11??WordTest\app.config
?????文件????8450048??2012-11-25?19:37??WordTest\bin\Debug\Aspose.Words.dll
?????文件??????31744??2014-10-21?16:24??WordTest\bin\Debug\MyWord.doc
?????文件??????50627??2014-10-21?16:24??WordTest\bin\Debug\MyWord.pdf
?????文件??????12288??2014-10-21?16:30??WordTest\bin\Debug\WordTest.exe
?????文件????????144??2014-10-21?16:11??WordTest\bin\Debug\WordTest.exe.config
?????文件??????28160??2014-10-21?16:30??WordTest\bin\Debug\WordTest.pdb
?????文件??????11600??2014-10-21?16:25??WordTest\bin\Debug\WordTest.vshost.exe
?????文件????????144??2014-10-21?16:11??WordTest\bin\Debug\WordTest.vshost.exe.config
?????文件????????490??2012-06-06?02:06??WordTest\bin\Debug\WordTest.vshost.exe.manifest
?????文件??????32256??2014-10-21?15:02??WordTest\bin\Debug\裴多菲啊.doc
?????文件??????13224??2014-10-21?16:30??WordTest\Form1.cs
?????文件???????2107??2014-10-21?16:26??WordTest\Form1.Designer.cs
?????文件???????5817??2014-10-21?16:26??WordTest\Form1.resx
?????文件????8450048??2012-11-25?19:37??WordTest\Libs\Aspose.Words.dll
?????文件????3714871??2014-10-21?16:23??WordTest\Libs\Aspose.Words_V11.9.0.0.zip
?????文件????????863??2014-10-21?16:11??WordTest\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6683??2014-10-21?16:30??WordTest\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????4608??2014-10-21?16:11??WordTest\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件???????1244??2014-10-21?16:25??WordTest\obj\x86\Debug\WordTest.csproj.FileListAbsolute.txt
?????文件????????975??2014-10-21?16:26??WordTest\obj\x86\Debug\WordTest.csproj.GenerateResource.Cache
?????文件????????935??2014-10-21?16:10??WordTest\obj\x86\Debug\WordTest.csproj.ResolveComReference.cache
?????文件???????5917??2014-10-21?16:30??WordTest\obj\x86\Debug\WordTest.csprojResolveAssemblyReference.cache
?????文件??????12288??2014-10-21?16:30??WordTest\obj\x86\Debug\WordTest.exe
?????文件????????180??2014-10-21?16:26??WordTest\obj\x86\Debug\WordTest.Form1.resources
?????文件??????28160??2014-10-21?16:30??WordTest\obj\x86\Debug\WordTest.pdb
?????文件????????180??2014-10-21?16:24??WordTest\obj\x86\Debug\WordTest.Properties.Resources.resources
?????文件????????489??2014-10-20?16:03??WordTest\Program.cs
?????文件???????1366??2014-10-20?16:03??WordTest\Properties\AssemblyInfo.cs
?????文件???????2856??2014-10-21?16:11??WordTest\Properties\Resources.Designer.cs
............此處省略17個(gè)文件信息
評(píng)論
共有 條評(píng)論