掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
EXCELの落しこみに関して (ID:10329)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
大変遅くなり申し訳ないです。それとご回答有難うございます。 やり方がおかしいかもしれませんが、どうも思った結果が出力されません。。 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; 大変お手数かけますが、もう一度ご指導よろしくお願い致します。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.