I'm trying to delete empty rows and columns from excel when reading that Excel sheet. In my local environment working fine.In staging environment not working workbook.SaveAs() functionality.I have placed some log's to read that log for trail and error method , After workbook.SaveAs() line the next log not reading.let me know what is the solution for this.Code image
Getting Following error : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from 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);
}
Please refer to Retrieving the COM class factory for component failed
I just pasted the content of the fix here.
In DCOMCNFG, right click on the My Computer and select properties.
Choose the COM Securities tab.
In Access Permissions, click Edit Defaults and add Network Service to it and give it Allow local access permission. Do the same for < Machine_name >\Users.
In Launch and Activation Permissions, click Edit Defaults and add Network Service to it and give it Local launch and Local Activation permission. Do the same for < Machine_name >\Users.
User contributions licensed under CC BY-SA 3.0