• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#实现通过模板自动创建Word文档的方法

C#实现通过模板自动创建Word文档的方法

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#api帮助文档,c#帮助文档,c#帮助文档chm下载,c#文档,c#msdn帮助文档等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧。分享给大家供大家参考。具体实现方法如下:

引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决!

现通过具体的示例演示具体的步骤:
 
第一步,制作模板
 
1.新建一个文档,设置文档内容。
2.在相应位置插入书签;将鼠标定位到要插入书签的位置,点击“插入”>“书签”,弹出对话框,输入书签名,点击“添加”按钮。
3.保存模板,命名为“模板1.dot”或者“模板1.doc”

第二步,设置项目中的引用

1.右击“解决方案资源管理器”中的项目目录下的“引用”,选择“添加引用”,打开“添加引用”对话框
2.在“添加引用”对话框中,选择“COM”>“Microsoft Word 11.0 Object Library”,点击“确定”按钮
3.相同操作打开“添加引用”对话框中,选择“浏览”项,查找到”Microsoft.Office.Interop.Word.dll”文件,选中它,点击“确定”按钮
 
注意:此处要查找的“Microsoft.Office.Interop.Word.dll”版本必须为“11.*.*.*”,“*”代表数字

第三步,编码

这一步分成两个部分
第一部分,Report类的编码
这部分我已经封装好,为文件“Report.cs”,可以直接使用

具体实现代码如下:(代码中有比较详细的注释)

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
 
