workbook.SaveAs(Filepath)在暂存环境中不起作用

时间:2018-06-06 02:48:02

标签: c# excel office-interop excel-interop

我正在尝试在读取Excel工作表时从excel中删除空行和列。 在我的本地环境中正常工作。在暂存环境中没有工作的workbook.SaveAs()功能。我已经放置了一些日志来读取该日志的跟踪和错误方法,在workbook.SaveAs()之后行下一个日志没有读取我知道这是什么解决方案。Code image

获取以下错误: 由于以下错误,检索CLSID为{00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80080005服务器执行失败(HRESULT异常:0x80080005(CO_E_SERVER_EXEC_FAILURE))。

        [HttpPost]
        public ActionResult ImportPartNumbersFromexcel()
        {
        DataSet ds = new DataSet();
        string UserID = Convert.ToString(Session["userID"]);
        ds = dbOperations.GetPartNumbers(UserID);
        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", "GetPartNumbers", null);
        DataTable partnums = ds.Tables[0];
        //  Get all files from Request object  
        HttpFileCollectionBase files = Request.Files;
        HttpPostedFileBase file = null;
        string extension = string.Empty;
        string fname = "";
        string NewPath = string.Empty;
        for (int i = 0; i < files.Count; i++)
        {
        file = files[i];
        // Checking for Internet Explorer  
        if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
        {
        string[] testfiles = file.FileName.Split(new char[] { '\\' });
        fname = testfiles[testfiles.Length - 1];
        }
        else
        {
        fname = file.FileName;
        }
        FileInfo fi = new FileInfo(fname);
        extension = fi.Extension;
        fname = UserID + "_" + fname + "_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension;
        // Get the complete folder path and store the file inside it.  
        fname = Path.Combine(Server.MapPath("~/EOrderImports/"), fname);
        file.SaveAs(fname);
        }
        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", fname, null);
        string connString = "";

        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(fname);
        Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Worksheets["Sheet1"];
        Microsoft.Office.Interop.Excel.Range usedRange = worksheet.UsedRange;


        object[,] allValues = (object[,])usedRange.Cells.Value;
        int totalRows = usedRange.Rows.Count;
        if (totalRows == 1)
        {
            return Json("InvalidTemplate", JsonRequestBehavior.AllowGet);
        }

        #region Delete Empty Rows and Cols from Template
        DeleteEmptyRowsCols(worksheet);
        #endregion

        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", "DeleteEmptyRowsCols", null);
        string filename = file.FileName;
        FileInfo fii = new FileInfo(filename);
        fname = Path.GetFileNameWithoutExtension(fii.Name);
        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", fname, null);
        string fname1 = string.Empty;

        fname = fname + "_Removed" + "_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + extension;

        // Get the complete folder path and store the file inside it.  
        fname = Path.Combine(Server.MapPath("~/EOrderImports/"), fname);

        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", fname1, null);
        //workbook.SaveAs(fname1);
        try
        {
         workbook.SaveAs(fname, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange);
        } 
        catch (Exception ex)
        {
        Logger.WriteLog("Public", "ImportPartNumbersFromexcel", ex, null);
        }


        Logger.TestWriteLog("Public", "ImportPartNumbersFromexcel", "SaveAS", null);
        }

1 个答案:

答案 0 :(得分:0)

请参阅Retrieving the COM class factory for component failed

我刚刚粘贴了修复内容。

在DCOMCNFG中,右键单击“我的电脑”,然后选择属性。

选择COM证券选项卡。

在“访问权限”中,单击“编辑默认值”并将“网络服务”添加到    它并给它允许本地访问权限。为&lt;做同样的事情    Machine_name&gt; \ Users。

在“启动和激活权限”中,单击“编辑默认值”并添加    网络服务,并为其提供本地启动和本地激活    允许。为&lt;做同样的事情Machine_name&gt; \ Users。