Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use the following code for exporting a data table into Microsoft Excel.

C#
public static void ExportToExcel(HttpResponse Response, string sData, string sFileName)
    {
    Response.ClearContent();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment; filename=" + sFileName +  ".xls");
        Response.Write(sData);
    }


The excel file gets generated. When I open the excel file and click on save as dialog window the default selected save as type is (Web Page(.htm or html).) . This should in Excel workbook 97-2003 by default. When I try to send .xls file through a particular mail server the receiver is able to open the attachment file. When I try to send the file which is generated by using the above code the receiver is unable to open the attachment file. The Error message is "This attachment was removed because it contains data that could pose a security risk." I need to resolve this issue any alternative solution. please help me on this regard
Posted
Updated 13-Jan-15 18:04pm
v2

1 solution

The problem is that you're not exporting an Excel file. The content of a Excel 97-2003 file is not a string at all but binary content. The "file" you're exporting is invalid.

You need to generate an actual Excel file using some library designed to do so. In a web server you cannot use Excel itself, but must use either a library dedicated to the purpose or use OleDb methods to generate the data in a sheet. The problem with the OleDb methods is that you get no formatting capabilities at all. You're just plugging data into cells.
 
Share this answer
 

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