datagridview中出现2次同样的内容。
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Title = "请选择要导入的Excel文件";
open.Filter = "Excel文件(*.xls)|*.xls|csv文件(*.csv)|*.csv|xlsx文件(*.xlsx)|*.xlsx";
if (open.ShowDialog() == DialogResult.OK)
{
string fileName = open.FileName;
//根据路径打开一个Excel文件并将数据填充到DataSet中
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + fileName + ";Extended Properties ='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "select * from [sheet1$]";
OleDbDataAdapter comm = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
try
{
comm.Fill(ds, "table1");
}
catch (Exception ex)
{
MessageBox.Show("错误信息:" + ex.ToString(), "错误");
}
comm.Fill(ds, "table1");
dataGridView1.DataSource = ds.Tables[0];
}
```![图片说明](https://img-ask.csdn.net/upload/202005/30/1590773127_856212.png)
![图片说明](https://img-ask.csdn.net/upload/202005/30/1590773328_155889.png)