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 _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;
            _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(); 
        }
    }
}

这样引用:

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

 

解决办法:

解决办法:解决方案资源管理器 
-> 引用 -> "Microsoft.Office.Interop.Word" -> 右键选择属性 ->
嵌入互操作类型的值改为"false"即可。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。