Delphi 生成excel中饼图的实例代码

所属分类: 软件编程 / Delphi 阅读数: 97
收藏 0 赞 0 分享
复制代码 代码如下:

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;
更多精彩内容其他人还在看

delphi实现保存和读取图片的方法

这篇文章主要介绍了delphi实现保存和读取图片的方法,主要包括了显示图片、保存图片和读取图片的实现方法,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi7中群发Email邮件的方法

这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi字符串分隔函数用法实例

这篇文章主要介绍了delphi字符串分隔函数用法,通过自定义函数SeparateTerms2实现将字符串分割后存入字符串列表的功能,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi实现将BMP格式图形转化为JPG格式图形的方法

这篇文章主要介绍了delphi实现将BMP格式图形转化为JPG格式图形的方法,通过简单的自定义函数调用系统自带的changefileext及SaveToFile等方法来实现格式转换功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi解析FTP地址的方法

这篇文章主要介绍了Delphi解析FTP地址的方法,涉及Delphi解析FTP的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法

这篇文章主要介绍了Delphi实现窗体感知鼠标滑过并自动隐藏与显示窗口的方法,涉及Delphi操作窗口及鼠标事件的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现截屏存盘的方法

这篇文章主要介绍了Delphi实现截屏存盘的方法,涉及Delphi图片操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi基本图像处理方法汇总

这篇文章主要介绍了Delphi基本图像处理方法,实例汇总了Delphi操作图像实现浮雕、反色、模糊、翻转等常用效果的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows API GetLastError()函数返回值含义解释

这篇文章主要介绍了Windows API GetLastError()函数返回值含义解释,本文罗列了所有错误代码及中文注释,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi制作wav文件的方法

这篇文章主要介绍了delphi制作wav文件的方法,涉及Delphi操作多媒体文件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多