0

I have a big problem with exporting my table to Excel file format.

Firstly I created code which runs on server and allows me to export data to Excel. Due to the fact that my table is created dynamically from the database there is nothing WITHIN the table at that stage, so no data were exported.

My second approach was targeting the final compiled table on the client side using either javascript or a very nice jQuery plugin called "DataTables" (www.datatables.net). Both of the attempts failed. Javascript seems to be to complex for me, plus it has difficulties running in Firefox, plugin on the other hand requires a very specific table structure which I am afraid I cannot provide.

So, a new idea of mine is: grab the page just after compiling and building it on the server, but before sending it to the browser. Target THE table and source its data using function on server. Finally export data to Excel, and send the page to the browser. Now. Is it possible? And if yes, then how?

I am beginner in programming world so any constructive suggestions and criticism would be highly appreciated. I would not mind any hard code examples ;)

4 Answers 4

1

You can try doing something like this:

protected void btnExport_Click(object sender, EventArgs e)
{       
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

    //if you're exporting a table put the table in a placeholder and render 
    //the placeholder to the text writer here
    grdJobs.RenderControl(oHtmlTextWriter);

    Response.Write(oStringWriter.ToString());
    Response.End();
}
Sign up to request clarification or add additional context in comments.

Comments

0

What you need to do is export your query results in a .CSV file. CSV files can be opened in Excel no problem at all. http://wiki.asp.net/page.aspx/401/export-to-csv-file/ This shows you how to export into a .CSV format.

1 Comment

Thanks for answer. I will research the topic. Unfortunately my table is being build using two loops one within each other and on top of that I have a query which checks the value for each cell. Since it is quite a complex procedure this approach might be quite difficult. Thanks for help once again
0

You're going to get a lot of suggestions instead of answers on this here. My recommendation would be to try the jQuery plugin: table2csv in order to create a more universal file format. But there are ways to target an actual Excel format, like this project.

3 Comments

This table2csv looks very promising!!! Hopefully it will do the job. I'll write if I'll succeed. Cheers. Bartosz
Your code works very well, but there is a file I need at the very end of the process. Is there any chance you can modify your code to enable it to save the file with .csv extension?? Regards. Bartosz
it's not actually my code, though i wish I could take credit for it. Are you attempting option 3.1? It should attempt to save as a .csv if you try it by that method.
0

If you want to export to actual XLS or XLSX instead of just CSV or something that just "opens" in Excel, there are third party tools that can help you with this. One example here:

http://www.officewriter.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.