Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to download an .xlsx report using the following code.
Error :: Excel cannot open the file ecause the file format and extension is not valid.
My report contents consist of two tabular structure in HTML format.
My data limit will always exceed 1 lakh plus records.
Kindly suggest any alternative for the same.

C#
  HttpContext.Current.Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
           HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
           HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=Reports.xlsx");
           HttpContext.Current.Response.Charset = "utf-8";
           HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
           HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");

HttpContext.Current.Response.Write("<BR><BR><BR>");
           HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri;'> <TR>");


additional information copied from comment below
No im not using Open XML.
I am using HTML table format to create the response in .xlsx format
C#
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri;'> <TR>");
int columnscount = dtHeader.Columns.Count;

for (int j = 0; j < columnscount; j++)
{
   HttpContext.Current.Response.Write("<Td>");
   HttpContext.Current.Response.Write("");
   HttpContext.Current.Response.Write(dtHeader.Columns[j].ColumnName.ToString());
   HttpContext.Current.Response.Write("");
   HttpContext.Current.Response.Write("</Td>");
}

HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in dtHeader.Rows)
{
   HttpContext.Current.Response.Write("<TR>");
   for (int i = 0; i < dtHeader.Columns.Count; i++)
   {
      HttpContext.Current.Response.Write("<Td>");
      HttpContext.Current.Response.Write(row[i].ToString());
      HttpContext.Current.Response.Write("</Td>");
   }

   HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
Posted
Updated 18-Nov-14 4:41am
v2
Comments
Nathan Minier 18-Nov-14 7:06am    
Where are you actually building the Excel filestream? Are you using OpenXML?
Member-515487 18-Nov-14 7:33am    
No im not using Open XML.
I am using HTML table format to create the response in .xlsx format

HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri;'> <TR>");
int columnscount = dtHeader.Columns.Count;

for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(dtHeader.Columns[j].ColumnName.ToString());
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in dtHeader.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < dtHeader.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}

HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
Nathan Minier 18-Nov-14 7:38am    
That's likely your problem, then. The xlsx file format has certain requirements and it is a binary format, the plain-text that you're trying to push down simply is not in the correct format to be interpreted as that file type.

1 solution

I just see you creating a html doc, not an xlsx - So why you wonder about the error message - which is very expressive - wrong format and extension not matching - So the solution should be: either create a valid xlsx or a html, don't you agree?
 
Share this answer
 

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