如何在按钮上单击事件阅读Excel文件

问题描述:

请帮助我如何在单击按钮时打开excel表。对于连接字符串,为什么我们提供这样的解释我关于连接字符串Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ fileName +;扩展属性= \Excel 8.0; HDR = YES; \

kindly help how do i open a excel sheet on clicking a button.and for connection string why we are providing like this explain me about the connection string "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"

如果您正在寻找从excel文件导入数据并将其显示到datagridview /保存到数据库然后你可以尝试





if u r looking for importing data from excel file and show it to datagridview /save to database then u may try


DialogResult dr= openFileDialog.ShowDialog();
          if (dr == DialogResult.OK && openFileDialog.CheckFileExists )
          {
              string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", openFileDialog.FileName);
              string query = String.Format("select * from [{0}


,Sheet1);
OleDbDataAdapter dataAdapter = new OleD bDataAdapter(query,connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dgvStudents.DataSource = dataSet.Tables [0];
}
", "Sheet1"); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); dgvStudents.DataSource = dataSet.Tables[0]; }