Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Situation: I have created a wcf service which uses Microsoft.Interop to generate excel file depending on the values I send. This service then writes the generated excel file to the client which happens to be the host of our website.

process flow:
Client request -> Server -> Wcf [writes file] -> server[send path to] ->client


After I generate the excel I have verified that the created file is fine when I open it in excel(file written on my website hosting server by wcf). But when I send it to the client for download and download it on client I am getting file with garbage values filled in it.

I have another such function which generates the excel file and is downloaded correctly (for a different module). But for the same code although I get the file generated correctly on the server but when it is exposed to client and downloaded it shows all garbage values inside the excel file.

Please suggest.

Here is my code:-
C#
public void ExportToExcel(SalesCriteriaReport obj)
   {
       try
       {
           OperationServiceClient client = new OperationServiceClient();
           Operations.IOperationService opService;
           opService = client;
           Operations.SalesCriteriaReport objReport = new Operations.SalesCriteriaReport();
           //Operations.SalesCriteriaReport objReport1 = new Operations.SalesCriteriaReport();
           /* Total 17 values */
           objReport.FilePath = obj.FilePath;
           objReport.procName = obj.procName;
           objReport.ReportName = obj.ReportName;
           objReport.sqlConnectionString = sqlConnectionString;

           objReport.RoleID = obj.RoleID;
           objReport.UserID = obj.UserID;
           objReport.isVolume = obj.isVolume;
           objReport.locationFilters = obj.locationFilters;
           objReport.modalityFilters = obj.modalityFilters;
           objReport.practiceFilters = obj.practiceFilters;
           objReport.referringPracticeFilters = obj.referringPracticeFilters;
           objReport.referringPhysiciansFilters = obj.referringPhysiciansFilters;
           objReport.specialityFilters = obj.specialityFilters;
           objReport.cptFilters = obj.cptFilters;
           objReport.rowArea = obj.rowArea;
           objReport.pageNumber = obj.pageNumber;
           objReport.pageSize = obj.pageSize;

           Operations.ExportedFileProperty excelFile = new ExportedFileProperty();
           Operations.ExportedFileProperty excelFile2 = new ExportedFileProperty();
           client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 5, 00);
           client.Open();
           excelFile = opService.SalesExportToExcel(objReport); // service call
                                               //which generates file correclty
           client.Close();
           if (excelFile.IsSucess) // gives access to file but when downloaded
                                   // gives files with garbage values filled.
           {
               HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + excelFile.FileName + excelFile.Extension);
               HttpContext.Current.Response.AddHeader("Content-Type", "application/Excel");
               HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
               HttpContext.Current.Response.WriteFile(excelFile.FilePath + excelFile.FileName + excelFile.Extension);
           }
           else
               //Response.Write("Error in file writing " + excelFile.ErrorMessage);
               throw new Exception(excelFile.ErrorMessage);
           try
           {
               if (client.State != System.ServiceModel.CommunicationState.Faulted)
               {
                   client.Close();
               }
           }
           catch (Exception ex)
           {
               client.Abort();
           }
           HttpContext.Current.Response.End();
       }
       catch (Exception ex)
       {
           Response.Write(ex.Message);
       }
   }
Posted
Updated 29-Jan-15 23:48pm
v2
Comments
Pikoh 30-Jan-15 5:56am    
Have you tried changing ContentType to application/XLSX?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900