Skip to content
Loading
  • Piyush Pansuriya
    +1
    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
  • Piyush Pansuriya
    0
    Hi,
    Please share your downloading file code.
    Problem is not in web.config. 
    0
  • Amit Kumar
    0
    Hi
       Please share your code.
    0
  • 0
    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");  
    0
  • Amit Kumar
    0
    Please share web.config also. and change your code
     
    string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "//SurveyReport.xlsx";
    0
  • Amit Kumar
    0
    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");  
     
    0
  • 0
    1. xml version="1.0" encoding="utf-8"?>  
    2.   
    3.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
    4.   configSections>  
    5.   <connectionStrings>  
    6.     <add name="CMSEntities" connectionString="Data Source=192.168.1.117;Initial Catalog=vecino;User ID=sa;Password=mipl@123" providerName="System.Data.SqlClient" />  
    7.   connectionStrings>  
    8.   <appSettings>  
    9.     <add key="myFilePath" value="D:\Kalyani Work\VIcino\DailyExcelFileUploadCode\WebApp\Images\" />  
    10.     <add key="ExcelPath" value="D:\Kalyani Work\VIcino\DailyExcelFileUploadCode\WebApp\Excel\" />  
    11.     <add key="FileUploadPath" value="C:/Users/COM-C/Source/Workspaces/InhouseDailyExpenseExcelProject/DailyExcelFileUploadCode/WebApp/UploadedFiles/" />  
    12.     <add key="webpages:Version" value="3.0.0.0" />  
    13.     <add key="webpages:Enabled" value="false" />  
    14.     <add key="ClientValidationEnabled" value="true" />  
    15.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
    16.   appSettings>  
    17.   <system.web>  
    18.     <compilation debug="true" targetFramework="4.5" />  
    19.     <httpRuntime targetFramework="4.5" />  
    20.     <httpModules>  
    21.       <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />  
    22.     httpModules>  
    23.   system.web>  
    24.   <runtime>  
    25.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
    26.       <dependentAssembly>  
    27.         <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />  
    28.         <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />  
    29.       dependentAssembly>  
    30.       <dependentAssembly>  
    31.         <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />  
    32.         <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />  
    33.       dependentAssembly>  
    34.       <dependentAssembly>  
    35.         <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />  
    36.         <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />  
    37.       dependentAssembly>  
    38.       <dependentAssembly>  
    39.         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />  
    40.         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
    41.       dependentAssembly>  
    42.       <dependentAssembly>  
    43.         <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />  
    44.         <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />  
    45.       dependentAssembly>  
    46.       <dependentAssembly>  
    47.         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />  
    48.         <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />  
    49.       dependentAssembly>  
    50.       <dependentAssembly>  
    51.         <assemblyIdentity name="DocumentFormat.OpenXml" publicKeyToken="8fb06cb64d019a17" culture="neutral" />  
    52.         <bindingRedirect oldVersion="0.0.0.0-2.8.0.0" newVersion="2.8.0.0" />  
    53.       dependentAssembly>  
    54.     assemblyBinding>  
    55.   runtime>  
    56.   <system.codedom>  
    57.     <compilers>  
    58.       <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />  
    59.       <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />  
    60.     compilers>  
    61.   system.codedom>  
    62.   <system.webServer>  
    63.     <validation validateIntegratedModeConfiguration="false" />  
    64.     <modules>  
    65.       <remove name="ApplicationInsightsWebTracking" />  
    66.       <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />  
    67.     modules>  
    68.   system.webServer>  
    69.   <entityFramework>  
    70.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />  
    71.     <providers>  
    72.       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />  
    73.     providers>  
    74.   entityFramework>  
    75. configuration>  
    0
  • 0
    for Amit Kumar
    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");  
     Error occured on this code
     
    {"The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"} 
    0
  • 0
    @piyush sir
     
    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. }    
    but this code througth Code Executed but in excel file data is not display. 
    0
  • 0
    @Amit Kumar Sir
     
    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. if (System.IO.File.Exists(path))        
    7. {        
    8. byte[] fileBytes = System.IO.File.ReadAllBytes(path);        
    9. return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "Download" + "surveyExcel.xlsx");        
    10. }     
     this code using excel file generated in server location but how to download this excel file   in  download Folder.
    0
  • 0
    @Amit Kumar 
     
    ur code is executed on the local machine but on server same issue occurred"Web Config "
    1. string contentType = "application/excel";        
    2. string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ExcelPath"].ToString() + "SurveyReport.xlsx";       
    3. worKbooK.SaveAs(path);      
    4. excel.Quit();      
    5. if (System.IO.File.Exists(path))          
    6. {          
    7. byte[] fileBytes = System.IO.File.ReadAllBytes(path);          
    8. return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "Download" + "surveyExcel.xlsx");          
    9. }   
     how to resolved this web config Issue.
    0