お尋ねします。
Queryを使ってExcelにエクスポートするPGを組んでいす。
小計も一緒にエクスポートをしたいと思いますが、どもう上手く行きません。
Delphi上で、どのような処理をすれば、Excelに上手くエクスポートが
できるのか教えていただきたいと思います。
例
1 a 2 1000
1 b 1 1000
小計 3 2000
2 c 3 500
2 d 4 1000
小計 7 1500
合計 10 3500
よろしくお願いします。
シャチさん。
現在はどのようにしているのですか?
漠然としすぎて、どう説明すればいいのか解りません。
begin
WITH ExcelApplication1 DO
begin
Connect;
Visible[0] :=false;
Workbooks.Add(EmptyParam,0);
ExcelWorksheet1.ConnectTo(ActiveWorkbook.Activesheet as _worksheet);
begin
WITH Excelworksheet1 DO
Screen.Cursor := crHourGlass;
Query1.First;
FOR i := 1 TO Query1.RecordCount DO
begin
FOR j := 1 to Query1.Fields.Count DO
begin
Cells.Item[i+4,j].Value:=Query1.Fields[j-1].AsString;
end;
Query1.Next;
end;
begin
WITH Excelworksheet1 DO
begin
WITH Pagesetup DO
end;
end;
DisplayAlerts[0] := false;
ActiveWorkbook.SaveAs(SaveDialog1.FileName,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,0,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,0);
ActiveWorkbook.Close(False,EmptyParam,False,0);
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
Excelworksheet1.Disconnect;
end;
end;
end;
end;
end;
省略させていただきましたが、このような文法です。
ちなみに、総合計をQUERYのsumを使って付け加えております。
よろしくお願いします。
いろんな方法がありますが、
例えば
var
shokei,gokei:array[1..2] of Integer; //小計と合計
aRow:Integer; //行位置
bunrui:String;
・・・・・・・・・・・・・・・・・・・・・・・・・・・
//追加
gokei[1] := 0;
gokei[2] := 0;
aRow := 5;
bunrui := '';
//ここまで
FOR i := 1 TO Query1.RecordCount DO
//RecordCountは保証されていないRDBMSもあるので
//while Not Query1.Eofの方がお勧め
begin
//追加
if (bunrui<>Query1.Fields[0].AsString) then
begin
if bunrui <> '' then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
bunrui := Query1.Fields[0].AsString;
shokei[1] := 0;
shokei[2] := 0;
end else
FOR j := 1 to Query1.Fields.Count DO
begin
Cells.Item[aRow,j].Value:=Query1.Fields[j-1].AsString;
end;
shokei[1] := shokei[1] + Query1.Fields[2].AsInteger;
//AsIntegerはfieldの属性で変更して下さい。
shokei[2] := shokei[2] + Query1.Fields[3].AsInteger;
gokei[1] := gokei[1] + Query1.Fields[2].AsInteger;
gokei[2] := gokei[2] + Query1.Fields[3].AsInteger;
Query1.Next;
Inc(aRow);
end;
if (shokei[1] > 0) or (shokei[2] > 0) then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
Cells.Item[aRow,1].Value:='合計';
Cells.Item[aRow,3].Value:=IntToStr(gokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(gokei[2]);
他にもSQLで小計・合計をすませておき、
プログラムでは取り出しだけということも出来ます。
大変遅くなり申し訳ないです。それとご回答有難うございます。
やり方がおかしいかもしれませんが、どうも思った結果が出力されません。。
procedure TForm1.Button1Click(Sender: TObject);
var
j,i:Integer;
shokei,gokei:array[1..2] of Integer; //小計と合計
aRow:Integer; //行位置
bunrui:String;
begin
query1.Close;
query1.SQL.Clear;
query1.SQL.Add('select * from AA order by FILE');
query1.Open;
begin
IF Savedialog1.Execute THEN
begin
WITH ExcelApplication1 DO
begin
Connect;
Visible[0] :=false;
Workbooks.Add(EmptyParam,0); ExcelWorksheet1.ConnectTo(ActiveWorkbook.Activesheet as _worksheet);
begin
WITH Excelworksheet1 DO
Query1.First;
//追加
gokei[1] := 0;
gokei[2] := 0;
aRow := 5;
bunrui := '';
//ここまで
FOR i := 1 TO Query1.RecordCount DO
//RecordCountは保証されていないRDBMSもあるので
//while Not Query1.Eofの方がお勧め
begin
//追加
if (bunrui<>Query1.Fields[0].AsString) then
begin
if bunrui <> '' then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
bunrui := Query1.Fields[0].AsString;
shokei[1] := 0;
shokei[2] := 0;
end else
FOR j := 1 to Query1.Fields.Count DO
begin
Cells.Item[aRow,j].Value:=Query1.Fields[j-1].AsString;
end;
shokei[1] := shokei[1] + Query1.Fields[2].AsInteger;
//AsIntegerはfieldの属性で変更して下さい。
shokei[2] := shokei[2] + Query1.Fields[3].AsInteger;
gokei[1] := gokei[1] + Query1.Fields[2].AsInteger;
gokei[2] := gokei[2] + Query1.Fields[3].AsInteger;
Query1.Next;
Inc(aRow);
end;
if (shokei[1] > 0) or (shokei[2] > 0) then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
Cells.Item[aRow,1].Value:='合計';
Cells.Item[aRow,3].Value:=IntToStr(gokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(gokei[2]);
FOR j := 1 to Query1.Fields.Count DO
begin
// Cells.Item[i,j].Value:=Query1.Fields[j-1].AsString;
end;
Query1.Next;
end;
begin
WITH Excelworksheet1 DO
end;
DisplayAlerts[0] := false; //上書き確認のダイアログを表示させない
ActiveWorkbook.SaveAs(SaveDialog1.FileName,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,0,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,0);
ActiveWorkbook.Close(False,EmptyParam,False,0);
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
Excelworksheet1.Disconnect;
end;
end;
end;
end;
大変お手数かけますが、もう一度ご指導よろしくお願い致します。
表示している結果と思った結果がどのように違っているのでしょうか?
本当に申し訳ないです。
結果としてはこのようにですのですが
19 1 80 60
19 1 1000 80
19 1 10 15
19 1 50 550
19 11 20 1000
小計 2160 1715
↓
19 1 10 100 ←ここの行が抜けてしまいます。
--------------------------------
19 1 80 60
19 1 1000 80
19 1 10 15
19 1 50 550
19 11 20 1000
小計 2160 1715
できれば19と数値が入った場合一行のみ表示ってできるのですか?
19 1 10 100
1 80 60
1 1000 80
1 10 15
1 50 550
11 20 1000
小計 2160 1715
このような感じです。よろしくお願いします。
一カ所間違えていました。
最初の行が出力されていませんでした。
--------------------------------−
FOR i := 1 TO Query1.RecordCount DO
//while Not Query1.Eofの方がお勧め
begin
//追加
if (bunrui<>Query1.Fields[0].AsString) then
begin
if bunrui <> '' then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
bunrui := Query1.Fields[0].AsString;
//これを追加します。
Cells.Item[aRow,1].Value := Query1.Fields[0].AsString;
shokei[1] := 0;
shokei[2] := 0;
//end else ここを修正してください。
end; //これにしてください。
FOR j := 1 to Query1.Fields.Count DO
begin
Cells.Item[aRow,j + 1].Value := Query1.Fields[j].AsString;
end;
shokei[1] := shokei[1] + Query1.Fields[2].AsInteger;
shokei[2] := shokei[2] + Query1.Fields[3].AsInteger;
gokei[1] := gokei[1] + Query1.Fields[2].AsInteger;
gokei[2] := gokei[2] + Query1.Fields[3].AsInteger;
Query1.Next;
Inc(aRow);
end;
if (shokei[1] > 0) or (shokei[2] > 0) then
begin
Cells.Item[aRow,1].Value:='小計';
Cells.Item[aRow,3].Value:=IntToStr(shokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(shokei[2]);
Inc(aRow,2);
end;
Cells.Item[aRow,1].Value:='合計';
Cells.Item[aRow,3].Value:=IntToStr(gokei[1]);
Cells.Item[aRow,4].Value:=IntToStr(gokei[2]);
--------------------------------
でいかがでしょうか?
HOtaさん
この度は誠に有難う御座いました。お蔭様で思い描いた事ができました。
これからはもう少しスキルアップを目指しがんばっていきますので
、また何かありましたらご指導よろしくお願いします。
ツイート | ![]() |