How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office ?

How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office ?

Asked on December 13, 2018 in Excel.
Add Comment


  • 3 Answer(s)

    Here, ExcelLibrary. It is a free, open source library:

        This look to be a port of the PHP ExcelWriter that may mentioned above. It will not write to the new .xlsx format yet, but they are working on adding that functionality in.

    It is  easy to use. Plus it has a DataSetHelper that lets you use DataSets and DataTables to easily work with Excel data.

    There are a few known bugs with each library as noted in the comments. In all, EPPlus seems to be the best choice as time goes on. It seems to be more actively updated and documented as well.

    EPPlus has support for Pivot Tables and ExcelLibrary may have some support.

    Here are a couple links for quick reference:

    ExcelLibrary – GNU Lesser GP

    Just you can try this code for ExcelLibrary:

    //Create the data set and table
    DataSet ds = new DataSet("New_DataSet");
    DataTable dt = new DataTable("New_DataTable");
     
    //Set the locale for each
    ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
    dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
     
    //Open a DB connection (in this example with OleDB)
    OleDbConnection con = new OleDbConnection(dbConnectionString);
    con.Open();
     
    //Create a query and fill the data table with the data from the DB
    string sql = "SELECT Whatever FROM MyDBTable;";
    OleDbCommand cmd = new OleDbCommand(sql, con);
    OleDbDataAdapter adptr = new OleDbDataAdapter();
     
    adptr.SelectCommand = cmd;
    adptr.Fill(dt);
    con.Close();
     
    //Add the table to the data set
    ds.Tables.Add(dt);
     
    //Here's the easy part. Create the Excel worksheet from the data set
    ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);
    
    Answered on December 13, 2018.
    Add Comment

    Your solution is here,

        Use this codeplex GitHub project. EPPlus. Started it with the source from ExcelPackage, but today it is a total rewrite. Supports ranges, cell styling, charts, shapes, pictures, namesranges, autofilter and a lot of other stuff.

     

    Answered on December 13, 2018.
    Add Comment

    Follow this answer,

    Can use this with success the following open source projects:

    • ExcelPackage for OOXML formats (Office 2007)
    • NPOI for .XLS format (Office 2003). NPOI 2.0 (Alpha) also supports XLSX.
    Answered on December 13, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.