C++Builder Personal で ADO

Excelファイルを読む


void __fastcall TForm1::Button23Click(TObject *Sender)
{
const int adOpenForwardOnly = 0;
const int adLockReadOnly = 1;

//表示用コントロールをクリア
ListBox1->Items->Clear();
ListBox2->Items->Clear();

Variant cn = Variant::CreateObject("ADODB.Connection");

//Edit3 に ファイル名を入力
cn.Exec(PropertySet("ConnectionString")<<"Driver={Microsoft Excel Driver (*.xls)};DBQ=" + Edit3->Text + ";");
cn.Exec(Procedure("Open"));

//Edit4 に シート名を入力
AnsiString sql = "SELECT * FROM [" + Edit4->Text + "$]";

Variant rs = Variant::CreateObject("ADODB.Recordset");
rs.Exec(PropertySet("Source")<<sql);
rs.Exec(PropertySet("Activeconnection")<<cn);
rs.Exec(PropertySet("CursorType")<<adOpenForwardOnly);
rs.Exec(PropertySet("LockType")<<adLockReadOnly);

rs.Exec(Procedure("Open"));
while (!rs.Exec(PropertyGet("EOF")))
{
//表示用コントロールに項目内容をセット
Variant fd = rs.Exec(PropertyGet("Fields")<<0);
ListBox1->Items->Add(fd.Exec(PropertyGet("Value")));

fd = rs.Exec(PropertyGet("Fields")<<1);
ListBox2->Items->Add(fd.Exec(PropertyGet("Value")));

rs.Exec(Function("MoveNext"));
}
rs.Exec(Procedure("Close"));

cn.Exec(Procedure("Close"));
}