|
건전맨 님이 쓰신 글 :
: 엑셀 파일을 불러와서 테이블에 집어넣고 그리고 테이블에 있는 내용을 가져옵니다.
: OpenDialog는 잘 되는데 제가 원하는 파일이 아닙니다.
:
: ======>
:
: void __fastcall TMainForm::BtnOpenClick(TObject *Sender)
: {
: OpenDialog1->Title = "Excel 파일 읽어오기";
: if (OpenDialog1->Execute())
: {
: OpenData(OpenDialog1->FileName);
: }
: }
: //---------------------------------------------------------------------------
: void TMainForm::OpenData(AnsiString strPath)
: {
: Table1->Close();
:
: AnsiString SQLCommand1;
:
: if( Query1->Active ) Query1->Active = false;
: Query1->SQL->Clear();
: SQLCommand1 = "DELETE FROM excel";
: Query1->SQL->Add( SQLCommand1 );
: Query1->ExecSQL();
:
: Variant xlApp;
: Variant ttt = OpenDialog1->FileName;
: //Variant로 엑셀 인스턴스를 만든다.
: xlApp = Variant::CreateObject("Excel.Application");
:
: // 엑셀의 워크북 오브젝트 접근
: Variant xlBooks;
: xlBooks = xlApp.OlePropertyGet("Workbooks");
: // 존재하는 엑셀 파일을 연다.
: xlBooks.OleProcedure("Open", "c:\\Book1.xls");
:
: // 어플리케이션 내에서 첫번째 워크북을 얻는다.
: Variant xlBook = xlBooks.OlePropertyGet("Item", 1);
: // 워크시트들을 관리할 수 있는 오브젝트를 얻는다.
: Variant xlSheets = xlBook.OlePropertyGet("Worksheets");
: // 첫번째 워크시트를 얻는다.
: Variant xlSheet = xlSheets.OlePropertyGet("Item", 1);
:
:
: // 셀로 작업할 경우
: Variant VCell, VCell1;
: AnsiString val1, val2, val3;
: VCell = xlSheet.OlePropertyGet("Cells", 1, 1);
: val1 = VCell.OlePropertyGet("Value");
: VCell = xlSheet.OlePropertyGet("Cells", 1, 2);
: val2 = VCell.OlePropertyGet("Value");
: VCell = xlSheet.OlePropertyGet("Cells", 1, 3);
: val3 = VCell.OlePropertyGet("Value");
: int i = 4;
: while((val1.Length() != 0) && (val2.Length() != 0) && (val3.Length() != 0))
: {
:
: //셀좌표(Y, X)인 셀을 얻는다.
: VCell = xlSheet.OlePropertyGet("Cells", i, 1);
: val1 = VCell.OlePropertyGet("Value");
: VCell = xlSheet.OlePropertyGet("Cells", i, 2);
: val2 = VCell.OlePropertyGet("Value");
: VCell = xlSheet.OlePropertyGet("Cells", i, 3);
: val3 = VCell.OlePropertyGet("Value");
: val3 = val3.SubString(1,3) + "-" + val3.SubString(4,3) + "-" + val3.SubString(7,2) + "-" + val3.SubString(9,4);
: if( Query1->Active ) Query1->Active = false;
: Query1->SQL->Clear();
: SQLCommand1 = "INSERT INTO excel ";
: SQLCommand1 = SQLCommand1 + "VALUES ('" + val1 + "','";
: SQLCommand1 = SQLCommand1 + val2 + "','";
: SQLCommand1 = SQLCommand1 + val3 + "') ";
: Query1->SQL->Add( SQLCommand1 );
: Query1->ExecSQL();
: i++;
: }
:
: xlBooks.OleProcedure("Close");
: xlApp.OleProcedure("Quit");
:
: Table1->Open();
: }
:
: 제 생각에는 xlBooks.OleProcedure("Open", "c:\\Book1.xls");
: 여기가 문제인 것 같은데요..
: c:\\Book1.xls 파일은 잘 열립니다.
: 그런데 다른 경로에 있는 파일을 가져와도 c:\\Book1.xls 파일이 열립니다.
: 제가 원하는 경로에 있는 파일을 가져올려면 어떻게 해야 하나요?
: 한꼬맹이님 답변 부탁드립니다.
: 아시는 분 있으시면 답변 부탁드리고요...
안녕하십니까?
한꼬맹이 입니다.
당연히 Book1.xls 파일만 불러오니 그런 결과가 나오게 되죠...
OpenData() 부분을 다음과 같이 바꾸셔야 됩니다.
void TMainForm::OpenData(AnsiString strPath)
{
Table1->Close();
AnsiString SQLCommand1;
if( Query1->Active ) Query1->Active = false;
Query1->SQL->Clear();
SQLCommand1 = "DELETE FROM excel";
Query1->SQL->Add( SQLCommand1 );
Query1->ExecSQL();
Variant xlApp;
// Variant ttt = OpenDialog1->FileName; => 이부분은 참조하지 않으므로 필요 없습니다.
//Variant로 엑셀 인스턴스를 만든다.
xlApp = Variant::CreateObject("Excel.Application");
// 엑셀의 워크북 오브젝트 접근
Variant xlBooks;
xlBooks = xlApp.OlePropertyGet("Workbooks");
// 존재하는 엑셀 파일을 연다.
/*******************************************************************************/
// => 이부분을 아래처럼 수정 xlBooks.OleProcedure("Open", "c:\\Book1.xls");
/* 수정=> */ xlBooks.OleProcedure("Open", strPath);
/*******************************************************************************/
그럼 참고하시길...
|