private DataTable openXLSX(string filename)

{

    OleDbConnection connection = null;

    OleDbCommand command = null;

    OleDbDataAdapter adapter = null;

    DataSet dataset = new DataSet();

    try

    {

        connection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", filename));

        connection.Open();

 

        DataTable sheetsName = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

 

        string firstSheetName = sheetsName.Rows[0][2].ToString();

 

        command = new OleDbCommand(string.Format("SELECT * FROM [{0}]",firstSheetName),  connection);

        //command.CommandType = CommandType.Text;

        adapter = new OleDbDataAdapter(command);

        adapter.Fill(dataset);

    }

    catch (Exception exception)

    {

        MessageBox.Show(exception.ToString());

    }

    finally

    {

        if (connection.State == ConnectionState.Open)

            connection.Close();

        connection.Dispose();

        command.Dispose();

        adapter.Dispose();

    }

    return dataset.Tables[0];

}

 

//dataGridView1.DataSource = dt;

//dataGridView1.Update();