• 大小: 1.14M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-26
  • 语言: C#
  • 标签: doc  表格  OC  文档  

资源简介

基于Microsoft.Office.Interop.Word实现

资源截图

代码片段和文件信息

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;//文档内容
            MSword.Application wordApp;//word应用程序变量
            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);
            //创建表格,默认表格是没有边框的
            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#代码实现word文档内容写入!!!!\n“;
            wordDoc.Paragraphs.Last.Range.Text = strContent;
            //普通源码
            strContent = “写入普通文本\n“;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容

            //写入黑体
            strContent = “写入黑体文本\n“;
            wordDoc.Paragraphs.Last.Range.Font.Name = “黑体“;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容
            //写入加粗

            strContent = “写入加粗文本\n“;
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容

            //写入斜体

            strContent = “写入斜体文本\n“;
            wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容

            //写入15

            strContent = “写入15号字体文本\n“;
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容

            //写入下划线

            strContent = “写入下划线文本\n“;
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSword.WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Text = strContent;// wordDoc.Paragraphs.Last.Range.Text 内容



            //写入蓝色

            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  创建word文档+表格\
     目录           0  2020-11-25 17:53  创建word文档+表格\CreateWordDemo\
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\.vs\
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\.vs\CreateWordDemo\
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\.vs\CreateWordDemo\v16\
     文件       41472  2020-11-25 17:53  创建word文档+表格\CreateWordDemo\.vs\CreateWordDemo\v16\.suo
     目录           0  2020-11-25 17:54  创建word文档+表格\CreateWordDemo\Connected Services\
     文件        3918  2020-11-25 16:57  创建word文档+表格\CreateWordDemo\CreateWordDemo.csproj
     文件         482  2020-11-25 16:57  创建word文档+表格\CreateWordDemo\CreateWordDemo.csproj.user
     文件        1133  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\CreateWordDemo.sln
     文件        4411  2020-11-25 17:53  创建word文档+表格\CreateWordDemo\Program.cs
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\Properties\
     文件        1320  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\Properties\AssemblyInfo.cs
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\bin\
     目录           0  2020-11-25 16:57  创建word文档+表格\CreateWordDemo\bin\Debug\
     文件        6656  2020-11-25 17:47  创建word文档+表格\CreateWordDemo\bin\Debug\CreateWordDemo.exe
     文件       22016  2020-11-25 17:47  创建word文档+表格\CreateWordDemo\bin\Debug\CreateWordDemo.pdb
     文件      949992  2016-03-10 16:19  创建word文档+表格\CreateWordDemo\bin\Debug\Microsoft.Office.Interop.Word.dll
     目录           0  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\obj\
     目录           0  2020-11-25 17:46  创建word文档+表格\CreateWordDemo\obj\Debug\
     文件         208  2020-11-25 16:25  创建word文档+表格\CreateWordDemo\obj\Debug\.NETframeworkVersion=v4.0.AssemblyAttributes.cs
     文件           0  2020-11-25 17:47  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.CopyComplete
     文件          42  2020-11-25 16:52  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.CoreCompileInputs.cache
     文件         756  2020-11-25 16:57  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csproj.FileListAbsolute.txt
     文件         424  2020-11-25 17:46  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.csprojAssemblyReference.cache
     文件        6656  2020-11-25 17:47  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.exe
     文件       22016  2020-11-25 17:47  创建word文档+表格\CreateWordDemo\obj\Debug\CreateWordDemo.pdb
     文件         675  2020-11-25 16:56  创建word文档+表格\CreateWordDemo\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6824  2020-11-25 16:57  创建word文档+表格\CreateWordDemo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2020-11-25 17:54  创建word文档+表格\CreateWordDemo\obj\Debug\TempPE\
     目录           0  2020-11-25 16:30  创建word文档+表格\CreateWordDemo\packages\
............此处省略13个文件信息

评论

共有 条评论