C# 엑셀불러오기
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "Excel파일|*.xl*";
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDlg.FileName;
var connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES""";
using (OleDbConnection con = new OleDbConnection(string.Format(connectionString, openFileDlg.FileName)))
{
con.Open();
if (con.State != ConnectionState.Open)
{
MessageBox.Show("엑셀파일에 연결할 수 없습니다", "에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var worksheets = con.GetSchema("Tables");
string Query = string.Empty;
Query += " select A.* ";
Query += string.Format(" from [{0}] as A ", worksheets.Rows[0]["TABLE_NAME"]);
OleDbCommand cmd = new OleDbCommand(Query, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count < 1)
return;
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}