0

I'm using the below code and I'm specifying what type of application it is. However, when prompted to open the application the browser does not know what type of file it is. How can I make the browser already want to open it as an excel?

Any help is appreciated

public static void ExportToSpreadsheet(DataTable table, string name)
{
    HttpContext context = HttpContext.Current;
    context.Response.Clear();
    foreach (DataColumn column in table.Columns)
    {
        context.Response.Write(column.ColumnName + "\t");
    }
    context.Response.Write(Environment.NewLine);
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < table.Columns.Count; i++)
        {
            context.Response.Write(row[i].ToString() + "\t");
        }
        context.Response.Write(Environment.NewLine);
    }
    context.Response.ContentType = "application/ms-excel";
    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name+ ".xls");
    context.Response.End();
}

1 Answers1

0

try:

Response.ContentType = "application/vnd.ms-excel";

or for xlsx

Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Pleun
  • 8,856
  • 2
  • 30
  • 50