private void BtnCalcolaSommaClick(object sender, EventArgs e)
{
const string query = “SELECT SUM(TOTAL) FROM [Foglio1$]”;

using (var myConnection = new OleDbConnection(@”provider=Microsoft.ACE.OLEDB.12.0;data source=E:\demo.XLSx;Extended Properties=Excel 12.0;”))
{
myConnection.Open();

using (myConnection)
{
object result;
using (var myCommand = new OleDbCommand(query, myConnection))
result = myCommand.ExecuteScalar();

if (!result.Equals(DBNull.Value))
{
TextBox1.Text = result.ToString();
}
}
}
}
Reference
http://www.c-sharpcorner.com/code/887/C-Sharp-make-the-sum-of-the-data-of-a-column-of-excel-spreadsheet.aspx

Advertisements