当我使用iis将mvc应用程序部署到活动服务器时,为什么会出现错误“来自HRESULT的异常:0x800A03EC”?

时间:2019-02-15 12:09:23

标签: c# model-view-controller excel-interop

但是我正在上传一个excel文件,并试图读取其内容。使用以下代码,它可以在localhost上正常工作。但是,如果我使用IIS上传到实时服务器,则无法正常工作。奇怪的是,我上传的文件确实保存到服务器上的内容文件夹中,但是在错误消息中,它看起来似乎是从本地计算机打开该文件:

Microsoft.Office.Interop.Excel.Workbooks.Open(字符串文件名,对象更新链接,对象只读,对象格式,对象密码,对象WriteResPassword,对象IgnoreReadOnlyRecommended,对象来源,对象定界符,对象可编辑,对象通知,对象转换器,对象AddToMru,对象本地,对象CorruptLoad)+0    Twilio4.Controllers.HomeController.Import(HttpPostedFileBase excelfile,SendMessageVal模型)

在C:\ Users \ sbudhai \ source \ repos \ SAP \ SAP \ Controllers \ HomeController.cs:111

[HttpPost]
public ActionResult Import(HttpPostedFileBase excelfile, SendMessageVal Model)
{
    if(ModelState.IsValid == true)
    {
        if (excelfile == null || excelfile.ContentLength == 0)
        {
            ViewBag.Error2 = "Please select an excel file <br>";
            return View(Model);
        }
        else
        {
            if (excelfile.FileName.EndsWith("xlsx") || excelfile.FileName.EndsWith("xls"))
            {
                string path = Server.MapPath("~/Content/" + excelfile.FileName);
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);
                excelfile.SaveAs(path);

                //Read data from excel file 
                Excel.Application application = new Excel.Application();
                Excel.Workbook workbook = application.Workbooks.Open(path);
                Excel.Worksheet worksheet = workbook.ActiveSheet;
                Excel.Range range = worksheet.UsedRange;
                List<NUMBER> listProducts = new List<NUMBER>();
                var C = range.Rows.Count;
                var B = range.Rows.Count;

                C++;
                for (int row = 2; row < C; row++)
                {
                    NUMBER p = new NUMBER();
                    p.NUMBER1 = long.Parse(((Excel.Range)range.Cells[row, 1]).Text);
                    listProducts.Add(p);
                }

                ViewBag.ListProducts = listProducts;
                workbook.Close(path);
                return View("Confirm", listProducts);
            }
            else
            {
                ViewBag.Error = "File type is incorrect <br>";
                return View(Model);
            }

        }
    }
    else
    { 
        return View(Model);
    }
}

3 个答案:

答案 0 :(得分:0)

在服务器上使用Office Interop是not supported

链接的文章描述了您将面临的一些问题。我建议使用诸如EPPlus之类的替代方案,或诸如Aspose Cells之类的商业产品。

答案 1 :(得分:0)

您可以使用OpenXML库而不是Office Interop,您将花费时间尝试对其进行修复,但最终要好得多

答案 2 :(得分:0)

根据this Microsoft forum的帖子,您可能需要创建文件夹:

  • C:\ Windows \ SysWOW64 \ config \ systemprofile \ Desktop“(用于64位Windows)

  • C:\ Windows \ System32 \ config \ systemprofile \ Desktop“(用于32位Windows)

并为将要访问它的用户设置完全控制权限(例如,在Win7&IIS 7和DefaultAppPool中,设置用户“ IIS AppPool \ DefaultAppPool”的权限)