0

I am trying to return xls file in hebrew but it returns me gibrish letters and writes that the file extension isn't the file real content.

Please help me.

public void ExportAddBalanceExcel(int CompanyID)
{
    string writer = "";
    writer += "<table><tr><td>UserName</td><td> PrivateName</td><td>LastName</td><td>Balance</td></tr>";

    List<CompanyEmployee> employees = Company.LoadByID(CompanyID).LoadAllEmployees();

    foreach (BLL.CompanyEmployee e in employees)
    {
        writer += "<tr>";
        writer += "<td>" + e.EmployeeUserName+ "</td>";
        writer += "<td>" + e.EmployeeFirstName+ "</td>";
        writer += "<td>" + e.EmployeeLastName  + "</td>";
        writer += "</tr>";
    }
    writer += "</table>";
    string currentData = DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year;

    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=AddBalanceToEmployees_" + currentData + ".xls");
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1255");
    Response.Write(writer);
    Response.Flush();
    Response.End();            
}
Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
Limor
  • 1
  • 1
  • 3

1 Answers1

0

How would this be opened in Excel? You seem to be generating some HTML-like syntax that you're telling the client is an Excel file.

To write to Excel, you could use COM Interop (though this is not recommended for use server-side), or if XLSX is sufficent you could look at the OpenXML SDK (a simple example here). If you require XLS you might want to check out some 3rd party libraries.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45