zoukankan      html  css  js  c++  java
  • Delphi 生成excel中的饼图

    生成excel中的饼图

    var

      i,j,m,n,count1:integer;

      str:string;

      Jdate:tdatetime;

      channellist,potBstrtime,potEstrtime,Jchannelname:string;

      Rres:boolean;

      JSHR:double;

      Excelid,achart1,cell1,cell2,cell3,cell4,cellMiddle,Range1,Range2,series1:variant;

      ExRowsValue,ExRowsName,ExClos:Integer;

      ImagePage,dataPage:integer;

      leftPostion,topPostion,widthPostion,heightPostion:integer;

    begin

      //创建OLE对象

      try

        Excelid:=CreateOleObject( 'Excel.Application' );

      except

        on Exception do raise exception.Create('无法创建Xls文件,请确认是否安装EXCEL')

      end;

      Excelid.Visible := false; //Excel显示设置

      Excelid.WorkBooks.Add;  //生成新的excel文件

      //不用excel以前的页

      Excelid.worksheets.add;

      dataPage:=Excelid.Worksheets.count;   //用作数据页

      Excelid.worksheets.add;

      ImagePage:=Excelid.Worksheets.count;

      //showmessage(inttostr(dataPage));

      //showmessage(inttostr(ImagePage)+'sdfg');

      Jdate:=DateTimePicker1.Date;

      count1:=0;

      ExRowsValue:=1; //存储值

      ExRowsName:=2;  //存储名称

      //取得频率编号

      for i:=0 to CheckListBoxFQ.Items.Count-1 do

      begin

        if CheckListBoxFQ.Checked[i] then

        begin

         channellist:= QCaky.FChannelList[i]+','+channellist;

        end;

      end;

      delete(channellist,length(channellist),1);

      leftPostion:=20;

      topPostion:=50;

      widthPostion:=450;

      heightPostion:=230;

      for i:=0 to CheckListBoxPOTT.Items.Count-1 do

      begin

        if CheckListBoxPOTT.Checked[i] then

        begin

          //动态生成excel页数

          //Excelid.worksheets.add;

          //sheetCount:=sheetCount+1;

          //Excelid.worksheets[sheetCount].name:='图表页'+inttostr(sheetCount);

          //在这里生成图表

          if CheckListBoxPOTT.Items[i]='全天' then

          begin

            potBstrtime:='';

            potEstrtime:='';

          end

          else

          begin

            potBstrtime:=QCaky.FPOTbeginList[i];

            potEstrtime:=QCaky.FPOTendList[i];

          end;

          Rres:=QCaky.getFQShr(Jdate,FormMain.QAreacode,channellist,potBstrtime,potEstrtime);  //取得占有率

          ExClos:=1;

          while not QCaky.FADOQueryFQ.Eof do

          begin

            Excelid.worksheets[dataPage].Activate;

            Jchannelname:=QCaky.FADOQueryFQ.fieldbyname('channelname').AsString;

            JSHR:=QCaky.FADOQueryFQ.fieldbyname('allshr').AsFloat;

            Excelid.worksheets[dataPage].Cells[ExRowsName,ExClos].Value :=Jchannelname;

            Excelid.worksheets[dataPage].Cells[ExRowsValue,ExClos].Value :=JSHR;

            inc(ExClos);

            QCaky.FADOQueryFQ.Next;

          end;

           {添加一个二维图(20,50,450,230)表示(左,顶,宽,高)}

          //achart1 := Excelid.worksheets[ImagePage].chartobjects.add(20,50,450,230);

          achart1 := Excelid.worksheets[ImagePage].chartobjects.add(leftPostion,topPostion,widthPostion,heightPostion);

          achart1.chart.charttype := xl3DPie ;  //生成何种类型的图********在此生成的是饼图

          //设置x轴、y轴数据

          cell1 :=  Excelid.worksheets[dataPage].Cells[ExRowsName,1];

          cell2 :=  Excelid.worksheets[dataPage].Cells[ExRowsName,ExClos];

          cell3 :=  Excelid.worksheets[dataPage].Cells[ExRowsValue,1];

          cell4 :=  Excelid.worksheets[dataPage].Cells[ExRowsValue,ExClos];

          Range1 := Excelid.worksheets[dataPage].Range[cell1, cell2]; //设定Chart类别坐标轴(x轴)的取值区域

          Range2 := Excelid.worksheets[dataPage].Range[cell3, cell4]; //设定Chart数值坐标轴(y轴)的取值区域

          series1:= achart1.chart.seriescollection ;

          //series1.add (Range2,xlColumns,false);  //设置y轴的数据

          series1.add (Range2,true);          //可以使用,能够生成饼图

          achart1.chart.seriescollection[1].name:='nihao';

          //设置饼图的形式

          achart1.chart.seriescollection[1].ApplyDataLabels(xlDataLabelsShowLabelAndPercent,true,true,true);

          achart1.Chart.Axes[xlCategory].CategoryNames := Range1;  //设置x轴数据

          //series1.add (Range2,xlColumns,false);  //设置y轴的数据

          //achart1.Chart.Axes[xlCategory].CategoryNames := Range1;  //设置x轴数据

          //删除右侧的标题

          achart1.Chart.Legend.delete;

          //添加图表标题

          achart1.Chart.HasTitle:=True;

          achart1.Chart.ChartTitle.Characters.Text:='收视率走势图';

          achart1.Chart.HasLegend:=true;

          achart1.Chart.Legend.Position := xlBottom;

          //图表左标说明

          //achart1.Chart.Axes(xlValue,xlPrimary).HasTitle := True;

          //achart1.Chart.Axes(xlValue,xlPrimary).AxisTitle.Characters.Text := '收视率(%)';

          //图表下标说明

          //achart1.Chart.Axes(xlCategory,xlPrimary).HasTitle:=True;

          //achart1.Chart.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text:='时间';

          ExRowsName:=ExRowsName+2;

          ExRowsValue:=ExRowsValue+2;

        end;  //如果时段设置被选中

        leftPostion:=20;

        topPostion:=topPostion+600;

        //showmessage(inttostr(dataPage));

      end;

      Excelid.Visible := true;

    end;

  • 相关阅读:
    spark简单入门
    vim 树形目录插件NERDTree
    Windows下查看系统端口使用的命令
    网络爬虫爬取动态网页
    Java并查集链表实现
    基于mahout的海量数据关联规则挖掘
    高维特征降维方法-随机映射
    JVM(4)--垃圾回收算法
    Java高并发程序设计(六)--线程池(1)
    Java高并发程序设计(五)--ReentrantLock源码解析
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/3267645.html
Copyright © 2011-2022 走看看