数据表导出到Excel在Excel文件中显示错误消息 [英] Datatable exporting to Excel showing Error Message in Excel File

查看:713
本文介绍了数据表导出到Excel在Excel文件中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据从数据表导出到Excel工作表在打开时显示此错误。



'。xls'的文件格式和扩展名没有match.The文件可能已损坏或不安全



以下是用于导出到Excel的代码



Datatable dt = BindData();

GridView GridView1 = new GridView();



if(dt.Rows.Count> 0)

{

Response.ClearContent();

Response.Buffer = true;

Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},Requisition.xls));

Response.ContentType = application / ms-excel;

System.IO.StringWriter sw = new System.IO.StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);



GridView1.AllowPaging = false;

GridView1 .DataSource = dt;

GridView1.DataBind();



//将标题行改回白色

GridView1.HeaderRow.Style.Add(background-color,#FFFFFF);

//将stlye应用于gridview标题单元格

for(int i = 0;我< GridView1.HeaderRow.Cells.Count; i ++)

{

GridView1.HeaderRow.Cells [i] .Style.Add(background-color,#507CD1);

}



int j = 1;

//此循环用于根据特定行将stlye应用于单元格

foreach(GridView1.Rows中的GridViewRow gvrow)

{

gvrow.BackColor = System.Drawing.Color.White;

if(j< = GridView1.Rows.Count)

{

if(j%2!= 0)

{

for(int k = 0; k< gvrow.Cells.Count; k ++)

{

gvrow.Cells [k] .Style。添加(背景颜色,#EFF3FB);

}

}

}

j ++;

}



GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();



}

After Exporting the Data from Datatable to Excel sheet shows this Error while Opening.

The file format and extension of '.xls' don't match.The file could be corrupted or unsafe

Below is the code used to Export to Excel

Datatable dt=BindData();
GridView GridView1=new GridView();

if (dt.Rows.Count > 0)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Requisition.xls"));
Response.ContentType = "application/ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}

int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in GridView1.Rows)
{
gvrow.BackColor =System.Drawing.Color.White;
if (j <= GridView1.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}

GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

}

推荐答案

您的代码未创建Excel文件。该代码仅适用于写入已安装Excel的客户端,它将在Excel而不是浏览器中显示数据。它只是冒烟,你没有创建格式正确的Excel文件,要做到这一点,你需要使用支持它的库,如Open XML SDK,Aspose,Excel ODBC驱动程序,没有怀疑他人。
Your code isn't creating an Excel file. That code is only good for writing to a client that has Excel installed and it will show the data in Excel rather than the browser. It's just "smoke and mirrors" though, you aren't creating a properly formatted Excel file, to do that you'll need to use a library that supports this such as Open XML SDK, Aspose, the Excel ODBC drivers and there are no doubt others.


这篇关于数据表导出到Excel在Excel文件中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