Exporting ComponentArt Grid into Excel

This post has 18 replies

Not Ranked
Posts: 11
apollogroup Posted: Tue Dec 4, 2007 @ 4:41 PM
The below code works fine for MS DataGrid, But does not work with ComponentArt Grid (The excel file opens with no data ). Any one has sample code for exporting ComponentArt Grid into Excel or Word?

Code which I used :

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


Thank you!
Top 10 Contributor
Posts: 6,478
hwan Posted: Fri Dec 7, 2007 @ 2:56 PM
You cannot export the Web.UI Grid directly to Excel. Instead, you should convert its data into another form (such as the ASP DataGrid) and export that instead. The reason behind this is that the Web.UI Grid's structure is build dynamically on the client -- it's passed from server to client as a bunch of XML and array data, which is converted into the tables and divs that you see in the window.
Not Ranked
Posts: 11
apollogroup Posted: Tue Dec 11, 2007 @ 12:36 PM
Thank you for your reply

I will try to get the data from the ComponentArt into another form and try exporting it,

In the mean time, Can you send me if you have have any sample code to convert the ComponentArt Grid to another form?

Top 10 Contributor
Posts: 6,478
hwan Posted: Wed Dec 19, 2007 @ 3:27 PM
I suppose one way would be to loop through the Grid's Items collection, and adding the data cell by cell, row by row. Or, if the changes are already saved to a DataSet/DataTable or the like, you can bind the GridView to that.

Another option would be to set ClientTarget=DownLevel, which will cause the Web.UI Grid to render itself as a plain HTML table.
Not Ranked
Posts: 11
jishy Posted: Sun Jan 13, 2008 @ 12:51 AM
Hey, I use this for my grid export. I use EntitySpaces to manage my business objects. Just add that to a button or something as an onClick event. Cool thing is it doesn't make your page go blank or whatever.

    public void ExportDataSetToExcel()
        object theColl = gridEmployee.DataSource; //just set this as your grid's datasource.
        GridView excelGrid = new GridView();
        excelGrid.DataSource = theColl;
        excelGrid.ID = "Export";
        excelGrid.AutoGenerateColumns = true;

        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=RegainExport.xls");
        Response.Charset = "";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Top 500 Contributor
Posts: 30
psenechal Posted: Tue Jan 15, 2008 @ 3:42 PM
When I tried this, my Excel file only contained
Not Ranked
Posts: 11
jishy Posted: Tue Jan 15, 2008 @ 6:10 PM
I guess it would all depend on your datasource and how it is binded. Like I said, I use an EntitySpaces collection as my datasource so it has all wonderful ways of binding.
Not Ranked
Posts: 5
Andreas Schlegel Posted: Mon Feb 25, 2008 @ 1:49 AM

this works fine for me (only with DataSet as DataSource):

public void ExcelExport()
DataSet data = (DataSet)Grid1.DataSource;
DataSet copy = data.Copy();
XmlDataDocument xmlDataDoc = new XmlDataDocument(copy);
XslCompiledTransform xt = new XslCompiledTransform();
StringWriter sw = new StringWriter();
xt.Transform(xmlDataDoc, null, sw);
string fileContent = sw.ToString();
Page.Response.ContentType = "application/vnd.ms-excel";
Page.Response.Charset = "";
Page.Response.AddHeader("content-disposition", "attachment; filename=resultList.xls");

I hope this helps.
Not Ranked
Posts: 2
andynetonline Posted: Thu Oct 23, 2008 @ 1:26 PM
Friends, I need a form of art component exporting grid to excel, thanks
Not Ranked
Posts: 2
andynetonline Posted: Thu Oct 23, 2008 @ 1:26 PM
email: andrespizarro@live.cl, thanks
Not Ranked
Posts: 8
Dicloniusjj Posted: Fri Dec 19, 2008 @ 11:51 AM
I use this code for export the gridview to excel:

 Public Sub ExportDataSetToExcel()

        Dim sb As New StringBuilder()
        Dim sw As New System.IO.StringWriter(sb)
        Dim htw As New HtmlTextWriter(sw)

        Dim page As New Page()
        Dim form As New HtmlForm()

        'GridView1.EnableViewState = False
        Grid1.EnableViewState = False

        ' Deshabilitar la validación de eventos, sólo asp.net 2
        page.EnableEventValidation = False

        ' Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.


        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=SIGA-SESIONES.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.[Default]

    End Sub

But the problem is this code does not work with the ComponentArt grid, I not expert in ASP but i hope any person more expert found the solition.
Not Ranked
Posts: 16
guptadeepika Posted: Wed Dec 21, 2011 @ 2:38 AM
To jishy,
 I have done the exactly what you have posted .It's working fine on the load but when i am doing filtering and sorting it doesn't export the current data of grid .It's export the old data when initially grid has loaded.

Please update it..It's really urgent.
Not Ranked
Posts: 16
guptadeepika Posted: Wed Dec 21, 2011 @ 2:43 AM
To Andreas Schlegel,

I am getting an error on "
xt.Load(Page.Server.MapPath(Coverter2ExcelFilePath));"  error is Coverter2ExcelFilePath does not exist in a current context.

Please resolve it soon.

do you know any updated functionality for export to pdf from asp.net Web.UI ajax.?

Thanks in advance.
Top 10 Contributor
Posts: 908
ajay Posted: Wed Dec 21, 2011 @ 4:53 AM


Please find the attached sample application to export Grid data to pdf (Webform1.aspx) and to excel (Webform2.aspx).

Hope it helps you.


Thank you.

Ajay Mishra

Not Ranked
Posts: 16
guptadeepika Posted: Wed Dec 21, 2011 @ 10:56 PM
To Ajay,

Thanks for the reply. In your attachment on webform2.aspx i am getting an error on BuildGrid() function cozi need to pass some parameter. i mean my grid function is something like BuildGrid(string name, string loc, datetime date). so i am getting error on the line  "ExportToExcel(buildGrid(), 0, Response, "MySheet-" + System.DateTime.Today.ToString("MM-dd-yyyy"));" .when i am passing parameter then it started to give error. please resolve it.

Is there any other way for export to pdf because i cannot install itextsharp dll and i want codes in C# coz i am not familiar with vb.

Page 1 of 2 (19 items) 1 2 Next >