定義變量
Excelid:variant;
1、創建OLE對象
try
Excelid:=CreateOleObject( 'Excel.Application' );
except
on Exception do raise exception.Create('無法創建Xls文件,請確認是否安裝EXCEL')
end;
Excelid.Visible := false; //Excel顯示設置
Excelid.WorkBooks.Add; //生成新的excel文件
BCount:=Excelid.Worksheets.count; //取得了excel的worksheets的頁數
BCount:=Excelid. sheets.count; //取得了excel的sheets的頁數,包括(worksheet,chart等等)
Excelid.worksheets.add(null,Excelid.Worksheets[BCount]); // Excelid.Worksheets[BCount])的 后面生成新頁 Excelid.worksheets.add(before,after)
Excelid.worksheets[dataPage].Columns[1].NumberFormatLocal := '00000000' ; //把這一列按某種格式顯示
ExcelApplication1.ActiveSheet.Rows[1].numberformat:='@'; //把這一列設置成文本格式
Excelid.worksheets[dataPage].name:='主要頻率時段占有率'; //worksheet的caption
Excelid.worksheets[dataPage].cells[1,ExClos].value:=FCheckListBoxPOTT.Items[i];//給單元格賦值
Excelid.worksheets[dataPage].rows[1].rowheight:=28.5;//行高
Excelid.worksheets[dataPage].rows[1].WrapText:=true;//文本自動換行
Excelid.worksheets[dataPage].cells[1,cols].Borders[2].Weight:=xlThin;//設置邊框為黑邊框
Excelid.worksheets[dataPage].Range[Excelid.worksheets[dataPage].Cells[1,1], Excelid.worksheets[dataPage].Cells[rows,cols]].Borders.LineStyle := xlContinuous;//加邊框
Excelid.worksheets[dataPage].Range[Excelid.worksheets[dataPage].Cells[1,1], Excelid.worksheets[dataPage].Cells[rows,cols]].HorizontalAlignment:=xlHAlignCenter;//居中對齊
Excelid.worksheets[dataPage].Range[Excelid.worksheets[dataPage].Cells[1,1], Excelid.worksheets[dataPage].Cells[rows,cols]].font.size:=12; //字體大小
Excelid.worksheets[dataPage].Range[Excelid.worksheets[dataPage].Cells[1,1], Excelid.worksheets[dataPage].Cells[rows,cols]].font.name:='宋體'; //字體格式
achart1 := Excelid.worksheets[dataPage].chartobjects.add(left,top,width,height); //在這個位置生成圖
achart1.chart.charttype := xl3DPieExploded;//xl3DPie ; //生成何種類型的圖
cell1 := Excelid.worksheets[dataPage].Cells[fixRows,fixClos];
cell2 := Excelid.worksheets[dataPage].Cells[Exrows,fixClos];
cell3 := Excelid.worksheets[dataPage].Cells[fixRows,ExClos];
cell4 := Excelid.worksheets[dataPage].Cells[Exrows,ExClos];
Range1 := Excelid.worksheets[dataPage].Range[cell1, cell2]; //設定Chart類別坐標軸(x軸)的取值區域
Range2 := Excelid.worksheets[dataPage].Range[cell3, cell4]; //設定Chart數值坐標軸(y軸)的取值區域
Range3:=Excelid.worksheets[dataPage].Range[Range1,Range2]; //取值區域
achart2:=achart1.chart.Location(xlLocationAsNewSheet,sheetName); //使生成的為一個新頁,並占滿整個頁
achart2.SetSourceData(Range3,xlColumns); // 設置生成圖形的數據源
achart2.ApplyDataLabels(xlDataLabelsShowLabelAndPercent,true,true,true); //設置格式,使圖形上顯示的文字顯示百分比
achart2.Legend.delete; //刪除右側的標題
achart2.HasTitle:=True; //添加圖表標題能夠顯示
achart2.ChartTitle.Characters.Text:=chartName;//圖的標題
achart2.ChartTitle.Font.size:=18;//圖的標題的字體的大小
achart2.HasLegend:=true;//能夠顯示屬性圖
achart2.Legend.Position := xlBottom; //屬性圖顯示在下面
achart2.ChartArea.Border.Weight := xlHairline; 圖的外框的粗細
achart2.ChartArea.Border.LineStyle := xlContinuous//圖的外框的樣式
//設置圖像
achart2.PlotArea.Interior.ColorIndex := xlNone;
achart2.PlotArea.Border.Weight := xlHairline ;
achart2.PlotArea.Border.LineStyle := xlNone ;
achart2.PlotArea.width:=495;
achart2.PlotArea.left:=117;
achart2.PlotArea.Top:=127;
Excelid.DisplayAlerts:= False; //使excel不談出對話框
Excelid.worksheets[3].Rows.EntireColumn.AutoFit;//excel自動調整列
Excelid.worksheets[3].rows.AutoFit;
Excelid.worksheets[excelSheetCount].range['D'+inttostr(hebin1)+':D'+inttostr(row-1)].Mergecells:=True;//合並單元格
Excelid.worksheets[excelSheetCount].range['A4:M'+inttostr(row-1)+''].borders.linestyle:=1;//設置邊框樣式
Excelid.worksheets[excelSheetCount].Rows.EntireColumn.AutoFit;//excel自動調整列
Excelid.worksheets[excelSheetCount].rows.AutoFit;//自動調整行
Excelid.WorkSheets[excelSheetCount].Cells[1,1].Characters(1,setStrLength).font.size:=20;//取得單元格內的內容是一部分按設定的格式顯示
Excelid.WorkSheets[excelSheetCount].Cells[1,1].font.size:=20; //設置單元格的字體大小
Excelid.WorkSheets[excelSheetCount].Cells[1,1].font.bold:=true;//設置字體為黑體
Excelid.worksheets[excelSheetCount].range['A1:N3'].HorizontalAlignment := xlleft;//字體左對齊
Excelid.WorkSheets[excelSheetCount].Cells[3,1].Value := '更新日期: '+formatdatetime('yyyy年mm月dd日',now());
ExcelId.WorkSheets[excelSheetCount].Columns[14].Columns.AutoFit; //列的自動調整
ExcelId.WorkSheets[excelSheetCount].Columns[13].ColumnWidth:=5;//設置一列的寬度
ExcelId.WorkSheets[excelSheetCount].Columns['K:M'].ColumnWidth:=9;//設置多列的寬度
ExcelId.WorkSheets[excelSheetCount].Columns[12].NumberFormatLocal := 'yyyy-mm-dd'; //設置列的顯示格式
Excelid.worksheets[excelSheetCount].range['A4:N5'].Interior.color:=RGB(191,191,191); //設置顏色
Excelid.worksheets[excelSheetCount].range['M4:N4'].Merge(True); //合並單元格