資源簡介
基于Microsoft.Office.Interop.Word實(shí)現(xiàn)
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?MSword?=?Microsoft.Office.Interop.Word;
using?System.IO;
using?System.Reflection;
namespace?CreateWordDemo
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????object?path;//路徑變量
????????????string?strContent;//文檔內(nèi)容
????????????MSword.Application?wordApp;//word應(yīng)用程序變量
????????????MSword.Document?wordDoc;//word文檔變量
????????????path?=?@“D:\Mywaord.doc“;
????????????wordApp?=?new?MSword.ApplicationClass();//初始化
????????????
????????????if?(File.Exists((string)path))//(string)path屬于拆箱
????????????{
????????????????File.Delete((string)path);
????????????}
????????????//由于使用的是com庫,因此有許多變量要用到missing.value
????????????object?Nothing?=?Missing.Value;
????????????wordDoc?=?wordApp.Documents.Add(ref?Nothing?ref?Nothing?ref?Nothing?ref?Nothing);
????????????//創(chuàng)建表格,默認(rèn)表格是沒有邊框的
????????????MSword.Table?table?=?wordDoc.Tables.Add(wordApp.Selection.Range?5?5?ref?Nothing?ref?Nothing);
????????????table.Borders.Enable?=?1;
????????????for?(int?i?=?1;?i?<=?5;?i++)
????????????{
????????????????for?(int?j?=?1;?j?<=?5;?j++)
????????????????{
????????????????????table.Cell(i?j).Range.Text?=?“第“?+?i?+?“行,第“?+?j?+?“列“;
????????????????}
????????????}
????????????strContent?=?“利用C#代碼實(shí)現(xiàn)word文檔內(nèi)容寫入!!!!\n“;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;
????????????//普通源碼
????????????strContent?=?“寫入普通文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入黑體
????????????strContent?=?“寫入黑體文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Name?=?“黑體“;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入加粗
????????????strContent?=?“寫入加粗文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Bold?=?1;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入斜體
????????????strContent?=?“寫入斜體文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Italic?=?1;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入15
????????????strContent?=?“寫入15號字體文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Size?=?15;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入下劃線
????????????strContent?=?“寫入下劃線文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Underline?=?MSword.WdUnderline.wdUnderlineThick;
????????????wordDoc.Paragraphs.Last.Range.Text?=?strContent;//?wordDoc.Paragraphs.Last.Range.Text?內(nèi)容
????????????//寫入藍(lán)色
????????????strContent?=?“寫入下劃線加顏色文本\n“;
????????????wordDoc.Paragraphs.Last.Range.Font.Underline?=?MSword.WdUnderline.wdUnderlineThick;
????????????wordDoc.Paragraphs.Last.Range.Font.UnderlineColor?=?MSword.WdColor.wdC
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\
?????目錄???????????0??2020-11-25?17:53??創(chuàng)建word文檔+表格\CreateWordDemo\
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\.vs\
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\.vs\CreateWordDemo\
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\.vs\CreateWordDemo\v16\
?????文件???????41472??2020-11-25?17:53??創(chuàng)建word文檔+表格\CreateWordDemo\.vs\CreateWordDemo\v16\.suo
?????目錄???????????0??2020-11-25?17:54??創(chuàng)建word文檔+表格\CreateWordDemo\Connected?Services\
?????文件????????3918??2020-11-25?16:57??創(chuàng)建word文檔+表格\CreateWordDemo\CreateWordDemo.csproj
?????文件?????????482??2020-11-25?16:57??創(chuàng)建word文檔+表格\CreateWordDemo\CreateWordDemo.csproj.user
?????文件????????1133??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\CreateWordDemo.sln
?????文件????????4411??2020-11-25?17:53??創(chuàng)建word文檔+表格\CreateWordDemo\Program.cs
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\Properties\
?????文件????????1320??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\Properties\AssemblyInfo.cs
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\bin\
?????目錄???????????0??2020-11-25?16:57??創(chuàng)建word文檔+表格\CreateWordDemo\bin\Debug\
?????文件????????6656??2020-11-25?17:47??創(chuàng)建word文檔+表格\CreateWordDemo\bin\Debug\CreateWordDemo.exe
?????文件???????22016??2020-11-25?17:47??創(chuàng)建word文檔+表格\CreateWordDemo\bin\Debug\CreateWordDemo.pdb
?????文件??????949992??2016-03-10?16:19??創(chuàng)建word文檔+表格\CreateWordDemo\bin\Debug\Microsoft.Office.Interop.Word.dll
?????目錄???????????0??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\obj\
?????目錄???????????0??2020-11-25?17:46??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\
?????文件?????????208??2020-11-25?16:25??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\.NETfr
?????文件???????????0??2020-11-25?17:47??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.CopyComplete
?????文件??????????42??2020-11-25?16:52??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.CoreCompileInputs.cache
?????文件?????????756??2020-11-25?16:57??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.FileListAbsolute.txt
?????文件?????????424??2020-11-25?17:46??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csprojAssemblyReference.cache
?????文件????????6656??2020-11-25?17:47??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.exe
?????文件???????22016??2020-11-25?17:47??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\CreateWordDemo.pdb
?????文件?????????675??2020-11-25?16:56??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6824??2020-11-25?16:57??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????目錄???????????0??2020-11-25?17:54??創(chuàng)建word文檔+表格\CreateWordDemo\obj\Debug\TempPE\
?????目錄???????????0??2020-11-25?16:30??創(chuàng)建word文檔+表格\CreateWordDemo\packages\
............此處省略13個文件信息
評論
共有 條評論