Click here to Skip to main content
15,886,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Al,

My program exported a excel file by using

C#
Response.ContentType = "application/vnd.ms-excel";
after downloading the same when i import it back to my system using Microsoft Excel driver, i got an error.
External table is not in the expected format.


Below is my code :


C#
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=LatestReport.xls");
Response.Charset = "";
this.EnableViewState = false;


using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);

//To Export all pages
gvProduct.AllowPaging = false;
gvProduct.Columns[3].HeaderText = "SKU_CODE";
gvProduct.Columns[5].HeaderText = "SKU_PRICE";
gvProduct.Columns[10].HeaderText = "SKU_QOH";
gvProduct.Columns[16].HeaderText = "ITEM_ATTRIB1";
gvProduct.Columns[18].HeaderText = "PDT_TITLE";
gvProduct.Columns[29].HeaderText = "ECOM_DEPT";

Bind();
gvProduct.Columns[0].Visible = false; 
gvProduct.Columns[1].Visible = true;
gvProduct.Columns[2].Visible = true;
gvProduct.Columns[4].Visible = true;
gvProduct.Columns[6].Visible = true;
gvProduct.Columns[7].Visible = true;
gvProduct.Columns[8].Visible = true;
gvProduct.Columns[9].Visible = true;
gvProduct.Columns[11].Visible = true;
gvProduct.Columns[12].Visible = true;
gvProduct.Columns[13].Visible = true;
gvProduct.Columns[14].Visible = true;
gvProduct.Columns[15].Visible = true;
gvProduct.Columns[17].Visible = true;
gvProduct.Columns[19].Visible = true;
gvProduct.Columns[20].Visible = true;
gvProduct.Columns[21].Visible = true;
gvProduct.Columns[22].Visible = true;
gvProduct.Columns[23].Visible = true;
gvProduct.Columns[24].Visible = true;
gvProduct.Columns[25].Visible = true;
gvProduct.Columns[26].Visible = true;
gvProduct.Columns[27].Visible = true;
gvProduct.Columns[28].Visible = true;
gvProduct.Columns[30].Visible = true;

gvProduct.Columns[31].Visible = false;
gvProduct.Columns[32].Visible = false;
gvProduct.Columns[33].Visible = false;
//gvProduct
gvProduct.Columns[gvProduct.Columns.Count - 1].Visible = false;
gvProduct.RenderControl(hw);

//style to format numbers to string
StringBuilder sBuilder = new StringBuilder();
sBuilder.Append("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'> <head><meta http-equiv='Content-Type' content='text/html;charset=windows-1252'><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>ExportToExcel</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
sBuilder.Append(sw + "</body></html>");
Response.Write(sBuilder.ToString());
Response.End();
}


Does anyone know how to solve it?

Tried a lot searching for the solution but did not get the perfect one. Kindly help or suggest.

Regards,
Posted
Updated 26-Aug-14 20:09pm
v4
Comments
Thilina Chandima 26-Aug-14 6:52am    
can you post your coding to that file
[no name] 26-Aug-14 8:59am    
and check if MS office is installed or not

1 solution

 
Share this answer
 
Comments
Gihan Liyanage 15-Sep-14 6:22am    
Did you got a solution from my support, I can see you have not accepted any answer.If you are ok with this answer plz accept it. Then any user having same problem can identified it has solved the problem..

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