namespace MYNAMESPACE //这边需要换成自己的命名空间名
{
  classReport
  {
    private_ApplicationwordApp= null;
    private_DocumentwordDoc= null;
    public_ApplicationApplication
    {
      get
      {
        return wordApp;
      }
      set
      {
        wordApp = value;
      }
    }
    public_DocumentDocument
    {
      get
      {
        return wordDoc;
      }
      set
      {
        wordDoc = value;
      }
    }
 
    //通过模板创建新文档
    publicvoidCreateNewDocument(stringfilePath)
    {
      killWinWordProcess();
      wordApp= new ApplicationClass();
      wordApp.DisplayAlerts =WdAlertLevel.wdAlertsNone;
      wordApp.Visible =false;
      objectmissing =System.Reflection.Missing.Value;
      objecttemplateName =filePath;
      wordDoc= wordApp.Documents.Open(reftemplateName, refmissing,
        ref missing, ref missing,ref missing, ref missing, refmissing,
        ref missing, ref missing,ref missing, ref missing, refmissing,
        ref missing, ref missing,ref missing, ref missing);
    }
 
    //保存新文件
    publicvoidSaveDocument(stringfilePath)
    {
      objectfileName =filePath;
      objectformat =WdSaveFormat.wdFormatDocument;//保存格式
      objectmiss =System.Reflection.Missing.Value;
      wordDoc.SaveAs(reffileName, ref format, ref miss,
        ref miss, ref miss,ref miss, ref miss,
        ref miss, ref miss,ref miss, ref miss,
        ref miss, ref miss,ref miss, ref miss,
        ref miss);
      //关闭wordDoc,wordApp对象
      objectSaveChanges =WdSaveOptions.wdSaveChanges;
      objectOriginalFormat =WdOriginalFormat.wdOriginalDocumentFormat;
      objectRouteDocument =false;
      wordDoc.Close(refSaveChanges, refOriginalFormat, refRouteDocument);
      wordApp.Quit(refSaveChanges, refOriginalFormat, refRouteDocument);
    }
 
    //在书签处插入值
    publicboolInsertValue(stringbookmark, stringvalue)
    {
      objectbkObj =bookmark;
      if(wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
      {
        wordApp.ActiveDocument.Bookmarks.get_Item(refbkObj).Select();
        wordApp.Selection.TypeText(value);
        return true;
      }
      returnfalse;
    }
 
    //插入表格,bookmark书签
    publicTableInsertTable(stringbookmark, int rows, int columns,float width)
    {
      objectmiss =System.Reflection.Missing.Value;
      objectoStart =bookmark;
      Rangerange =wordDoc.Bookmarks.get_Item(refoStart).Range;//表格插入位置
      TablenewTable =wordDoc.Tables.Add(range,rows, columns, ref miss, refmiss);
      //设置表的格式
      newTable.Borders.Enable =1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过)
      newTable.Borders.OutsideLineWidth=WdLineWidth.wdLineWidth050pt;//边框宽度
      if(width != 0)
      {
        newTable.PreferredWidth=width;//表格宽度
      }
      newTable.AllowPageBreaks =false;
      returnnewTable;
    }
 
    //合并单元格 表名,开始行号,开始列号,结束行号,结束列号
    publicvoidMergeCell(Microsoft.Office.Interop.Word.Tabletable, int row1, int column1,int row2, int column2)
    {
      table.Cell(row1,column1).Merge(table.Cell(row2,column2));
    }
 
    //设置表格内容对齐方式Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1)
    publicvoidSetParagraph_Table(Microsoft.Office.Interop.Word.Tabletable, int Align, int Vertical)
    {
      switch(Align)
      {
        case -1:table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphLeft;break;//左对齐
        case 0: table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphCenter;break;//水平居中
        case 1: table.Range.ParagraphFormat.Alignment=WdParagraphAlignment.wdAlignParagraphRight;break;//右对齐
      }
      switch(Vertical)
      {
        case -1: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalTop;break;//顶端对齐
        case 0: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalCenter;break;//垂直居中
        case 1: table.Range.Cells.VerticalAlignment=WdCellVerticalAlignment.wdCellAlignVerticalBottom;break;//底端对齐
      }
    }
 
    //设置表格字体
    publicvoidSetFont_Table(Microsoft.Office.Interop.Word.Tabletable, string fontName, double size)
    {
      if(size != 0)
      {
        table.Range.Font.Size =Convert.ToSingle(size);
      }
      if(fontName !="")
      {
        table.Range.Font.Name =fontName;
      }
    }
 
    //是否使用边框,n表格的序号,use是或否
    publicvoidUseBorder(int n,bool use)
    {
      if(use)
      {
        wordDoc.Content.Tables[n].Borders.Enable =1; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
      }
      else
      {
        wordDoc.Content.Tables[n].Borders.Enable =2; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
      }
    }
 
    //给表格插入一行,n表格的序号从1开始记
    publicvoidAddRow(int n)
    {
      objectmiss =System.Reflection.Missing.Value;
      wordDoc.Content.Tables[n].Rows.Add(refmiss);
    }
 
    //给表格添加一行
    publicvoidAddRow(Microsoft.Office.Interop.Word.Tabletable)
    {
      objectmiss =System.Reflection.Missing.Value;
      table.Rows.Add(refmiss);
    }
 
    //给表格插入rows行,n为表格的序号
    publicvoidAddRow(int n, int rows)
    {
      objectmiss =System.Reflection.Missing.Value;
      Microsoft.Office.Interop.Word.Tabletable = wordDoc.Content.Tables[n];
      for(inti = 0; i < rows; i++)
      {
        table.Rows.Add(refmiss);
      }
    }
 
    //给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
    publicvoidInsertCell(Microsoft.Office.Interop.Word.Tabletable, int row, int column,string value)
    {
      table.Cell(row,column).Range.Text =value;
    }
 
    //给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
    publicvoidInsertCell(int n, int row,int column, string value)
    {
      wordDoc.Content.Tables[n].Cell(row,column).Range.Text =value;
    }
 
    //给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值
    publicvoidInsertCell(int n, int row,int columns, string[] values)
    {
      Microsoft.Office.Interop.Word.Tabletable = wordDoc.Content.Tables[n];
      for(in



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
  • C#获取USB事件API实例分析
  • C#实现通过模板自动创建Word文档的方法
  • c#文档图片自动纠偏
  • c#不使用系统api实现可以指定区域屏幕截屏功能
  • c#不使用windows api函数打开我的电脑和获取电脑驱动器信息
  • C#中通过API实现的打印类 实例代码

相关文章

  • 2017-05-28C#实现通过winmm.dll控制声音播放的方法
  • 2017-05-28C#如何实现图片的剪裁并保存
  • 2017-05-28C#中的Linq to Xml详解
  • 2017-05-28c#中@的3种作用
  • 2017-05-28C#利用Random得随机数求均值、方差、正态分布的方法
  • 2017-05-28C#连接mysql数据库完整实例
  • 2017-05-28LZW压缩算法 C#源码
  • 2017-05-28C#判断一个矩阵是否为对称矩阵及反称矩阵的方法
  • 2017-05-28C#操作SQLite方法实例详解
  • 2017-05-28C#实现顺序表(线性表)完整实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 三十分钟快速掌握C# 6.0知识点
    • C#中调用命令行cmd开启wifi热点的实例代码
    • C#实现随机数产生类实例
    • C#遍历集合与移除元素的方法
    • vista和win7在windows服务中交互桌面权限问题解决方法:穿透Session 0 隔离
    • C#判断数据类型的简单示例代码
    • C#中使用ADOMD.NET查询多维数据集的实现方法
    • C#实现自定义FTP操作封装类实例
    • C#数字图像处理之图像缩放的方法
    • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有