procedure TfrmJuchu.Button1Click(Sender: TObject);
var
MsExcel : Variant;
MsApplication: Variant;
WBook : Variant;
WSheet : Variant;
i : integer;
begin
//登録されていない場合処理抜ける
if cdsJuchuM.Eof then
begin
ShowMessage('出力対象レコードがありません。');
Exit;
end;
//Excel起動
MsExcel := CreateOleObject('Excel.Application');
MsApplication := MsExcel.Application;
MsApplication.Visible := True;
WBook := MsApplication.WorkBooks.Add ;
WSheet :=WBook.ActiveSheet;
//Excelにタイトル出力
WSheet.Cells[1,3].Value := '受注データ';
WSheet.Cells[1,3].Font.Size := 15;
WSheet.Cells[2,1].Value := '受注番号';
WSheet.Cells[2,2].Value := IntToStr(tblJuchuH.FieldByName('SJJUNO').AsInteger);
WSheet.Cells[2,3].Value := '得意先';
WSheet.Cells[3,3].Value := '納品先';
WSheet.Cells[2,4].Value := IntToStr(tblJuchuH.FieldByName('SJTKCD').AsInteger);
WSheet.Cells[2,5].Value := tblJuchuH.FieldByName('SJTKKJ').AsString;
WSheet.Cells[3,4].Value := IntToStr(tblJuchuH.FieldByName('SJEUSR').AsInteger);
WSheet.Cells[3,5].Value := tblJuchuH.FieldByName('SJEUKJ').AsString;
WSheet.Cells[4,1].Value := '商品名';
WSheet.Cells[4,2].Value := '数量';
WSheet.Cells[4,3].Value := '納期';
WSheet.Cells[4,4].Value := '販売単価';
WSheet.Cells[4,1].Font.Bold := 'True';
WSheet.Cells[4,2].Font.Bold := 'True';
WSheet.Cells[4,3].Font.Bold := 'True';
WSheet.Cells[4,4].Font.Bold := 'True';
//Excelにデータ出力
with cdsJuchuM do
begin
i := 0;
First;
while not eof do
begin
WSheet.Cells[i+5,1].Value := FieldByName('SHOHINNM').AsString;
WSheet.Cells[i+5,2].Value := FieldByName('SURYO').AsInteger;
WSheet.Cells[i+5,3].Value := FieldByName('NOUKI').DisplayText;
WSheet.Cells[i+5,4].Value := FieldByName('TANKA').DisplayText;
Next;
i := i + 1;
end;
Active := False;
end;
//保存の確認を行う
WBook.Saved := False;
//レコード位置を先頭に戻す
cdsJuchuM.First;
end;
|