Copy the code code as follows:
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
//Create OLE object
try
Excelid:=CreateOleObject( 'Excel.Application' );
except
on Exception do raise exception.Create('Unable to create Xls file, please confirm whether EXCEL is installed')
end;
Excelid.Visible := false; //Excel display settings
Excelid.WorkBooks.Add; //Generate a new excel file
//Do not use excel’s previous pages
Excelid.worksheets.add;
dataPage:=Excelid.Worksheets.count; //Used as data page
Excelid.worksheets.add;
ImagePage:=Excelid.Worksheets.count;
//showmessage(inttostr(dataPage));
//showmessage(inttostr(ImagePage)+'sdfg');
Jdate:=DateTimePicker1.Date;
count1:=0;
ExRowsValue:=1; //Storage value
ExRowsName:=2; //Storage name
//Get frequency number
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
//Dynamicly generate excel page number
//Excelid.worksheets.add;
//sheetCount:=sheetCount+1;
//Excelid.worksheets[sheetCount].name:='Chart page'+inttostr(sheetCount);
//Generate chart here
if CheckListBoxPOTT.Items[i]='All day' 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); //Get occupancy
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;
{Add a two-dimensional image (20,50,450,230) to represent (left, top, width, height)}
//achart1 := Excelid.worksheets[ImagePage].chartobjects.add(20,50,450,230);
achart1 := Excelid.worksheets[ImagePage].chartobjects.add(leftPostion,topPostion,widthPostion,heightPostion);
achart1.chart.charttype := xl3DPie; //What type of chart is generated ******** Here is a pie chart.
//Set x-axis and y-axis data
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]; //Set the value area of the Chart category coordinate axis (x-axis)
Range2 := Excelid.worksheets[dataPage].Range[cell3, cell4]; //Set the value area of the Chart numerical coordinate axis (y-axis)
series1:= achart1.chart.seriescollection;
//series1.add (Range2,xlColumns,false); //Set the y-axis data
series1.add (Range2,true); //Can be used to generate pie charts
achart1.chart.seriescollection[1].name:='nihao';
//Set the pie chart format
achart1.chart.seriescollection[1].ApplyDataLabels(xlDataLabelsShowLabelAndPercent,true,true,true);
achart1.Chart.Axes[xlCategory].CategoryNames := Range1; //Set x-axis data
//series1.add (Range2,xlColumns,false); //Set the y-axis data
//achart1.Chart.Axes[xlCategory].CategoryNames := Range1; //Set x-axis data
//Delete the title on the right
achart1.Chart.Legend.delete;
//Add chart title
achart1.Chart.HasTitle:=True;
achart1.Chart.ChartTitle.Characters.Text:='Ratings Chart';
achart1.Chart.HasLegend:=true;
achart1.Chart.Legend.Position := xlBottom;
//Chart left subscript description
//achart1.Chart.Axes(xlValue,xlPrimary).HasTitle := True;
//achart1.Chart.Axes(xlValue,xlPrimary).AxisTitle.Characters.Text := 'Rating (%)';
//Chart subscript description
//achart1.Chart.Axes(xlCategory,xlPrimary).HasTitle:=True;
//achart1.Chart.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text:='time';
ExRowsName:=ExRowsName+2;
ExRowsValue:=ExRowsValue+2;
end; //If the period setting is selected
leftPostion:=20;
topPostion:=topPostion+600;
//showmessage(inttostr(dataPage));
end;
Excelid.Visible := true;
end;