本文介绍了通过asp.net c#导出的excel文件中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码将GridView数据导出为ex​​cel,代码工作正常,但Excel文件不支持某些操作;如果我正在打印此文件中的标签,则显示错误:外部表格不符合预期格式

怎么办???

有没有办法简单地导出Excel或没有任何格式,因为excel具有与GridView相同的格式...



我试过的:



  protected   void  btnExcel1_Click( object  sender,ImageClickEventArgs e)
{

Response.ClearContent();
Response.Buffer = true ;
Response.AddHeader( content-disposition string .Format( attachment; filename = {0} ExcelSheet.xls));
Response.ContentType = application / ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false ;
// 将标题行更改回白色
GridView1.HeaderRow。 Style.Add( background-color #FFFFFF);
// 将stlye应用于gridview标题单元格
for int i = 0 ; i < GridView1.HeaderRow.Cells.Count; i ++)
{
GridView1.HeaderRow.Cells [i] .Style.Add( background-color #507CD1\" );
}
int j = 1 ;
// 此循环用于根据特定行将stlye应用于单元格
foreach (GridViewRow gvrow in GridView1.Rows)
{
gvrow.BackColor = 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();
}
解决方案



I'm using this code to export GridView data to excel, code is working fine but Excel file is not supporting some operations; If I'm printing labels from this File it is showing "Error: External table is not in expected format"
What to do???
Is there any way to export Excel in simply or without any formatting as the excel has same formatting as of GridView...

What I have tried:

protected void btnExcel1_Click(object sender, ImageClickEventArgs e)
    {

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ExcelSheet.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        //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 = 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();
    }
解决方案



这篇关于通过asp.net c#导出的excel文件中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 23:47
查看更多