13
Answers

Export Excel not working in Server

Hello All,
 
I am working with Excel. i have a an issue if i export to excel in local Machine it's working fine when i Deploy in server it's not working i got an error like Request failed. 
 
Here is my code:
 
 
public JsonResult ExportEMCTSReleaseDetails()
{     
   Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
   try
   {
         Excel.Workbook xlWb;
         Excel.Worksheet xlWs;
         long lnRelease = 0;
         int iRevision;
         string strHeader = string.Empty;
         string strEMCTSRelease = string.Empty;
         string strShtName = string.Empty;
         string strProject = string.Empty;
         string strDesign = string.Empty;
          string strDRevision = string.Empty;
          for (int j = 0; j < SearchRecords.Count(); j++)
          {
                     if (SearchRecords[j].Comments == "History Starts")
                     {
                           SearchRecords.RemoveRange(j, SearchRecords.Count() - j);
                             break;
                       }
            }
            xlApp.SheetsInNewWorkbook = SearchRecords.Count();
            xlWb = xlApp.Workbooks.Add();
         List<IndexEmctestSetup> RelDetailsSearchRecords = new List<IndexEmctestSetup>();
            int r;
            for (int i = 0; i < SearchRecords.Count(); i++)
            {
                  lnRelease = SearchRecords[i].ReleaseID;
                  iRevision = (int)SearchRecords[i].Revision;
                  RelDetailsSearchRecords = null;
                  RelDetailsSearchRecords = GetEMCTSReleaseVersionRecords(lnRelease, iRevision);
                  r = 5;
                  xlWs = xlWb.Worksheets.get_Item(i + 1);
                  RelDetailsSearchRecords.ForEach(delegate(IndexEmctestSetup e)
                  {
                  strEMCTSRelease = e.ReleaseName;
                  strProject = e.Project;
                  xlWs.Cells[r, 1] = e.ReleaseName + " - " + e.ReleaseRevision;
               strShtName = e.ReleaseName + " - " + e.ReleaseRevision;
               xlWs.Cells[r, 2] = e.Project;
                  xlWs.Cells[r, 3] = e.ECSEMCTestSetupId + " - " + e.Revision;
               xlWs.Cells[r, 4] = e.ECSSuitcaseTesterId;
               xlWs.Cells[r, 5] = e.ECSSuitcaseControllerId;
               xlWs.Cells[r, 6] = e.ECSEMCLoadBoxId;
               xlWs.Cells[r, 7] = e.ECSEMCModuleId;
                 xlWs.Cells[r, 8] = e.HarnessId;
                  xlWs.Cells[r, 9] = (!string.IsNullOrEmpty(e.DataCollector)) && (e.DataCollector != clsLists.NA) ? e.DataCollector : clsLists.NA;
               xlWs.Cells[r, 10] = (!string.IsNullOrEmpty(e.TriggerBox)) && (e.TriggerBox != clsLists.NA) ? e.TriggerBox : clsLists.NA;
                 xlWs.Cells[r, 11] = e.Location;
                  xlWs.Cells[r, 12] = e.Disposition;
               xlWs.Cells[r, 13] = e.Comments;
                  r++;
               });
strHeader = "EMCTestSetup Release \"" + strShtName + "\" Exported on: " + System.DateTime.Now.ToString() + " by \"" + clsLists.getCurrentUserEmail() + "\"";
project pr = ReleaseEMCTSDB.projects.Where(x => x.ProjectName == strProject).FirstOrDefault();
platform pf = ReleaseEMCTSDB.platforms.Where(x => x.PlatformId == pr.Platform_Proj).FirstOrDefault();
strProject = "Project: " + strProject + "(" + pr.ProjectDescription + ")";
releasesemctestsetup rel = ReleaseEMCTSDB.releasesemctestsetups.Where(m => m.ReleaseName == strEMCTSRelease).FirstOrDefault();
strDesign = "Release Design: " + clsLists.getDesigns().Find(x => x.Value == rel.Design).Text;
strDRevision = "Release Design Revision: " + rel.DesignRevision.ToString();
xlWs.Cells[1, 1] = strHeader;
xlWs.Cells[2, 1] = strProject;
xlWs.Cells[3, 1] = strDesign;
xlWs.Cells[3, Convert.ToInt32(clsLists.getEMCTSReleaseDetailsExportParamList().Count / 2)] = strDRevision;
int col = 1;
clsLists.getEMCTSReleaseDetailsExportParamList().ForEach(delegate(string strParam)
{
xlWs.Cells[4, col] = strParam;
col++;
});
xlWs.Name = strShtName;
Excel.Range rng;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[1, 1], (Excel.Range)xlWs.Cells[1, clsLists.getEMCTSReleaseDetailsExportParamList().Count]);
rng.Merge(Missing.Value);
rng.EntireRow.Font.Bold = true;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[2, 1], (Excel.Range)xlWs.Cells[2, Convert.ToInt32(clsLists.getEMCTSReleaseDetailsExportParamList().Count / 2) - 1]);
rng.Merge(Missing.Value);
rng.EntireRow.Font.Bold = true;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[2, Convert.ToInt32(clsLists.getEMCTSReleaseDetailsExportParamList().Count / 2)], (Excel.Range)xlWs.Cells[2, clsLists.getEMCTSReleaseDetailsExportParamList().Count]);
rng.Merge(Missing.Value);
rng.EntireRow.Font.Bold = true;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[3, 1], (Excel.Range)xlWs.Cells[3, Convert.ToInt32(clsLists.getEMCTSReleaseDetailsExportParamList().Count / 2) - 1]);
rng.Merge(Missing.Value);
rng.EntireRow.Font.Bold = true;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[3, Convert.ToInt32(clsLists.getEMCTSReleaseDetailsExportParamList().Count / 2)], (Excel.Range)xlWs.Cells[3, clsLists.getEMCTSReleaseDetailsExportParamList().Count]);
rng.Merge(Missing.Value);
rng.EntireRow.Font.Bold = true;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[4, 1], (Excel.Range)xlWs.Cells[4, clsLists.getEMCTSReleaseDetailsExportParamList().Count]);
rng.EntireRow.Font.Bold = true;
rng.Interior.Color = Excel.XlRgbColor.rgbLightSteelBlue;
rng = xlWs.get_Range((Excel.Range)xlWs.Cells[1, 1], (Excel.Range)xlWs.Cells[xlWs.UsedRange.Rows.Count, clsLists.getEMCTSReleaseDetailsExportParamList().Count]);
rng.Borders[Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Blue.ToArgb();
rng.Borders[Excel.XlBordersIndex.xlEdgeLeft].Color = Color.Blue.ToArgb();
rng.Borders[Excel.XlBordersIndex.xlEdgeRight].Color = Color.Blue.ToArgb();
rng.Borders[Excel.XlBordersIndex.xlEdgeTop].Color = Color.Blue.ToArgb();
rng.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Color = Color.Blue.ToArgb();
rng.Borders[Excel.XlBordersIndex.xlInsideVertical].Color = Color.Blue.ToArgb();
xlWs.get_Range((Excel.Range)xlWs.Cells[4, 1], (Excel.Range)xlWs.Cells[xlWs.UsedRange.Rows.Count, clsLists.getEMCTSReleaseDetailsExportParamList().Count]).EntireColumn.AutoFit();
}
xlApp.Visible = true;
return Json(new { Error = "Please save file" });
}
catch (Exception e)
{
         xlApp.Visible = true;
          return Json(new { Error = "Failed to generate the report" });
}
}

Answers (13)