Delphi TMS FlexCel 的使用

它是Delphi用来操作Excel的组件

它拥有读 、写excel的功能。还可以添加excel函数,这里我了解一下它的基础功能。

读取excel

var

 xls: TXlsFile;

begin

xls :=TXlsFile.create(false);

var FILep:='XXX路径';

  if not FileExists(FILep + '.xlsx') then exit;
   xls.Open(FILep + '.xlsx');
    xls.ActiveSheet := 1;//激活某个sheet页

 var value := xls.GetCellValue(1,2);//获取第一行第二列的值

xls.free;

end;

写入excel

var

 xls: TXlsFile;

  fmt, ColFmt: TFlxFormat;

begin

xls :=TXlsFile.create(true);//这里要设置为true

var FILep:='XXX路径';
 if FileExists(FILep + '.xlsx') then
      DeleteFile(FILep + '.xlsx');

      xls.Save(FILep + '.xlsx');//保存
      xls.Open(FILep + '.xlsx');//打开
      xls.ActiveSheet := 1;//激活某个sheet页
      xls.SheetName :=‘sheet页的名字’;

      xls.DefaultColWidth := 900;//默认excel的列宽
      xls.DefaultRowHeight := 800;//默认excel的行高

       xls.SetCellValue(1, 2, 值);
        fmt := xls.GetCellVisibleFormatDef(1, 2);//设置样式
        fmt.FillPattern.Pattern := TFlxPatternStyle.Solid;//实线
        fmt.FillPattern.FgColor := clInactiveBorder;//字体加粗  

          fmt.Format := '0.00%';//设置百分比

       fmt.FillPattern.FgColor := TExcelColor.FromArgb(0, 0, 255);//设置表格区域的颜色
        fmt.FillPattern.BgColor := TExcelColor.Automatic;//颜色自适应

//设置字体

      fmt.Font.Size20 := 480;
      fmt.Font.Style := [TFlxFontStyles.Bold];
      fmt.Font.Family := 3;
      fmt.Font.CharSet := 134;
        xls.SetCellFormat(1, 2, xls.AddFormat(fmt));

  xls.SetCellValue(3, BinCOl + 3, TFormula.Create('{=COUNTIF(' + srow + ',' + scol + ')}', TSingleFormulaValue.Empty, TFormulaSpan.Create(2 1, true)));

设置excel 的筛选函数。这里是筛选表格中元素的个数

//求合函数

     xls.SetCellValue(listBIn.Count + 3, BinCOl + 5 + Site * 2, TFormula.Create('{=SUM(' + scol1 + ')}', TSingleFormulaValue.Empty, TFormulaSpan.Create(1, 1, true)));

    xls.MergeCells(1, 1, 1, xls.colcount);合并表格

      xls.Save(FILep + '.xlsx');//保存
             

xls.free;

end;

实现效果:

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值