The content type for .xlsx files is:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Or use this:
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");
For Excel 2007 and above the MIME type differs
Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-disposition", "attachment; filename=myfile.xlsx");
Or if you are trying to read the file then try this:
DataSet objds = new DataSet();
string ConnStr = "";
if (FileExtension == ".xlsx")
{
ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\";";
}
else
{
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";";
}
OleDbCommand selectCommand = new OleDbCommand();
OleDbConnection connection = new OleDbConnection();
OleDbDataAdapter adapter = new OleDbDataAdapter();
connection.ConnectionString = ConnStr;
string strSQL = "SELECT * FROM [Sheet1$]";
if (connection.State != ConnectionState.Open)
connection.Open();
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(objds);
connection.Close();
All the best.
--Amit