它是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;
实现效果: