您的位置:首页 > 编程语言 > Delphi

delphi要控制EXCEL时用的,不错,转了。

2016-01-29 15:29 405 查看
定义变量

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); //合并单元格
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: