本文介绍了如何使用C#在ASP.net中将gridview导出到Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我如何将ASP.net中的gridview导出到Excel吗?



我使用这段代码,但是我无法访问我的gridview,因为它是空的



pre $ protected void BtnExport_Click(object sender,EventArgs e)
{
DataTable dtOriginal = new DataTable() ;
dtOriginal =(DataTable)gvRapporten.DataSource; //返回包含数据的表

//创建临时表
DataTable dtTemp = new DataTable();

//创建标题行
dtTemp.Columns.Add(< b> Melder< / b>);
dtTemp.Columns.Add(< b> Onderwerp< / b>);
dtTemp.Columns.Add(< b> Oplosser< / b>);
dtTemp.Columns.Add(< b> Niveau 2< / b>);
DataRow drAddItem;
for(int i = 0; i< dtOriginal.Rows.Count; i ++)
{
drAddItem = dtTemp.NewRow();
drAddItem [0] = dtOriginal.Rows [i] [0] .ToString(); // Melder
drAddItem [1] = dtOriginal.Rows [i] [1] .ToString(); / / Onderwerp
drAddItem [2] = dtOriginal.Rows [i] [2] .ToString(); // Oplosser
drAddItem [3] = dtOriginal.Rows [i] [3] .ToString() ; // Niveau 2

dtTemp.Rows.Add(drAddItem);
}

// Temp Grid
DataGrid dg = new DataGrid();
dg.DataSource = dtTemp;
dg.DataBind();
ExportToExcel(Report.xls,dg);
dg = null;
dg.Dispose();


private void ExportToExcel(string strFileName,DataGrid dg)
{
Response.ClearContent();
Response.AddHeader(content-disposition,attachment; filename =+ strFileName);
Response.ContentType =application / excel;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


解决方案

你可以试试这个吗? / p>

  System.IO.StringWriter sw = new System.IO.StringWriter(); 
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader(content-disposition,attachment; filename =+ strFileName);
Response.ClearContent();

Response.AddHeader(content-disposition,附件);

dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();


Does someone know how I can export a gridview in ASP.net to Excel??

I use this code but I can't access my gridview because it's null

protected void BtnExport_Click(object sender, EventArgs e)
{
    DataTable dtOriginal = new DataTable();
    dtOriginal = (DataTable)gvRapporten.DataSource; //Return Table consisting data

    //Create Tempory Table
    DataTable dtTemp = new DataTable();

    //Creating Header Row
    dtTemp.Columns.Add("<b>Melder</b>");
    dtTemp.Columns.Add("<b>Onderwerp</b>");
    dtTemp.Columns.Add("<b>Oplosser</b>");
    dtTemp.Columns.Add("<b>Niveau 2</b>");
    DataRow drAddItem;
    for (int i = 0; i < dtOriginal.Rows.Count; i++)
    {
        drAddItem = dtTemp.NewRow();
        drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Melder
        drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Onderwerp
        drAddItem[2] = dtOriginal.Rows[i][2].ToString();//Oplosser
        drAddItem[3] = dtOriginal.Rows[i][3].ToString();//Niveau 2

        dtTemp.Rows.Add(drAddItem);
    }

    //Temp Grid
    DataGrid dg = new DataGrid();
    dg.DataSource = dtTemp;
    dg.DataBind();
    ExportToExcel("Report.xls", dg);
    dg = null;
    dg.Dispose();
}

private void ExportToExcel(string strFileName, DataGrid dg)
{
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
    Response.ContentType = "application/excel";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    dg.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
解决方案

can you try this ?

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();

这篇关于如何使用C#在ASP.net中将gridview导出到Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 23:46