3 根据数据集生成DataSheet
此例子用到了 定义区域,定义单元格类型,定义边框线,全新创建一个DataSheet
var
lSheet: TDataSheet;
lExporter: TSheetExporter;
lArea: TDataSheetArea;
lDataSet: TDataSet;
lCell: TSheetCell;
i, lCnt: Integer;
b: TCellSideBorder;
begin
lDataSet := DataSetBroker1.DataSet;
lSheet := TDataSheet.Create(Self);
lExporter := TSheetExporter.Create(Self);
lExporter.Sheet := lSheet;
//这里必须定义PageInfo和HFSheet,否则Preview出指针错
lExporter.PageInfo := TMySheetDoc(DocView1.Doc).PageInfo as TSheetPageInfo;
lExporter.HFSheet := TMySheetDoc(DocView1.Doc).HF.Sheet;
lSheet.AddDataSet(lDataSet);
lSheet.RowCount := 6;
b.Line := TCellSideLine.slSingle;
b.Color := Business.System.Graphics.clBlue;
lCnt := 0;
for i:=0 to lDataSet.FieldCount-1 do
begin
if not lDataSet.Fields.Visible then
Continue;
Inc(lCnt);
lSheet.ColWidths[lCnt] := lDataSet.Fields.DisplayWidth*lSheet.Cells[0,0].Font.Size;
//必须先定义CellType再定义单元格的其他属性,否则就晚了
lSheet.CellTypes[lCnt, 1] := TCellType.ctFieldLabel;
lCell := lSheet.Cells[lCnt, 1];
TFieldLabelCell(lCell).DataSetID := TSQLDataSet(lDataSet).ID;
TFieldLabelCell(lCell).FieldName := lDataSet.Fields.FieldName;
//设置上边框线为粗线
b.Line := TCellSideLine.slMedium;
lCell.Border[TCellSide.sTop] := b;
//设置下边框线为细线
b.Line := TCellSideLine.slSingle;
lCell.Border[TCellSide.sBottom] := b;
lSheet.CellTypes[lCnt, 2] := TCellType.ctField;
lCell := lSheet.Cells[lCnt, 2];
TFieldCell(lCell).DataSetID := TSQLDataSet(lDataSet).ID;
TFieldCell(lCell).FieldName := lDataSet.Fields.FieldName;
b.Line := TCellSideLine.slMedium;
lCell.Border[TCellSide.sBottom] := b;
end;
//设置外边框是粗线
b.Line := TCellSideLine.slMedium;
lCell := lSheet.Cells[1, 1];
lCell.Border[TCellSide.sLeft] := b;;
lCell := lSheet.Cells[1, 2];
lCell.Border[TCellSide.sLeft] := b;
lCell := lSheet.Cells[lCnt, 1];
lCell.Border[TCellSide.sRight] := b;
lCell := lSheet.Cells[lCnt, 2];
lCell.Border[TCellSide.sRight] := b;
//定义数据集区域
lArea := lSheet.Areas.Add(TAreaType.atDBArea) as TDataSheetArea;
lArea.DataSetID := TSQLDataSet(lDataSet).ID;
lArea.BoundsRect := Types.Bounds(1, 1, lCnt, 2);
lArea.TitleRows := 1;
//DataSheet的列数一定要比实际列数多一列,否则最右边的边框线就没有了
lSheet.ColCount := lCnt+1;
lExporter.Preview(nil);
end; |