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.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";

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

myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


Thank you!
Top 10 Contributor
Posts: 6,473
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?

regards
Top 10 Contributor
Posts: 6,473
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.DataBind();
        excelGrid.AutoGenerateColumns = true;

        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=RegainExport.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        excelGrid.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
Top 500 Contributor
Posts: 30
psenechal Posted: Tue Jan 15, 2008 @ 3:42 PM
When I tried this, my Excel file only contained
<div></div>
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
Hi,

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.Load(Page.Server.MapPath(Coverter2ExcelFilePath));
xt.Transform(xmlDataDoc, null, sw);
string fileContent = sw.ToString();
sw.Close();
Page.Response.Clear();
Page.Response.ContentType = "application/vnd.ms-excel";
Page.Response.Charset = "";
Page.Response.AddHeader("content-disposition", "attachment; filename=resultList.xls");
Page.Response.Write(fileContent);
Page.Response.Flush();
Page.Response.End();
copy.Dispose();
}

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.
        page.DesignerInitialize()

        page.Controls.Add(form)
        'form.Controls.Add(GridView1)
        form.Controls.Add(Grid1)

        page.RenderControl(htw)
    
        Response.Clear()
        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]
        Response.Write(sb.ToString())
        Response.[End]()

    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

Hello,


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 >