Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i m c# developer and new here what i want is i have a data from SQL which was showing in gird-view after filter, so now i want to export that filtered data which was showing in grid-view to excel file but on predefined excel file where i already make a template.

What I have tried:

not tried any thing for now just confused on that how to do that
Posted
Updated 23-Jul-17 20:43pm

I would recommend ClosedXML -
GitHub - ClosedXML/ClosedXML: ClosedXML makes it easier for developers to create Excel 2007+ (.xlsx, .xlsm, etc) files. It provides a nice object oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and VisualBasic.NET.[^]
You can turn a DataTable into an Excel worksheet with some very readable code:

XLWorkbook wb = new XLWorkbook();
DataTable dt = GetDataTableOrWhatever();
wb.Worksheets.Add(dt,"WorksheetName");
 
Share this answer
 
For export to excel here is the simplest code. Through that you can directly download your file in excel format.

first you have take one viewstate
To store your data-table Through Which your going to bind Your Griedview1


e.g.,
Griedview1.DataSource=dt;
Griedview1.BindData();

And Then Write This Line;

ViewState["dtData"]=dt



C#
Private Void ExportExcelFile()
{
       DataTable dt=new DataTable();
       dt=(DataTable)ViewState["dtData"];
       

        GridView gvExp = New GridView();
        gvExp.AutoGenerateColumns = False;

        int counter = 0;
        foreach(DataColumn col In GriedView1.Columns)
        {
            BoundField bf = New BoundField();
            bf.DataField = col.ColumnName;
            bf.HeaderText = col.ColumnName;
            gvExp.Columns.Add(bf);
            counter += 1;
        }

        gvExp.DataSource = dt;
        gvExp.DataBind();

        String FileName = ""
        FileName = "YourFileName.xls";

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" & FileName);
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.Charset = "";
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

        
        System.IO.StringWriter tw = New System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = New System.Web.UI.HtmlTextWriter(tw);

        gvExp.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
}


Hope it helps you.
 
Share this answer
 
Comments
Member 13324837 24-Jul-17 3:07am    
in this way we i can export data to excel but what i want also is to define the datagridview column on excel example Gridview row 1 export to excelsheet column "C1"

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