[ 1 ]
==> 추가 사항 .
using System.Data.OleDb;using Microsoft.Office.Interop;
[ 2 ]
== > 전역 변수 선언 ( MS- ExCel 버전 별로 코딩방식이 달라 두개의 변수 선언 1. Excel 97_2003, Excel 2007 용 ))
private const string ConnectStrFrm_Excel97_2003 =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=\"{0}\";" +
"Mode=ReadWrite|Share Deny None;" +
"Extended Properties='Excel 8.0; HDR={1}; IMEX={2}';" +
"Persist Security Info=False";
// 확장명 XLSX (Excel 2007 이상용)
private const string ConnectStrFrm_Excel =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=\"{0}\";" +
"Mode=ReadWrite|Share Deny None;" +
"Extended Properties='Excel 12.0; HDR={1}; IMEX={2}';" +
"Persist Security Info=False";
[ 3 ]
--> 본문
string Source = string.Empty;
string ConnStr = string.Empty;
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo
System.IO.FileInfo[] fi = di.GetFiles("검정백신정보*"); <-- 특정 문자로 시작하거 찾기
if (fi.Length == 0)
{
MessageBox.Show("파일이없습니다.");
return;
}
else
FileName = fi[0].Name.ToString();
FileName = System.Windows.Forms.Application.StartupPath +"\\Data\\"+ FileName;
int ExcelType = ExcelFileType(FileName); <-- Excel 버전 변환 부분
switch (ExcelType)
{
case (-2): throw new Exception(Source + "의 형식검사중 오류가 발생하였습니다.");
case (-1): throw new Exception(Source + "은 엑셀 파일형식이 아닙니다.");
case (0):
ConnStr = string.Format(ConnectStrFrm_Excel97_2003, Source, "True", "1");
break;
case (1):
ConnStr = string.Format(ConnectStrFrm_Excel, Source, "True", "1");
break;
}
OleDbConnection excelConn = null;
string xlsfilename;
DataTable excelTable = new DataTable();
DataSet exceIDs = new DataSet();
DataRow lastDr = null;
excelTable.Columns.Add("SEQNO", typeof(string));
excelTable.Columns.Add("LOTNO", typeof(string));
excelTable.Columns.Add("VACODE", typeof(string));
excelTable.Columns.Add("VENDORCODE", typeof(string));
excelTable.Columns.Add("LOPPERID", typeof(string));
excelTable.Columns.Add("VANAME", typeof(string));
excelTable.Columns.Add("VENDORNAME", typeof(string));
excelTable.Columns.Add("UNIT", typeof(string));
excelTable.Columns.Add("VCNCOD", typeof(string));
try
{
string strCon = ConnStr;
excelConn = new OleDbConnection(strCon);
excelConn.Open();
string excelSql = @"select * from [Sheet1$]";
OleDbDataAdapter excelAdapter = new OleDbDataAdapter(excelSql, excelConn);
excelAdapter.Fill(exceIDs);
excelTable = exceIDs.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show("파일 가져오기 실패 :" + ex.Message);
}
DataRow[] drs = excelTable.Select("SEQNO > '0'");
DataSet ds = new DataSet();
ds.Merge(drs);
DataTable dt1 = null;
if (ds.Tables.Count > 0)
{
dt1 = ds.Tables[0].Copy();
}
if (dt1 != null && dt1.Rows.Count > 0)
{
_controller.InsertVaccMstExcelData(dt1);
this.ShowInstanceMessage("HIS.MD", "제조번호 갱신이 완료되었습니다. ", 2);
}
else
{
this.ShowInstanceMessage("HIS.MD", "갱신할 제조번호가 없습니다. ", 2);
}
}
catch (Exception ex)
{
if (ex.Message.StartsWith("제한 시간이 만료되었습니다.")
|| ex.Message.StartsWith("Timeout이 만료되었습니다.")
|| ex.Message.StartsWith("Timeout expired."))
{
MessageBoxViewer.ShowAlert(this, "HIS.Common", "COMMON_DB_TIMEOUT");
}
else if (ex.Message.StartsWith("XX_") || ex.Message.StartsWith("MD"))
{
MessageBoxViewer.ShowAlert(this, "HIS.MD", ex.Message);
}
else if (ex.Message.StartsWith("HP"))
{
MessageBoxViewer.ShowAlert(this, "HIS.HP", ex.Message);
}
else
{
MessageBoxViewer.ShowError(this, "HIS.MD", ex.Message, ex, "VaccPatMgtDF", "CodeDataLoad", "");
}
}
}
// 버전 체크 메서드
public static int ExcelFileType(string XlsFile)
{
byte[,] ExcelHeader = {
{ 0xD0, 0xCF, 0x11, 0xE0, 0xA1 }, // XLS File Header
{ 0x50, 0x4B, 0x03, 0x04, 0x14 } // XLSX File Header
};
// result -2=error, -1=not excel , 0=xls , 1=xlsx
int result = -1;
FileInfo FI = new FileInfo(XlsFile);
FileStream FS = FI.Open(FileMode.Open);
try
{
byte[] FH = new byte[5];
FS.Read(FH, 0, 5);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
if (FH[j] != ExcelHeader[i, j]) break;
else if (j == 4) result = i;
}
if (result >= 0) break;
}
}
catch
{
result = (-2);
//throw e;
}
finally
{
FS.Close();
}
return result;
}