using System.Data.OleDb;
添加一个button控件,一个textBox控件,用于显示选择路径
private void loadxls()
{
String fileName = textBox1.Text;
String connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Members.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
string strSQL = "SELECT \* FROM \[Sheet1$\]";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open(); // This code will open excel file.
OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
// create data table
DataTable dTable = new DataTable();
dataAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
dTable.Dispose();
dataAdapter.Dispose();
dbCommand.Dispose();
excelConnection.Close();
excelConnection.Dispose();
}
private void button1\_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel文件(\*.xls)|\*.xls";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
loadxls();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章