locked
Export to Excel using .net framework 4.0 RRS feed

  • Question

  • I would like to know whether there are any classes available in .net framework 4.0 for exporting data to excel.

    I am  not looking for any third party libraries.

    Any help will be greatly appreciated.


    Abhinav Galodha

    Monday, June 6, 2011 11:15 AM

Answers

All replies

  • If you are not looking for any third party, this work around might help you.

    http://dotnetguts.blogspot.com/2007/12/export-datagrid-to-excel-in-aspnet.html


    Malayalam SMS Website http://aspspider.info/smsmallu
    Monday, June 6, 2011 12:46 PM
  • Yeap, also have a look at my answer to similar question

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/3cbce986-ea38-4c76-a245-570e2548df74

    Another good example would be

    http://www.kodefuguru.com/post/2010/06/02/DataGridView-Export-to-Excel.aspx

     

    Hope this helps.


    Please mark this as "Answer" or "Vote as helpful" if it has resolved your issue/question/problem.
    Monday, June 6, 2011 12:53 PM
  • Thanks vishvas and Nishath,

    Sorry but i forgot to mention that i am not expecting Microsoft excel to be installed on the machine. So i  will not be able to use any of the Interop solutions i guess.

    @Nshanth :I am looking for general approach for exporting to Excel. The Approach you mentioned is for converting from gridview to excel


    Abhinav Galodha
    Monday, June 6, 2011 1:45 PM
  • There is a Free solution which Export to Excel, Word, PDF without need Microsoft excel,Word, PDF to be installed on the machine.

    http://www.codeproject.com/KB/cs/Excel_PDF_Word_ExportWiz.aspx

    • Proposed as answer by Vishvvas Wednesday, June 8, 2011 6:26 AM
    • Marked as answer by Aspen VJ Wednesday, June 15, 2011 1:26 AM
    Tuesday, June 7, 2011 6:43 AM
  • Agalo,

    Oh!So you need something which would do exporting of data to excel even when excel is not installed. In addition to what NeedleSK has suggested, there are some good component like excelxmlwriter http://www.carlosag.net/Tools/ExcelXmlWriter/

    And visit http://www.aneef.net/2008/07/25/create-excel-sheets-programmatically-with-c-without-installing-excel/ for demonstration of such usage of writer.

    Using xml to synthesize export of data to excel looks an excellent approach to achieve this as there won't be any intrinsic support of installed excel and hence the conformance could be a challenge.

    Hope this helps.


    Please mark this as "Answer" or "Vote as helpful" if it has resolved your issue/question/problem.
    • Proposed as answer by Vishvvas Wednesday, June 8, 2011 6:25 AM
    • Marked as answer by Aspen VJ Wednesday, June 15, 2011 1:27 AM
    Tuesday, June 7, 2011 3:11 PM
  • Hi,

    No. Excel functionality is to specific to be included in .NET Framework. If you are working with XLSX files, then you can manipulate them using System.IO.Compression and System.Xml namespaces because they are basically zipped collection of XML files. But be prepared to study hundred of pages of Open XML specification.

    I think it doesn't worth it. Just use Open XML SDK or some other 3rd party dependency.

    Here is one way to export to Excel in C#:

    // Create new Excel file.
    var excelFile = new ExcelFile();
    
    // Add new worksheet and export data from DataTable into it.
    excelFile.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, true);
    
    // Export to XLS format.
    excelFile.SaveXls(dataTable.TableName + ".xls");

    Thursday, October 11, 2012 11:01 AM
  • I've written a free C#/VB class, to export any DataSet/DataTable/List<> to an Excel .xlsx file using one line of code, using the free OpenXML libraries.

    Full details, with all of the source code and a demo are available here (no registration needed):

    Export to Excel library

    Here's what your code would look like:

    // Step 1: Create a DataSet, and put some sample data in it
    DataSet ds = CreateSampleData();
    
    // Step 2: Create the Excel .xlsx file
    try
    {
        string excelFilename = "C:\\Sample.xlsx";
        CreateExcelFile.CreateExcelDocument(ds, excelFilename);
    }
    catch (Exception ex)
    { 
        MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
        return;
    }

    And this year, I've improved it for ASP.Net devlopers, so you can export, without writing to an external file first, in one line of code:

    // In this example, I have a defined a List of my Employee objects.
    class Employee;
    List<Employee> listOfEmployees = new List<Employee>();
    
    ...
    
    // The following ASP.Net code gets run when I click on my "Export to Excel" button.
    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        // It doesn't get much easier than this...
        CreateExcelFile.CreateExcelDocument(listOfEmployees, "Employees.xlsx", Response);
    }

    Voila.

    Everything you could need to get started, and you are free to modify the code yourself.

    Good luck !

    Mike

    Saturday, November 30, 2013 8:24 AM