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);
}

posted on 2012-10-18 15:56  习惯简单  阅读(1086)  评论(0编辑  收藏  举报

导航