日期:2014-05-18  浏览次数:20466 次

C# 调用word并读取里面的内容
在VS里运行没问题,部署到IIS上就出现各种问题,开始是 oWord = new Word.Application(); 这句话报错,网上查了查实权限问题,改完之后说 returnContent = oDoc.Content.Text; 这句未将对象引用到实例,代码如下:

C# code
 protected void Button1_Click(object sender, EventArgs e)
        {
            object oReadOnly = true;
            object oMissing = System.Reflection.Missing.Value;

            object oFileName ="E:/文档.docx";
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            //oWord.Visible = true;
            oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);


            returnContent = oDoc.Content.Text;


            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);


            WorkLog wl = new WorkLog();
            wl.Acnt_id = "111";
            wl.Del_flg = false;
            wl.Dma_code = "";
            wl.Log_body = returnContent;
            wl.Log_time = DateTime.Now;
            wl.Mdf_time = DateTime.Now;

            AccountService As = new AccountService();
            Account acc = As.Login("test", "test");
            As.AddWorkLog(wl);

            //关闭COM
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            //File.Delete(oFileName.ToString());
        }
    }


------解决方案--------------------
用這個
http://ufo-crackerx.blog.163.com/blog/static/11307877820111017113958413/