ForNext
Only Do What Only You Can Do
26. Excelファイルを読む
VBScript
JScript
Perl
PHP
Python
Ruby
PowerShell
Scala
F#
C
C++
C++Builder
更新日 : 2005.12.13
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")); }
VC++
C#
Java
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2005.12.13
procedure TForm1.Button23Click(Sender: TObject); var cn :Variant;//Connection rs :Variant;//Recordset sql:string; const adOpenDynamic: Integer = 2; adOpenForwardOnly: Integer = 0; adOpenKeyset: Integer = 1; adOpenStatic: Integer = 3; adOpenUnspecified: Integer = -1; adLockBatchOptimistic: Integer = 4; adLockOptimistic: Integer = 3; adLockPessimistic: Integer = 2; adLockReadOnly: Integer = 1; adLockUnspecified: Integer = -1; begin //表示用コントロールをクリア ListBox1.Items.Clear; ListBox2.Items.Clear; cn := CreateOleObject('ADODB.Connection'); //Edit3 に ファイル名を入力 cn.ConnectionString := 'Driver={Microsoft Excel Driver (*.xls)};DBQ=' + Edit3.Text + ';'; cn.Open; //Edit4 に シート名を入力 sql := 'SELECT * FROM [' + Edit4.Text + '$]'; rs := CreateOleObject('ADODB.Recordset'); rs.Source := sql; rs.Activeconnection := cn; rs.CursorType := adOpenForwardOnly; rs.LockType := adLockReadOnly; rs.Open; while not rs.EOF do begin //表示用コントロールに項目内容をセット ListBox1.Items.Add(rs.Fields[0].Value); ListBox2.Items.Add(rs.Fields[1].Value); rs.MoveNext; end; rs.Close; cn.Close; end;