通过asp.net c#导出的excel文件中的问题 [英] Problem in excel file exported through asp.net c#

查看:73
本文介绍了通过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();
}

解决方案

请尝试以下代码



private void ExportGridToExcel()

{

Response.Clear();

Response.Buffer = true;

Response.ClearContent();

Response.ClearHeaders();

Response.Charset ="" ;;

string FileName =" Vithal" + DateTime.Now +" .xls" ;;

StringWriter strwritter = new StringWriter();

HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType =" application / vnd.ms-excel" ;;

Response.AddHeader(" Content- Disposition"," attachment; filename =" + FileName);

GridView1.GridLines = GridLines.Both;

GridView1.HeaderStyle.Font.Bold = true;

GridView1.RenderControl(htmltextwrtter);

Response.Write(strwritter.ToString());

Response.End();



}

protected void btnExcel1_Click(object sender,ImageClickEventArgs e)

{

ExportGridToExcel();

}


请参考

使用C#和VB.Net将格式导出GridView到ASP.Net中的Excel [ ^ ]

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();
    }

解决方案

Please try below code

private void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName ="Vithal"+DateTime.Now+".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType ="application/vnd.ms-excel";
Response.AddHeader("Content- Disposition","attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();

}
protected void btnExcel1_Click(object sender, ImageClickEventArgs e)
{
ExportGridToExcel();
}


Please refer
Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net[^]


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

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