분노의 챔질
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();
}
'Programming > C#' 카테고리의 다른 글
[C#] 간단한 UserControl (0) | 2010.03.05 |
---|---|
[C#] IP주소 알아내기 (0) | 2010.03.05 |
C# 간단한 메일보내기 (0) | 2010.02.25 |
C# LINQ 쿼리식 (0) | 2010.02.24 |
C# XML 만들기, 읽기 (0) | 2010.02.23 |