c#选择文件并读取EXECL

//选取

try
{
string strexcelPath;
openFileDialog1.Filter = "Excel2003 *xls|*xls|Excel2007 *xlsx|*xlsx";
openFileDialog1.FileName = "";
//1、读取数据
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
else
{
strexcelPath = openFileDialog1.FileName;
}
txt_Path.Text = strexcelPath;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}

//读取

try
{
DataTable dataExcel;
string strexcelPath = txt_Path.Text;
OpExcel opexcel = OpExcelFactory.CreateInstance(strexcelPath);
dataExcel = opexcel.GetData();
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dataExcel;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}

原文地址:https://www.cnblogs.com/xiguanjiandan/p/2729654.html