如果报表复杂,可以利用cxSpreadSheetBook电子表格控件来生成报表。
下面例子是装载数据同时设置电子表格格式:
procedure TAO_RepShowData.LoadRepData;
var
CurCursor: TCursor;
begin
CurCursor := Screen.Cursor; // 光标设置
Screen.Cursor := crHourGlass;
try
cxSpreadBook.BeginUpdate;
LoadTitle; //从数据库取数加载表格标题
LoadData; //从数据库取数加载表格数据
cxSpreadBook.ActiveSheet.SelectCell(-1, -1, False);//光标位置在表格外
Finally
cxSpreadBook.EndUpdate;
cxSpreadBook.Recalc; //重新计算表格
cxSpreadBook.ActivePage := 0; // 第一页
Screen.Cursor := CurCursor; // 光标设置
end;
end;
procedure TAO_RepShowData.LoadTitle; //装载表格标题
var
i, CRow: integer;
CHeader: TcxSSHeader;
begin
i := cdsAnalyseField.RecordCount; //合并单元格格数
//GlobalVars.Values[‘REPNAME‘] 是个变量
SetCellText(1, 0, GlobalVars.Values[‘REPNAME‘]); //设置表格头名称(报表名称) cxSpreadBook.ActiveSheet.Caption := GlobalVars.Values[‘REPNAME‘];//页标签名称
cxSpreadBook.ActiveSheet.SelectCell(1, 0); //选中单元格
SetMergedStateA(1, 0, i + 1, 0, true); //合并单元格
SetCellFont(1, 0, i, 0, [fsBold], 20); //设置字体大小
//SetCellsStyle([svAlign], haCenter, 0, cbxFont.Text, []);
SetCellAlignment(1, 0, i + 1, 0, haCenter, vaCenter);//格式居中
SetCellText(1, 1, GlobalVars.Values[‘JHDW‘]); //设置金额单位
SetCellText(1, 2, ‘单位名称‘);//设置表格表题
SetCellAlignment(1, 2, 1, 2, haCenter, vaCenter);//格式居中
SetCellFont(1, 2, 1, 2, [fsBold], 8);
CHeader := cxSpreadBook.ActiveSheet.Cols;
CHeader.Size[1] := 180;//列宽度
cdsRepDwData.DisableControls;
try
CRow := 3;//从第三行开始
cxSpreadBook.ActivePage := 0;
cdsRepDwData.First;
while not cdsRepDwData.eof do begin
SetCellText(1, CRow, cdsRepDwDataDANWEI_MINGCHANG.Value);//从数据库中取数填入表格单元
cdsRepDwData.Next;
Inc(CRow);
end;
SetmnuCellBorders(1, 2, 1, CRow - 1, 5);//设置表格格式
//SetCellPattern(1,2,1,CurRow-1,23,1,fsSolid);
Finally
cdsRepDwData.EnableControls; // restore original cursor
end;
end;
procedure TAO_RepShowData.LoadData;//装载数据
var
CurCol, CurRow: Integer;
CHeader: TcxSSHeader;
begin
if cdsAnalyseField.RecordCount > 0 then
begin
try
cxSpreadBook.ActivePage := 0;
cdsAnalyseField.DisableControls;
CurCol := 2;//从第二列开始
CurRow := 3;//从第三行开始
cdsAnalyseField.First;
while not cdsAnalyseField.eof do begin
CHeader := cxSpreadBook.ActiveSheet.Cols;
CHeader.Size[CurCol] := 120;//列宽度
SetCellText(CurCol, 2, cdsAnalyseFieldANALYSE_FIELD_NAME.Value);
try
OpenRepDwData(GlobalVars.Values[‘DANWEI_QYDM‘], StrToInt(GlobalVars.Values[‘REP_VOL_KEY‘]), cdsAnalyseFieldSIGN_CELL_KEY.Value);
cdsRepDwData.DisableControls;
CurRow := 3;
cdsRepDwData.First;
while not cdsRepDwData.eof do begin
if cxSpreadBook.ActiveSheet.GetCellObject(1, CurRow).Text = cdsRepDwDataDANWEI_MINGCHANG.Value then
begin
if cdsRepDwDataSIGN_DELL_TYPE.Value = 0 then //RoundFloat(StrToFloat(Text),2)
if cdsRepDwDataREP_DATA.Value = 0 then
SetCellText(CurCol, CurRow, ‘0‘)
else
begin
if GlobalVars.Values[‘JHDW‘] = ‘金额单位:元‘ then//加载数据,金额换算成元。
SetCellText(CurCol, CurRow, FormatFloat(‘#,#.00‘, cdsRepDwDataREP_DATA.Value)); //FormatFloat(‘#,#.##‘,cdsRepCellDataREP_DATA.Value))
if GlobalVars.Values[‘JHDW‘] = ‘金额单位:万元‘ then//加载数据,金额换算成万元。
SetCellText(CurCol, CurRow, FormatFloat(‘#,#.00‘, StrToFloat(FloatToStr(cdsRepDwDataREP_DATA.Value / 10000))));
if GlobalVars.Values[‘JHDW‘] = ‘金额单位:亿元‘ then//加载数据,金额换算成亿元。
SetCellText(CurCol, CurRow, FormatFloat(‘#,#.00‘, StrToFloat(FloatToStr(cdsRepDwDataREP_DATA.Value / 100000000))));
end;
if cdsRepDwDataSIGN_DELL_TYPE.Value = 1 then
SetCellText(CurCol, CurRow, cdsRepDwDataREP_TEXT.Value);
end;
cdsRepDwData.Next;
Inc(CurRow);
end;
Finally
cdsRepDwData.EnableControls; // restore original cursor
end;
cdsAnalyseField.Next;
Inc(CurCol);
end;
SetCellFormat(2, 3, CurCol, CurRow, 4);//设置表格格式
SetmnuCellBorders(2, 2, CurCol - 1, CurRow - 1, 5);//设置表格线
SetCellAlignment(2, 2, CurCol, 2, haCenter, vaCenter);
SetCellFont(2, 2, CurCol, 2, [fsBold], 8);
Finally
cdsAnalyseField.EnableControls; // restore original cursor
end;
end;
end;