Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I save the file that is returned by Handler and try to open it I get the following message:<b>"The file you trying to open, 'Users.xls', is in a different format than specified by the file extension. Verify that the file is not corrupt and is from a trusted source before opening the file. Do you want to open the file now?"</b>
How to fix this?

Here the code of the handler.

<code>using System.Web;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text;

namespace WebApplication1
{
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string query = "SELECT FirstName, LastName, Email, Phone FROM tblUser WHERE IsSubscribed = 1";
// As QueryDataSql = New QueryDataSql(query)

DataTable dt = new DataTable();
GridView gv = new GridView();

dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Email");
dt.Columns.Add("Phone");


DataRow dr = dt.NewRow();
dr["FirstName"] = "f";
dr["LastName"] = "l";
dr["Email"] = "e";
dr["Phone"] = "p";
dt.Rows.Add(dr);

gv.DataSource = dt;
ExportGridToExcel(gv, "Users.xls", context);
}
private void ExportGridToExcel(GridView grid, string filename, HttpContext context)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.AllowPaging = false;
grid.AllowSorting = false;
grid.DataBind();

context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
context.Response.ContentType = "application/vnd.ms-excel";

context.Response.ContentEncoding = Encoding.Unicode;
context.Response.Charset = string.Empty;
context.Response.BinaryWrite(Encoding.Unicode.GetPreamble());
grid.RenderControl(htw);

context.Response.Write(sw.ToString());

context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}

}
}
</code>
Posted

What is actually producing this error? The web browser?
 
Share this answer
 
Comments
TheAteist 6-Oct-10 16:54pm    
MS-Excel. Every version of Excel(2003-2010)
LloydA111 6-Oct-10 16:58pm    
In that case, some data in the file is telling Excel that it is not the version that it specified by the file extension. Unfortunately, I don't really do much (well actually, anything) to do with MS Office, so I can't help, sorry!
HI you can get more info here for the problem you are facing - http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx[^]
 
Share this answer
 
Comments
TheAteist 6-Oct-10 17:13pm    
Have I understood it correctly: this "bug" is only in 2007 version and will be fixed in future"?
TheAteist 7-Oct-10 9:44am    
I downloaded "DocumentFormat.OpenXml" library, but I still get this message
Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

using DocumentFormat.OpenXml.Spreadsheet;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Workbook book = new Workbook();
Worksheet sheet = new Worksheet();

Cell cell = new Cell();
cell.CellValue = new CellValue("test");

Row row = new Row();
row.Append(cell);
sheet.Append(row);
book.Append(sheet);


Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=test.xls");
Response.ContentType = "application/vnd.ms-excel";
book.Save(Response.OutputStream);
}
}
}

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