procedure TForm1.btnPrintClick(Sender: TObject);var Lv_ExcelApp, Lv_ExcelWkBook, Lv_ExcelWkSheet: Variant; Lv_i, Lv_j, Lv_z, Lv_Cnt, Lv_RowStart: Integer ; Lv_Sum: Real; Lv_MaxMonth, Lv_MaxMonthR, Lv_MaxMonthL, Lv_CurMonth: string ; Function IntToYearMonth() function IntToYearMonth(AYearMonth: Integer): String; var Lv_Year, Lv_Month: Integer; begin Lv_Year := StrToInt(Copy(IntToStr(AYearMonth),1,4)); Lv_Month := StrToInt(Copy(IntToStr(AYearMonth),5,2)); if Lv_Month>12 then begin Lv_Year := Lv_Year + Lv_Month div 12; if (Lv_Month mod 12) >= 10 then Result := IntToStr(Lv_Year) + IntToStr(Lv_Month mod 12) else Result := IntToStr(Lv_Year) + '0' + IntToStr(Lv_Month mod 12); end else Result := IntToStr(AYearMonth); end; Function IntColToCharCol()function IntColToCharCol(AIntCol: Integer): string; const Lv_IntChar: array[0..25,0..1] of string=(('1','A'),('2','B'),('3','C'),('4','D'),('5','E'),('6','F'),('7','G'),('8','H'),('9','I'),('10','J'),('11','K'),('12','L'),('13','M'),('14','N'),('15','O'), ('16','P'),('17','Q'),('18','R'),('19','S'),('20','T'),('21','U'),('22','V'),('23','W'),('24','X'),('25','Y'),('26','Z')); var Lv_Index: Integer; begin for Lv_Index := 0 to 25 do begin if IntToStr(AIntCol) = Lv_IntChar[Lv_Index][0] then begin Result := Lv_IntChar[Lv_Index][1]; Break; end; end; end;begin PageControl.ActivePage := QueryPage2; Lv_ExcelApp := CreateOleObject('Excel.Application'); Lv_ExcelWkBook := Lv_ExcelApp.WorkBooks.Add; Lv_ExcelWkSheet := Lv_ExcelWkBook.WorkSheets.Item[1]; with DM1.Query1 do begin Close; Sql.Text := 'select max(months) months from view_rep_01_lxx'; Open; Lv_MaxMonthR := FieldByName('MONTHS').AsString; Close; Sql.Text := 'select max(months) months from view_rep_02_lxx'; Open; Lv_MaxMonthL := FieldByName('MONTHS').AsString; if StrToInt(Lv_MaxMonthR)>StrToInt(Lv_MaxMonthL) then Lv_MaxMonth := Lv_MaxMonthR else Lv_MaxMonth := Lv_MaxMonthL; Close; Sql.Text := 'select to_char(sysdate,''yyyymm'') cur_date from dual'; Open; Lv_CurMonth := FieldByName('CUR_DATE').AsString; end; Lv_Cnt := (StrToInt(Copy(Lv_MaxMonth,1,4)) - StrToInt(Copy(Lv_CurMonth,1,4))) * 12 + StrToInt(Copy(Lv_MaxMonth,5,2)) - StrToInt(Copy(Lv_CurMonth,5,2)) + 1; Lv_ExcelWkSheet.Cells[3,1] := '鋼種'; Lv_ExcelWkSheet.Cells[2,2] := Lv_CurMonth + '前'; Lv_ExcelWkSheet.Cells[3,2] := '訂單重量'; Lv_ExcelWkSheet.Cells[3,3] := '鎳含量'; Lv_ExcelWkSheet.Range['B2:C2'].Merge; for Lv_i := 0 to Lv_Cnt - 1 do begin Lv_ExcelWkSheet.Cells[2,Lv_i * 2 + 4] := IntToYearMonth(StrToInt(Lv_CurMonth) + Lv_i); Lv_ExcelWkSheet.Cells[3,Lv_i * 2 + 4] := '訂單重量'; Lv_ExcelWkSheet.Cells[3,Lv_i * 2 + 5] := '鎳含量'; Lv_ExcelWkSheet.Range[IntColToCharCol(Lv_i * 2 + 4)+'2:'+IntColToCharCol(Lv_i * 2 + 5)+'2'].Merge; end; //=========================冷精=============================================== for Lv_j := 4 to StrGrid1.RowCount + 2 do begin Lv_ExcelWkSheet.Cells[Lv_j,1] := '''' + StrGrid1.Cells[1,Lv_j - 3]; end; for Lv_i := 1 to StrGrid3.ColCount - 2 do //比較次數 begin for Lv_j := 0 to Lv_Cnt do begin if VarToStr(StrGrid3.Cells[Lv_i + 1,0]) = VarToStr(Lv_ExcelWkSheet.Cells[2,Lv_j * 2 + 2]) then begin for Lv_z := 4 to StrGrid1.RowCount + 2 do begin Lv_ExcelWkSheet.Cells[Lv_z,Lv_j * 2 + 2] := StrGrid1.Cells[Lv_i * 2,Lv_z - 3]; Lv_ExcelWkSheet.Cells[Lv_z,Lv_j * 2 + 3] := StrGrid1.Cells[Lv_i * 2 + 1,Lv_z - 3]; end; end; end; end; Lv_ExcelWkSheet.Cells[StrGrid1.RowCount + 3,1] := '冷精總計'; Lv_ExcelWkSheet.Cells[StrGrid1.RowCount + 3,1].Interior.ColorIndex := 40; Lv_Sum := 0; for Lv_j := 2 to (Lv_Cnt + 1) * 2 + 1 do begin for Lv_i := 4 to StrGrid1.RowCount + 2 do begin if VarToStr(Lv_ExcelWkSheet.Cells[Lv_i,Lv_j])='' then Lv_ExcelWkSheet.Cells[Lv_i,Lv_j] := '0'; Lv_Sum := Lv_Sum + StrToFloat(Lv_ExcelWkSheet.Cells[Lv_i,Lv_j]); end; Lv_ExcelWkSheet.Cells[StrGrid1.RowCount + 3,Lv_j] := FloatToStr(Lv_Sum); Lv_ExcelWkSheet.Cells[StrGrid1.RowCount + 3,Lv_j].Interior.ColorIndex := 40; Lv_Sum := 0; end; //==========================熱軋============================================== Lv_RowStart := StrGrid1.RowCount + 3; for Lv_i := 1 to StrGrid2.RowCount -1 do begin Lv_ExcelWkSheet.Cells[Lv_i + Lv_RowStart ,1] := '''' + StrGrid2.Cells[1,Lv_i]; end; for Lv_i := 1 to StrGrid4.ColCount - 2 do //比較次數 begin for Lv_j := 0 to Lv_Cnt do begin if VarToStr(StrGrid4.Cells[Lv_i + 1,0]) = VarToStr(Lv_ExcelWkSheet.Cells[2,Lv_j * 2 + 2]) then begin for Lv_z := 1 to StrGrid2.RowCount -1 do begin Lv_ExcelWkSheet.Cells[Lv_z + Lv_RowStart,Lv_j * 2 + 2] := StrGrid2.Cells[Lv_i * 2,Lv_z]; Lv_ExcelWkSheet.Cells[Lv_z + Lv_RowStart,Lv_j * 2 + 3] := StrGrid2.Cells[Lv_i * 2 + 1,Lv_z]; end; end; end; end; Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount,1] := '熱軋總計'; Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount,1].Interior.ColorIndex := 40; Lv_Sum := 0; for Lv_j := 2 to (Lv_Cnt + 1) * 2 + 1 do begin for Lv_i := 1 to StrGrid2.RowCount - 1 do begin if VarToStr(Lv_ExcelWkSheet.Cells[Lv_i + Lv_RowStart ,Lv_j])='' then Lv_ExcelWkSheet.Cells[Lv_i + Lv_RowStart,Lv_j] := '0'; Lv_Sum := Lv_Sum + StrToFloat(Lv_ExcelWkSheet.Cells[Lv_i + Lv_RowStart,Lv_j]); end; Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount,Lv_j] := FloatToStr(Lv_Sum); Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount,Lv_j].Interior.ColorIndex := 40; Lv_Sum := 0; end; //============================================================================ Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount + 1,1] := '合計'; Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount + 1,1].Interior.ColorIndex := 6; for Lv_j := 2 to (Lv_Cnt + 1) * 2 + 1 do begin Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount + 1,Lv_j] := StrToFloat(VarToStr(Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount,Lv_j])) + StrToFloat(VarToStr(Lv_ExcelWkSheet.Cells[StrGrid1.RowCount + 3,Lv_j])); Lv_ExcelWkSheet.Cells[Lv_RowStart + StrGrid2.RowCount + 1,Lv_j].Interior.ColorIndex := 6; end; Lv_ExcelWkSheet.Cells[1,Lv_Cnt + 1] := '確認訂單出貨(單位:KG)'; Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+'1'].HorizontalAlignment := xlCenter; Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+'1'].Interior.ColorIndex := 34; Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+'2'].HorizontalAlignment := xlCenter; Lv_ExcelWkSheet.Range['A2:'+IntColToCharCol(Lv_Cnt*2+3)+'2'].Interior.ColorIndex := 6; Lv_ExcelWkSheet.Range['A3:'+IntColToCharCol(Lv_Cnt*2+3)+'3'].Interior.ColorIndex := 40; Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+'1'].Merge; Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+IntToStr(Lv_RowStart + StrGrid2.RowCount + 1)].Borders[3].Weight := 2; //up Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+IntToStr(Lv_RowStart + StrGrid2.RowCount + 1)].Borders[4].Weight := 2; //down Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+IntToStr(Lv_RowStart + StrGrid2.RowCount + 1)].Borders[1].Weight := 2; //left Lv_ExcelWkSheet.Range['A1:'+IntColToCharCol(Lv_Cnt*2+3)+IntToStr(Lv_RowStart + StrGrid2.RowCount + 1)].Borders[2].Weight := 2; //right Lv_ExcelApp.Visible := True;end;