zoukankan      html  css  js  c++  java
  • Delphi 总结操作Excel

    定义变量

      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.=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); //合并单元格

  • 相关阅读:
    【Azure Redis 缓存】Azure Redis 功能性讨论二
    【Azure Developer】如何用Microsoft Graph API管理AAD Application里面的Permissions
    【Azure 环境】通过Python SDK收集所有订阅简略信息,例如订阅id 名称, 资源组及组内资源信息等,如何给Python应用赋予相应的权限才能获取到信息呢?
    【Azure 应用服务】App Service与APIM同时集成到同一个虚拟网络后,如何通过内网访问内部VNET的APIM呢?
    【Azure 云服务】如何从Azure Cloud Service中获取项目的部署文件
    【Azure Redis 缓存】Azure Redis 异常
    【Azure 微服务】基于已经存在的虚拟网络(VNET)及子网创建新的Service Fabric并且为所有节点配置自定义DNS服务
    【Azure Redis 缓存】遇见Azure Redis不能创建成功的问题:至少一个资源部署操作失败,因为 Microsoft.Cache 资源提供程序未注册。
    【Azure Redis 缓存】如何得知Azure Redis服务有更新行为?
    【Azure API 管理】在 Azure API 管理中使用 OAuth 2.0 授权和 Azure AD 保护 Web API 后端,在请求中携带Token访问后报401的错误
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/3267640.html
Copyright © 2011-2022 走看看