Skip to content
Loading
From Server to Excel file downloading time this Probem Occur
  • Amit Kumar
    last few line change your code becaue you create excel wrookbook and bind data but you did not save this workbook  in your local system with respected directory so it is not diownlaod. 
    1. worKbooK.Close();  
    2. string contentType = "application/excel";  
    3. string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "\\SurveyReport.xlsx";  
    4. worKbooK.SaveAs(path);  
    5. excel.Quit();  
    6. return File(path, contentType, "Download" + "surveyExcel.xlsx");  
     
  • Amit Kumar
    Please share web.config also. and change your code
     
    string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "//SurveyReport.xlsx";
  • Piyush Pansuriya
    Hi,
     
    Did you get correct path using ?
    1. string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "SurveyReport.xlsx";  
     Please check once putting break point.
    If yes, Then use below code
     
    1. if (System.IO.File.Exists(path))  
    2. {  
    3. byte[] fileBytes = System.IO.File.ReadAllBytes(path);  
    4. return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "Download" + "surveyExcel.xlsx");  
    5. }  
     This is for existing file.
    If you are generating new file than use below code:
    1. return File(path, System.Net.Mime.MediaTypeNames.Application.Octet, "Download" + "surveyExcel.xlsx");  
    1. Microsoft.Office.Interop.Excel.Application excel;  
    2. Microsoft.Office.Interop.Excel.Workbook worKbooK;  
    3. Microsoft.Office.Interop.Excel.Worksheet worKsheeT;  
    4. Microsoft.Office.Interop.Excel.Range celLrangE;  
    5. excel = new Microsoft.Office.Interop.Excel.Application();  
    6. excel.Visible = false;  
    7. excel.DisplayAlerts = false;  
    8. worKbooK = excel.Workbooks.Add(Type.Missing);  
    9. worKsheeT = (Microsoft.Office.Interop.Excel.Worksheet)worKbooK.ActiveSheet;  
    10. var Question = new BussinessLogic.Admin.UserSurvey(new UserSurveyRepository(new CRMContext())).GetAllUserSurvey().Where(m => m.SurveyId == 9);  
    11. worKsheeT.Name = "Admission Data";  
    12. int i = 1;  
    13. int j = 1;  
    14. int kk = 2;  
    15. var eachSurvey = new BussinessLogic.Answer.Answer(new AnswerRepository(new CRMContext())).GetAllAnswer().Where(a=>a.SurveyId==SurveyId).Select(a => a.EachSurveyUniqueNo).Distinct();  
    16. int cou = eachSurvey.Count();  
    17. #region  
    18. foreach (var q in Question)  
    19. {  
    20. var QuestionRow = new BussinessLogic.Admin.QuestionAnswer(new QuestionAnswerRepository(new CRMContext())).GetAllQuestionAnswer().Where(k => k.QuestionId == q.Id && k.ColumnOption == null);  
    21. if (q.QuestionTypeId == 1)  
    22. {  
    23. worKsheeT.Cells[1, i] = q.QuestionText;  
    24. kk = 2;  
    25. var eachSurvey1 = new BussinessLogic.Answer.Answer(new AnswerRepository(new CRMContext())).GetAllAnswer().Where(a => a.SurveyId == SurveyId).Select(a => a.EachSurveyUniqueNo).Distinct();  
    26. foreach (var sam in eachSurvey1)  
    27. {  
    28. var ans = new BussinessLogic.Answer.Answer(new AnswerRepository(new CRMContext())).GetAllAnswer().Where(m => m.QuestionId == q.Id && m.EachSurveyUniqueNo == sam).ToList();  
    29. if (ans.Count() != 0)  
    30. {  
    31. foreach (var an in ans)  
    32. {  
    33. var Ques = new BussinessLogic.Admin.QuestionAnswer(new QuestionAnswerRepository(new CRMContext())).GetAllQuestionAnswer().Where(k => k.Id == an.SelectedAnswerOptionId).FirstOrDefault();  
    34. worKsheeT.Cells[kk, i] = Ques.AnswerOption;  
    35. kk++;  
    36. }  
    37. }  
    38. else  
    39. {  
    40. worKsheeT.Cells[kk, i] = "NA";  
    41. kk++;  
    42. }  
    43. }  
    44. i++;  
    45. }  
    46. if (q.QuestionTypeId == 2)  
    47. {  
    48. var Ques = new BussinessLogic.Admin.QuestionAnswer(new QuestionAnswerRepository(new CRMContext())).GetAllQuestionAnswer().Where(k => k.QuestionId == q.Id).ToList();  
    49. foreach (var anss in Ques)  
    50. {  
    51. kk = 2;  
    52. worKsheeT.Cells[1, i] = q.QuestionText + ": " + anss.AnswerOption;  
    53. var eachSurvey1 = new BussinessLogic.Answer.Answer(new AnswerRepository(new CRMContext())).GetAllAnswer().Where(a => a.SurveyId == SurveyId).Select(a => a.EachSurveyUniqueNo).Distinct();  
    54. foreach (var sam in eachSurvey1)  
    55. {  
    56. var ans1 = new BussinessLogic.Answer.Answer(new AnswerRepository(new CRMContext())).GetAllAnswer().Where(m => m.QuestionId == q.Id && m.SelectedAnswerOptionId == anss.Id && m.EachSurveyUniqueNo == sam).ToList();  
    57. int len = ans1.Count();  
    58. if (len == 0)  
    59. {  
    60. worKsheeT.Cells[kk, i] = "NA";  
    61. kk++;  
    62. }  
    63. else if (len == 1)  
    64. {  
    65. foreach (var b in ans1)  
    66. {  
    67. var Quesoption = new BussinessLogic.Admin.QuestionAnswer(new QuestionAnswerRepository(new CRMContext())).GetAllQuestionAnswer().Where(k => k.Id == b.SelectedAnswerOptionId).FirstOrDefault();  
    68. worKsheeT.Cells[kk, i] = Quesoption.AnswerOption;  
    69. kk++;  
    70. }  
    71. }  
    72. else  
    73. {  
    74. foreach (var b in ans1)  
    75. {  
    76. var Quesoption = new BussinessLogic.Admin.QuestionAnswer(new QuestionAnswerRepository(new CRMContext())).GetAllQuestionAnswer().Where(k => k.Id == b.SelectedAnswerOptionId).FirstOrDefault();  
    77. worKsheeT.Cells[kk, i] = Quesoption.AnswerOption;  
    78. kk++;  
    79. }  
    80. }  
    81. }  
    82. i++;  
    83. j++;  
    84. }  
    85. }  
    86. }  
    87. #endregion  
    88. celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[1, 150]];  
    89. celLrangE.EntireColumn.AutoFit();  
    90. Microsoft.Office.Interop.Excel.Borders border = celLrangE.Borders;  
    91. border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;  
    92. border.Weight = 2d;  
    93. celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[2, 150]];  
    94. worKbooK.Close();  
    95. excel.Quit();  
    96. string contentType = "application/excel";  
    97. string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "SurveyReport.xlsx";  
    98. return File(path, contentType, "Download" + "surveyExcel.xlsx");  
  • Amit Kumar
    Hi
       Please share your code.
  • Piyush Pansuriya
    Hi,
    Please share your downloading file code.
    Problem is not in web.config.