如何将几张sql数据表(包括报表中的台头)倒成一张excel工作表(有相应的台头),给点源代码?谢谢

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/1105/1105886.xml?temp=.5982935
      

  2.   

    uses Excel2000, OleServer,;//不能少!
    procedure TMaterialFrm.btnOutToExcelClick(Sender: TObject);
    var
    xlApp:TExcelApplication;
    xlBook:TExcelWorkBook;
    xlSheet:TExcelWorkSheet;
    iRow:Double;
    begin
    if Application.MessageBox('是否确认将当前数据导出到Excel中?','程序提示',mb_yesno+mb_iconquestion)=idno then
    exit;
    xlApp:=TExcelApplication.Create(Self);
    xlBook:=TExcelWorkBook.Create(Self);
    xlSheet:=TExcelWorkSheet.Create(Self);
    try
    try
    xlApp.ConnectKind:=ckNewInstance;
    xlApp.Connect;
    xlApp.Visible[0]:=True;
    except
    Application.MessageBox('没有安装Excel2000.','程序提示',mb_iconinformation);
    exit;
    end;
    xlApp.WorkBooks.Add(EmptyParam,0);
    xlBook.ConnectTo(xlApp.Workbooks[1]);
    xlSheet.ConnectTo(xlBook.Worksheets[1] as _WorkSheet);
    //打印和页面设置
    //xlSheet.PageSetup.Orientation:=xlLandscape;
    xlSheet.PageSetup.LeftMargin := xlApp.InchesToPoints(0.748031496062992);
    xlSheet.PageSetup.RightMargin := xlApp.InchesToPoints(0.590551181102362);
    xlSheet.PageSetup.TopMargin := xlApp.InchesToPoints(0.511811023622047);
    xlSheet.PageSetup.BottomMargin := xlApp.InchesToPoints(0.78740157480315);
    xlSheet.PageSetup.HeaderMargin := xlApp.InchesToPoints(0.511811023622047);
    xlSheet.PageSetup.FooterMargin := xlApp.InchesToPoints(0.47244094488189);
    xlSheet.PageSetup.PrintTitleRows := '$1:$2';
    //页眉页脚
    xlSheet.PageSetup.LeftFooter:= '日期:&D';
    xlSheet.PageSetup.RightFooter:='第 &P 页,共 &N 页';
    //列宽
    xlSheet.Cells.Item[1,1].ColumnWidth:=14.13;
    xlSheet.Cells.Item[1,2].ColumnWidth:=21.75;
    xlSheet.Cells.Item[1,3].ColumnWidth:=6.88;
    xlSheet.Cells.Item[1,4].ColumnWidth:=7.38;
    xlSheet.Cells.Item[1,5].ColumnWidth:=6.25;
    xlSheet.Cells.Item[1,6].ColumnWidth:=14.13;
    //标题
    xlSheet.Cells.Item[1,6]:='基本材料列表';
    xlSheet.Cells.Item[1,6].Font.Size:=18;
    xlSheet.Cells.Item[1,6].Font.name:='黑体';
    xlSheet.Cells.Item[1,6].Font.Bold:=True;
    xlSheet.Range[xlSheet.Cells.Item[1,1],xlSheet.Cells.Item[1,6]].Merge(EmptyParam);
    xlSheet.Cells.Item[1,1].HorizontalAlignment:=xlCenter;
    xlSheet.Cells.Item[1,1].VerticalAlignment:=xlTop;
    xlSheet.Cells.Item[1,1].RowHeight:=27;
    //表头
    iRow:=2;
    xlSheet.Cells.Item[iRow,1]:='材料编号';
    xlSheet.Cells.Item[iRow,2]:='材料名称';
    xlSheet.Cells.Item[iRow,3]:='单 位';
    xlSheet.Cells.Item[iRow,4]:='类 别';
    xlSheet.Cells.Item[iRow,5]:='颜 色';
    xlSheet.Cells.Item[iRow,6]:='备 注';
    xlSheet.Range[xlSheet.Cells.Item[iRow,1],xlSheet.Cells.Item[iRow,6]].Font.Bold:=True;
    xlSheet.Range[xlSheet.Cells.Item[iRow,1],xlSheet.Cells.Item[iRow,6]].HorizontalAlignment:= xlCenter;
    iRow:=iRow+1;
    QryMaterial.DisableControls;
    QryMaterial.First;
    while not QryMaterial.Eof do
    begin
    xlSheet.Cells.Item[iRow,1]:=QryMaterial['MaterialId'];
    xlSheet.Cells.Item[iRow,2]:=QryMaterial['MaterialName'];
    xlSheet.Cells.Item[iRow,3]:=QryMaterial['MaterialUnit'];
    xlSheet.Cells.Item[iRow,3].HorizontalAlignment:=xlCenter;
    xlSheet.Cells.Item[iRow,4]:=QryMaterial['MaterialClass'];
    xlSheet.Cells.Item[iRow,5].NumberFormatLocal := '@';
    xlSheet.Cells.Item[iRow,5]:=QryMaterial['ColorId'];
    xlSheet.Cells.Item[iRow,5].HorizontalAlignment:=xlCenter;
    xlSheet.Cells.Item[iRow,6]:=QryMaterial['Res'];
    QryMaterial.Next;
    iRow:=iRow+1;
    end;
    QryMaterial.EnableControls;
    xlSheet.Range[xlSheet.Cells.Item[2,1],xlSheet.Cells.Item[iRow-1,6]].Borders.LineStyle := xlContinuous;
    xlApp.Disconnect;
    finally
    xlApp.Free;
    xlBook.Free;
    xlSheet.Free;
    end;
    end;