locked
Read xls document c#.net RRS feed

  • Question

  • Hi ,

         How to read xls document in .Net ?
    Tuesday, September 15, 2009 5:46 AM

Answers

  • Hi,

    Please check sample code in this link:

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

    Regards,
    Jai

    • Marked as answer by Harry Zhu Tuesday, September 22, 2009 1:34 AM
    Tuesday, September 15, 2009 12:38 PM
  • You can use this connection string:


    // filepath is a fullpath to the document
    String ConnString = "Driver= {Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Provider=Microsoft.ACE.OLEDB.12.0;DBQ=" + filepath + ";Extended Properties=Excel 12.0 Xml;HDR=YES"

    // and if the excel data is tabular you can query it using OleDbCommand objects with this query:
    DataSet ds = new DataSet();
    OleDbCommand cmd = OleDbCommand("SELECT * FROM [sheet1$]", ConnString);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);

    // into a dataset/datatable
    da.Fill(ds, "Myexcel");
    Formerly MichaelTx
    • Proposed as answer by JohnGrove Tuesday, September 15, 2009 2:38 PM
    • Marked as answer by Harry Zhu Tuesday, September 22, 2009 1:34 AM
    Tuesday, September 15, 2009 1:29 PM

All replies

  • You could do it by using Microsoft.Office.Interop.Excel

    Nicolai Søndergaard LM Glasfiber A/S
    Tuesday, September 15, 2009 5:53 AM
  • Hi,

    Please check sample code in this link:

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

    Regards,
    Jai

    • Marked as answer by Harry Zhu Tuesday, September 22, 2009 1:34 AM
    Tuesday, September 15, 2009 12:38 PM
  • You can use this connection string:


    // filepath is a fullpath to the document
    String ConnString = "Driver= {Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Provider=Microsoft.ACE.OLEDB.12.0;DBQ=" + filepath + ";Extended Properties=Excel 12.0 Xml;HDR=YES"

    // and if the excel data is tabular you can query it using OleDbCommand objects with this query:
    DataSet ds = new DataSet();
    OleDbCommand cmd = OleDbCommand("SELECT * FROM [sheet1$]", ConnString);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);

    // into a dataset/datatable
    da.Fill(ds, "Myexcel");
    Formerly MichaelTx
    • Proposed as answer by JohnGrove Tuesday, September 15, 2009 2:38 PM
    • Marked as answer by Harry Zhu Tuesday, September 22, 2009 1:34 AM
    Tuesday, September 15, 2009 1:29 PM