반응형
엑셀파일에 ODBC를 연결하여 엑셀의 데이터를 DataGridView에 표시해 보도록 하겠습니다.
1. 엑셀파일에 ODBC 연결하기
string excelODBCConString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Extended Properties='Excel 8.0;HDR=YES';DBQ=";
string conStr = excelODBCConString + excelFilePath;
OdbcConnection con = new OdbcConnection(conStr);
2. 엑셀의 시트명 얻기
string sheetName = null;
string excelODBCConString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Extended Properties='Excel 8.0;HDR=YES';DBQ=";
string conStr = excelODBCConString + excelFilePath;
try
{
using (OdbcConnection con = new OdbcConnection(conStr))
{
using (OdbcCommand cmd = new OdbcCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetSchema("Tables");
// 첫번째 시트명
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
con.Close();
}
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
3. 엑셀의 데이타 얻기
string excelODBCConString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Extended Properties='Excel 8.0;HDR=YES';DBQ="
string conStr = excelODBCConString + excelFilePath;
try
{
using (OdbcConnection con = new OdbcConnection(conStr))
{
DataTable dt = new DataTable();
string sql = "SELECT * FROM [" + sheetName + "]";
con.Open();
OdbcCommand cmd = new OdbcCommand(sql, con);
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dt);
con.Close();
return dt;
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
4. 전체 소스 및 실행화면
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
namespace ExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenExcel_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
dgvExcel.DataSource = GetDataTableFromExcelODBC(openFileDialog.FileName);
}
private string GetSheetNameFromExcelODBC(string excelFilePath)
{
string sheetName = null;
string excelODBCConString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Extended Properties='Excel 8.0;HDR=YES';DBQ=";
string conStr = excelODBCConString + excelFilePath;
try
{
using (OdbcConnection con = new OdbcConnection(conStr))
{
using (OdbcCommand cmd = new OdbcCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetSchema("Tables");
sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
con.Close();
}
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return sheetName;
}
public DataTable GetDataTableFromExcelODBC(string excelFilePath)
{
string excelODBCConString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Extended Properties='Excel 8.0;HDR=YES';DBQ=";
string conStr = excelODBCConString + excelFilePath;
try
{
using (OdbcConnection con = new OdbcConnection(conStr))
{
DataTable dt = new DataTable();
string sql = "SELECT * FROM [" + GetSheetNameFromExcelODBC(excelFilePath) + "]";
con.Open();
OdbcCommand cmd = new OdbcCommand(sql, con);
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dt);
con.Close();
return dt;
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return null;
}
}
}
반응형
'.Net' 카테고리의 다른 글
C# 개행문자 Replace하기 (1) | 2016.05.31 |
---|---|
C# DateTimePicker 커스텀 포맷(Custom Format) 적용하기 (0) | 2016.02.23 |
C# 엑셀 셀 형식(서식) 정의하기 (0) | 2015.12.15 |
Error : are you missing a using directive or an assembly reference? (1) | 2015.11.26 |
VB.Net 프로젝트에서 참조(Reference)를 Solution Explorer에 표시하기 (0) | 2015.11.18 |
댓글