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/archive/2012/10/18/2729654.html

你可能感兴趣的:(c#选择文件并读取EXECL)