asp.net 操作word

简介: 参考一:点击这里 参考二:点击这里 参考三:点击这里 using System; using System.Web.Security; using Microsoft.Office.Interop.

参考一:点击这里

参考二:点击这里

参考三:点击这里

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 _isOpened = false;//判断word模版是否被占用 
        public void SaveAs(string strFileName, bool isReplace)
        {
            if (isReplace && File.Exists(strFileName))
            {
                File.Delete(strFileName);
            }
            object missing = Type.Missing;
            object fileName = strFileName;
            _wordDoc.SaveAs(ref fileName, 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);
        }

        //定义一个Word.Application 对象 
        public void ActiveWordApp()
        {
            _wordApp = new ApplicationClass();
        }

        public void Quit()
        {
            object missing = System.Reflection.Missing.Value;
_wordDoc.Close(ref missing, ref missing, ref missing);//word关闭应该在ApplicationClass之前,如果不加这段代码,可能会报进程正在使用无法操作的错误
            _wordApp.Application.Quit(ref missing, ref missing, ref missing);
            _isOpened = false;
        }

        //基于模版新建Word文件 
        public void OpenTempelte(string strTemppath)
        {
            object missing = Type.Missing;
            //object Missing = System.Reflection.Missing.Value; 
            ActiveWordApp();
            _wordApp.Visible = false;
            object oTemplate = (object)strTemppath;
            try
            {
                while (_isOpened)
                {
                    System.Threading.Thread.Sleep(500);
                }
                _wordDoc = _wordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
                _isOpened = true;
                _wordDoc.Activate();
            }
            catch (Exception Ex)
            {
                Quit();
                _isOpened = false;
                throw new Exception(Ex.Message);
            }
        }
        public void FillLable(string labelId, string content)
        {
            //打开Word模版 
            // OpenTempelte(tempName); //对LabelId的标签进行填充内容Content,即函件题目项 
            object bkmC = labelId;
            if (_wordApp.ActiveDocument.Bookmarks.Exists(labelId))
            {
                _wordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
            }
            _wordApp.Selection.TypeText(content);
            //SaveAs(saveAsFileName); 
            //Quit(); 
        }
       /// <summary>
       /// 向word标签插入图片
        /// </summary>
       /// <param name="labelId"></param>
       /// <param name="imgpath"></param>
       public void FillLableImg(string labelId, string imgpath)
       {
           //打开Word模版 
           // OpenTempelte(tempName); //对LabelId的标签进行填充内容Content,即函件题目项 
           object bkmC = labelId;
           if (_wordApp.ActiveDocument.Bookmarks.Exists(labelId))
           {
               _wordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
           }
           object Anchor = _wordApp.Selection.Range;
           object LinkToFile = false;
           object SaveWithDocument = true;
           object Left = 1;
           object Top = 1;
           object Width = 40;
           object Height = 32;
           _wordApp.ActiveDocument.Shapes.AddPicture(imgpath, ref LinkToFile, ref SaveWithDocument, ref Left, ref Top, ref Width, ref Height, ref Anchor);
       }
    }
}

这样引用:

public void Test(HttpContext context)
       {
           try
           {
               string path = context.Server.MapPath("RiskNoticeDoc");
               string templatePath = path + @"\RiskNoticeModel.dot";
               var wop = new WordOp();
               wop.OpenTempelte(templatePath);
               wop.FillLable("zs", "测试");
               wop.SaveAs(@"E:\Projects\微信公共服务平台项目\WeixinService\WeixinService.Web\RiskNoticeDoc\zw.docx", true);
               wop.Quit();
               //context.Response.Redirect(@"/download/savetest.doc");//做个跳转用于下载. 
           }
           catch (Exception)
           {
               throw;
           }
       }

 

报错:

Error    1    Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.    E:\Projects\微信公共服务平台项目\WeixinService\WeixinService.Bll\WordOp.cs    10    17    WeixinService.Bll

 

解决办法:

目录
相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
43 0
|
存储 开发框架 NoSQL
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
284 0
ASP.NET Core微服务(六)——【.Net Core操作redis】StackExchange.Redis
|
开发框架 JavaScript 前端开发
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
332 0
ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
129 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
124 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
136 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(5)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
105 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(4)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
109 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(3)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
107 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
|
开发框架 前端开发 JavaScript
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(1)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(1)
122 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(1)