Asp.Net 操作word 第二篇[推荐]

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

现通过具体的示例演示具体的步骤:

第一步,制作模板

1,新建一个文档,文档内容如下:

图1

2,在相应位置插入书签;将鼠标定位到要插入书签的位置,点击“插入”>“书签”,弹出对话框,输入书签名,点击“添加”按钮,书签位置如图3所示

图2

图3

3,保存模板,命名为“模板1.dot”或者“模板1.doc”

图4

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

1,右击“解决方案资源管理器”中的项目目录下的“引用”,选择“添加引用”,打开“添加引用”对话框

图5

2,在“添加引用”对话框中,选择“COM”>“Microsoft Word 11.0 Object Library”,点击“确定”按钮

图6

3,相同操作打开“添加引用”对话框中,选择“浏览”项,查找到”Microsoft.Office.Interop.Word.dll”文件,选中它,点击“确定”按钮

图7

注意:此处要查找的“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 //这边需要换成自己的命名空间名
{

    class Report
    {

        private _Application wordApp = null;

        private _Document wordDoc = null;

        public _Application Application
        {

            get
            {

                return wordApp;

            }

            set
            {

                wordApp = value;

            }

        }

        public _Document Document
        {

            get
            {

                return wordDoc;

            }

            set
            {

                wordDoc = value;

            }

        }

        //通过模板创建新文档

        public void CreateNewDocument(string filePath)
        {

            killWinWordProcess();

            wordApp = new ApplicationClass();

            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            wordApp.Visible = false;

            object missing = System.Reflection.Missing.Value;

            object templateName = filePath;

            wordDoc = wordApp.Documents.Open(ref templateName, ref missing,

            ref missing, ref missing, ref missing, ref missing, ref missing,

            ref missing, ref missing, ref missing, ref missing, ref missing,

            ref missing, ref missing, ref missing, ref missing);

        }

        //保存新文件

        public void SaveDocument(string filePath)
        {

            object fileName = filePath;

            object format = WdSaveFormat.wdFormatDocument;//保存格式

            object miss = System.Reflection.Missing.Value;

            wordDoc.SaveAs(ref fileName, 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对象

            object SaveChanges = WdSaveOptions.wdSaveChanges;

            object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;

            object RouteDocument = false;

            wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

            wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);

        }

        //在书签处插入值

        public bool InsertValue(string bookmark, string value)
        {

            object bkObj = bookmark;

            if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
            {

                wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();

                wordApp.Selection.TypeText(value);

                return true;

            }

            return false;

        }

        //插入表格,bookmark书签

        public Table InsertTable(string bookmark, int rows, int columns, float width)
        {

            object miss = System.Reflection.Missing.Value;

            object oStart = bookmark;

            Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置

            Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss);

            //设置表的格式

            newTable.Borders.Enable = 1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过)

            newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;//边框宽度

            if (width != 0)
            {

                newTable.PreferredWidth = width;//表格宽度

            }

            newTable.AllowPageBreaks = false;

            return newTable;

        }

        //合并单元格 表名,开始行号,开始列号,结束行号,结束列号

        public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
        {

            table.Cell(row1, column1).Merge(table.Cell(row2, column2));

        }

        //设置表格内容对齐方式 Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1)

        public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, 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;//底端对齐

            }

        }

        //设置表格字体

        public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size)
        {

            if (size != 0)
            {

                table.Range.Font.Size = Convert.ToSingle(size);

            }

            if (fontName != "")
            {

                table.Range.Font.Name = fontName;

            }

        }

        //是否使用边框,n表格的序号,use是或否

        public void UseBorder(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开始记

        public void AddRow(int n)
        {

            object miss = System.Reflection.Missing.Value;

            wordDoc.Content.Tables[n].Rows.Add(ref miss);

        }

        //给表格添加一行

        public void AddRow(Microsoft.Office.Interop.Word.Table table)
        {

            object miss = System.Reflection.Missing.Value;

            table.Rows.Add(ref miss);

        }

        //给表格插入rows行,n为表格的序号

        public void AddRow(int n, int rows)
        {

            object miss = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];

            for (int i = 0; i < rows; i++)
            {

                table.Rows.Add(ref miss);

            }

        }

        //给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素

        public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value)
        {

            table.Cell(row, column).Range.Text = value;

        }

        //给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素

        public void InsertCell(int n, int row, int column, string value)
        {

            wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;

        }

        //给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值

        public void InsertCell(int n, int row, int columns, string[] values)
        {

            Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];

            for (int i = 0; i < columns; i++)
            {

                table.Cell(row, i + 1).Range.Text = values[i];

            }

        }

        //插入图片

        public void InsertPicture(string bookmark, string picturePath, float width, float hight)
        {

            object miss = System.Reflection.Missing.Value;

            object oStart = bookmark;

            Object linkToFile = false; //图片是否为外部链接

            Object saveWithDocument = true; //图片是否随文档一起保存

            object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//图片插入位置

            wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);

            wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; //设置图片宽度

            wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight; //设置图片高度

        }

        //插入一段文字,text为文字内容

        public void InsertText(string bookmark, string text)
        {

            object oStart = bookmark;

            object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;

            Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range);

            wp.Format.SpaceBefore = 6;

            wp.Range.Text = text;

            wp.Format.SpaceAfter = 24;

            wp.Range.InsertParagraphAfter();

            wordDoc.Paragraphs.Last.Range.Text = "\n";

        }

        // 杀掉winword.exe进程

        public void killWinWordProcess()
        {

            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");

            foreach (System.Diagnostics.Process process in processes)
            {

                bool b = process.MainWindowTitle == "";

                if (process.MainWindowTitle == "")
                {

                    process.Kill();

                }

            }

        }

    }

}
第二部分,具体生成文档的编码

