创建xls文件乱码

时间:2014-11-30 16:18:34

标签: c# asp.net xls

我试图用希伯来语返回xls文件,但它返回了我的乱码信,并写道文件扩展名不是文件真实内容。

请帮帮我。

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();            
}

1 个答案:

答案 0 :(得分:0)

如何在Excel中打开它?您似乎正在生成一些类似HTML的语法,您告诉客户端是Excel文件。

要写入Excel,您可以使用COM Interop(尽管this is not recommended for use server-side),或者如果XLSX足够,您可以查看OpenXML SDK(一个简单示例here)。如果您需要XLS,可能需要查看一些第三方库。