代码见下文:

1,首先需要载入模板

Report report = new Report();

report.CreateNewDocument(TemPath); //模板路径

2,插入一个值

report.InsertValue("Bookmark_value", "世界杯");//在书签“Bookmark_value”处插入值

3,创建一个表格

Table table = report.InsertTable("Bookmark_table", 2, 3, 0); //在书签“Bookmark_table”处插入2行3列行宽最大的表

4,合并单元格

report.MergeCell(table, 1, 1, 1, 3); //表名,开始行号,开始列号,结束行号,结束列号

5,表格添加一行

report.AddRow(table); //表名

6,在单元格中插入值

report.InsertCell(table, 2, 1, "R2C1");//表名,行号,列号,值

7,设置表格中文字的对齐方式

report.SetParagraph_Table(table, -1, 0);//水平方向左对齐,垂直方向居中对齐

8,设置表格字体

report.SetFont_Table(table, "宋体", 9);//宋体9磅

9,给现有的表格添加一行

report.AddRow(1); //给模板中第一个表格添加一行

10,确定现有的表格是否使用边框

report.UseBorder(1, true); //模板中第一个表格使用实线边框

11,给现有的表格添加多行

report.AddRow(1, 2); //给模板中第一个表格插入2行

12,给现有的表格插入一行数据

string[] values={ "英超", "意甲", "德甲", "西甲", "法甲" };

report.InsertCell(1, 2, 5, values); //给模板中第一个表格的第二行的5列分别插入数据

12,插入图片

string picturePath = @"C:\Documents and Settings\Administrator\桌面\1.jpg";

report.InsertPicture("Bookmark_picture", picturePath, 150, 150); //书签位置,图片路径,图片宽度,图片高度

13,插入一段文字

string text = "长期从事电脑操作者,应多吃一些新鲜的蔬菜和水果,同时增加维生素A、B1、C、E的摄入。为预防角膜干燥、眼干涩、视力下降、甚至出现夜盲等,电 脑操作者应多吃富含维生素A的食物,如豆制品、鱼、牛奶、核桃、青菜、大白菜、空心菜、西红柿及新鲜水果等。";

report.InsertText("Bookmark_text", text);

14,最后保存文档

report.SaveDocument(RepPath); //文档路径

第四步,运行程序生成文档,并查看生成的文档

自动生成的文档如下图

测试中遇到的BUG

1. 出现两个对话框,一个对话框提示“(前面省略)…被呼叫方拒绝接收呼叫。(Exception from HRESULT:0x80010001 (RPC_E_CALL_REJECTED))”;另一个对话框提示““**(此处是模板文件名)”中的拼写或语法错误过多,无法继续显示。如果要检查此文档的拼写和语法,请选择“工具”菜单中的“拼写和语法”命令。”。

分析原因:这可能是由于Word中有对英文的拼写和语法有自动检测的功能,而使某些字母组合被认为是语法错误。

解决方案:选择“工具”菜单中的“拼写和语法”命令,将所有“不在词典中:”的红色字体“添加到词典”,如下图所示:

注意:如果程序自动生成报告失败,那么程序中调用的模板的进程不会自动结束,需要在任务管理器中手动删除模板的进程“WINWORD.EXE”。

测试环境

操作系统:Windows XP sp3

语言:c#

编译器:Microsoft Visual Studio 2005

框架版本:Microsoft .NET Framework 2.0

Office版本:Microsoft Office 2003

结束

时间: 2024-08-09 19:51:51

Asp.Net 操作word 第二篇[推荐]的相关文章

ASP.NET 操作WORD 遇到的两个问题之后的解决办法

因为用到ASP.NET操作word,根据网页内容在服务端生成word之后,供用户下载. 在本机测试时,一切正常,当我发布到服务器之后,问题来了 1,提示:未将对象引用设置到对象的实例. 搜素之后,按照网上的内容开始折腾,才发现这个问题真烦人. 根据网友的帖子,cmd 输入"dcomcnfg.exe", 如图所示,并没有出现帖子所描述的word之类的,唯一沾边的也就这个了,经过各种权限设置之后,还是不行(也可能时我设置的不对). 突然想起来服务器环境时64位,而开发环境时32位,搜索之后

ASP.NET操作Word的IIS权限配置

ASP.NET账号在默认情况下是没有权限操作Microsoft Office对象的,如果不进行权限的配置,代码会抛出类似以下的异常: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005. 这样给Asp.NET操作Microsoft Office对象带来了一定的困难.但我们还是要想办法来进行Office对象的操作.下面简单介绍一下本人在开发过程中曾经使用过的方法: 方法1:配置W

Asp.net操作Word文档,原来这么简单啊!

引用Word对象库文件  具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的! 如图 这会自动在你的应用程序文件夹中放置一个程序集(assembly)将COM接口邦定到Word. 上传并存储word文件 上传文件时首先通过上传控件找到所需上传的文件,然后获取文件的大小,最后以流的形式写入数据库 具体代码为 C#

asp.net signalR 专题—— 第二篇 对PersistentConnection持久连接的快速讲解

上一篇我们快速的搭建了一个小案例,但是并没有对其中的方法进行介绍,这一篇我来逐一解析下. 一:从override的那些方法说起 不管怎么样,我们先上代码,如下: 1 public class MyConnection1 : PersistentConnection 2 { 3 protected override Task OnConnected(IRequest request, string connectionId) 4 { 5 return Connection.Send(connect

asp.net 操作word

参考一:点击这里 参考二:点击这里 using System; using System.Web.Security; using Microsoft.Office.Interop.Word; using System.IO; namespace WeixinService.Bll { public class WordOp { private ApplicationClass _wordApp; private Document _wordDoc; private static bool _is

【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)

目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策略(MVC5+EF6) [第四篇]ASP.NET MVC快速入门之完整示例(MVC5+EF6) [番外篇]ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6) 请关注三石的博客:http://cnblogs.com/sanshi 数据库连接字符串 上一篇文章中,我们使用MVC的

Asp.net管道 (第二篇)

从请求进入ASP.NET工作者进程,直至它到达最终的处理程序之前要经过一系列的步骤和过程,这个步骤和过程称为ASP.NET处理管道. Asp.net的处理管道流程如下: 语言描述如下: Asp.net处理管道的第一步是创建HttpWorkerRequest对象,它包含于当前请求有关的所有信息. HttpWorkerRequest把请求传递给HttpRuntime类的静态ProcessRequest方法.HttpRuntime首先要做的事是创建HttpContext对象,并用HttpWorkerR

深入理解javascript对象系列第二篇——属性操作

× 目录 [1]查询 [2]设置 [3]删除[4]继承 前面的话 对于对象来说,属性操作是绕不开的话题.类似于“增删改查”的基本操作,属性操作分为属性查询.属性设置.属性删除,还包括属性继承.本文是对象系列的第二篇——属性操作 属性查询 属性查询一般有两种方法,包括点运算符和方括号运算符 var o = { p: 'Hello World' }; o.p // "Hello World" o['p'] // "Hello World" [注意]变量中可以存在中文,因

.NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了

作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/9985451.html 本来这篇只是想简单介绍下ASP.NET Core MVC项目的(毕竟要照顾到很多新手朋友),但是转念一想不如来点猛的(考虑到急性子的朋友),让你通过本文的学习就能快速的入门ASP.NET Core.既然是快速入门所以过多过深的内容我这里就一笔带过了!然后在后面的一些列文章中再慢慢的对其中的概念进行阐述. 本文已收录至.NET Core实战项目之CMS 第一章 入门篇-开篇及总体规