一剑笑红尘 發表於 2022-6-20 10:20:00

delphi TMS FlexCel常用功能

<h1 id="tms-flexcel常用功能">TMS FlexCel常用功能</h1>
<h2 id="属性和方法">属性和方法</h2>
<h3 id="txlsfilecreate">TXlsFile.Create</h3>
<pre><code>constructor Create;
constructor Create(const aAllowOverwritingFiles: Boolean);
constructor Create(const aFileName: string);
constructor Create(const aFileName: string; const aAllowOverwritingFiles: Boolean);
constructor Create(const aSheetCount: Integer; const aAllowOverwritingFiles: Boolean);
constructor Create(const aStream: TStream; const aAllowOverwritingFiles: Boolean);
constructor Create(const aSheetCount: Integer; const aFileFormat: TExcelFileFormat; const aAllowOverwritingFiles: Boolean);
</code></pre>
<p>创建一个新的 <code>XlsFile</code>。</p>
<p><strong>重载</strong></p>
<p><code>Create</code>、<code>Create(Boolean)</code></p>
<p>创建一个新的 <code>XlsFile</code>。 使用此构造函数创建 <code>XlsFile</code> 后,需要打开或创建新文件。</p>
<p><code>Create(string)</code> 、 <code>Create(string, Boolean)</code>、 <code>Create(TStream, Boolean)</code></p>
<p>创建一个新的 XlsFile 并打开所需的文件。</p>
<p><code>Create(Integer, Boolean)</code> 、 <code>Create(Integer, TExcelFileFormat, Boolean)</code></p>
<p>创建一个新的 <code>XlsFile</code> 并创建一个具有所需张数的新空文件。</p>
<p><strong>参数</strong></p>
<p><em>aAllowOverwritingFiles</em>当为<em>true</em>时,调用 <code>Save</code> 将覆盖现有文件。</p>
<p><em>aFileName</em>要打开的文件的名称。</p>
<p><em>aSheetCount</em>新空文件的工作表数。</p>
<p><em>aStream</em>要打开的文件流。</p>
<p><em>aFileFormat</em>用于创建新文件的 Excel 版本。 不同的版本可以有不同的默认字体、列宽等。</p>
<h3 id="texcelfileformat">TExcelFileFormat</h3>
<p>不同的 Excel 版本会创建不同的空 xls/xlsx 文件。例如,Excel 2003创建的空xls文件的默认字体为“Arial”,Excel 2007创建的文件的默认字体为“Calibri”。默认情况下,当你调用<code>TExcelFile.NewFile</code>时,FlexCel会创建一个类似于Excel 2003创建的文件。但是如果你想从一个空的Excel 2007文件开始,你可以通过这个枚举调用<code>TExcelFile.NewFile</code>来完成。</p>
<p><strong>unit</strong></p>
<p>FlexCel.Core</p>
<ul>
<li><em>v2003</em>将创建空文件,就像Excel 2003创建的一样。默认字体为 Arial。</li>
<li><em>v2007</em>将创建空文件,就像Excel 2007创建的一样。默认字体为 Calibri。</li>
<li><em>v2010</em>将创建空文件,就像Excel 2010创建的一样。默认字体为 Calibri。</li>
<li><em>v2013</em>将创建空文件,就像Excel 2013创建的一样。默认字体为 Calibri。</li>
<li><em>v2016</em>将创建空文件,就像Excel 2016创建的一样。默认字体为 Calibri。</li>
<li><em>v2019</em>将创建空文件,就像Excel 2019创建的一样。默认字体为 Calibri。</li>
</ul>
<h3 id="txlsfilesheetcount">TXlsFile.SheetCount</h3>
<pre><code class="language-delphi">property SheetCount: Integer
</code></pre>
<p>文件中工作表数。</p>
<h3 id="txlsfilerowcount">TXlsFile.RowCount</h3>
<pre><code class="language-delphi">property RowCount: Integer
</code></pre>
<p>工作表上实际使用的行数。</p>
<h3 id="txlsfilecolcount">TXlsFile.ColCount</h3>
<pre><code class="language-delphi">property ColCount: Integer
</code></pre>
<p>活动工作表上实际使用的列数,包括格式化的列。</p>
<blockquote>
<p><strong>请注意,此方法很慢</strong>,因为它需要遍历所有行以找出最大的使用列。 使用 <code>TExcelFile.ColCountInRow</code>会更快。 如果需要使用 <code>ColCount</code>,首先缓存它的值:</p>
<pre><code class="language-pas">var
RowCount: Int32;
ColCount: Int32;
row: Int32;
col: Int32;
...
RowCount := xls.RowCount;
ColCount := xls.ColCount;
for row := 1 to RowCount do
begin
for col := 1 to ColCount do//使用 ColCountInRow 会更快。
begin
   DoSomething(row, col);
end;
end;
</code></pre>
<p>通常使用 <code>TExcelFile.ColCountOnlyData</code> 代替。 <code>ColCount</code> 将返回包含数据和格式化列的最大列,<code>ColCountOnlyData</code> 方法返回只包含带有数据的单元格。</p>
</blockquote>
<h3 id="texcelfilegetcellvalue">TExcelFile.GetCellValue</h3>
<pre><code class="language-delphi">function GetCellValue(const row: Integer; const col: Integer): TCellValue;
</code></pre>
<p>读取单元格值。</p>
<p><strong>参数</strong></p>
<p><em>row</em> 行,索引从<em>1</em>开始。</p>
<p><em>col</em>   列,索引从<em>1</em>开始。</p>
<p><strong>返回值</strong></p>
<p><code>TCellValue</code>的对象。 它可以是 <em>null</em>、浮点数、字符串、布尔值、<code>TFormula</code>、<code>TFlxFormulaErrorValue</code> 或 <code>TRichString</code>。 日期以双精度形式返回。</p>
<blockquote>
<p>此方法将返回存储在单元格上的实际值。 例如,如果将“1.3”格式化为“1.30”,GetCellValue 将返回数字 1.3。 要获取具有格式化值的字符串,请参阅 <code>GetStringFromCell</code></p>
</blockquote>
<h3 id="txlsfilesetcellvalue">TXlsFile.SetCellValue</h3>
<pre><code class="language-delphi">procedure SetCellValue(const row: Integer; const col: Integer; const value: TCellValue; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: string; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: TRichString; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: TDateTime; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: Double; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: Single; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: Integer; const XF: Integer = -1);
procedure SetCellValue(const row: Integer; const col: Integer; const value: Int64; const XF: Integer = -1);
procedure SetCellValue(const sheet: Integer; const row: Integer; const col: Integer; const value: TCellValue; const XF: Integer);
procedure SetCellValue(const sheet: Integer; const row: Integer; const col: Integer; const value: string; const XF: Integer);
procedure SetCellValue(const sheet: Integer; const row: Integer; const col: Integer; const value: Double; const XF: Integer);
procedure SetCellValue(const sheet: Integer; const row: Integer; const col: Integer; const value: Integer; const XF: Integer);
</code></pre>
<p>设置单元格的值和格式。</p>
<p><strong>参数</strong></p>
<p><em>sheet</em>工作表编号,从 1 开始。</p>
<p><em>row</em>行,索引从<em>1</em>开始。</p>
<p><em>col</em>列,索引从<em>1</em>开始。</p>
<p><em>value</em>要设置的值。</p>
<p><em>XF</em>   要设置的格式。您通常使用 <code>TExcelFile.AddFormat</code> 函数获得此数字。 使用 <em>-1</em> 保持格式不变。 <strong>默认值</strong> <em>-1</em>。</p>
<blockquote>
<p>此方法将输入传递给它的对象的数据类型。例如,如果设置 <code>value="1"</code>,将在单元格中输入字符串“1”。 要将字符串转换为最佳表示形式(在本例中为数字),请使用 <code>TExcelFile.SetCellFromString(Integer, Integer, TRichString, Integer)</code> 要输入 HTML 格式的字符串,请使用 <code>TExcelFile.SetCellFromHtml(Integer, Integer, string, Integer)</code></p>
</blockquote>
<h2 id="例子">例子</h2>
<h3 id="读取excel">读取EXCEL</h3>
<pre><code>uses VCL.FlexCel.Core, FlexCel.XlsAdapter;

procedure TForm1.Button1Click(Sender: TObject);
var
Xls: TXlsFile;
Sheet, Row, Col: Integer;
Cell: TCellValue;
Addr: TCellAddress;
begin
//加载excel文件
Xls := TXlsFile.Create('C:\Users\Administrator\Desktop\ceshi.xlsx');
try
    //循环所有Sheet
    for Sheet := 1 to Xls.SheetCount do
    begin
      Xls.ActiveSheet := Sheet;
      Memo1.Lines.Add('Sheet ' + Xls.SheetName);
      Memo1.Lines.Add('总行数 ' + Xls.RowCount.ToString);
      Memo1.Lines.Add('总列数 ' + Xls.ColCount.ToString);
      //循环所有行
      for Row := 1 to Xls.RowCount do
      begin
      //循环所有列
      for Col := 1 to Xls.ColCount do
      begin
          //获取单元格
          Cell := Xls.GetCellValue(Row, Col);
          //获取单元格的字符串标识
          Addr := TCellAddress.Create(Row, Col);
          Memo1.Lines.Add('单元格[' + Row.ToString + ',' + Col.ToString + ']' +
            '名称[' + Addr.CellRef + ']值[' + Cell.ToString + ']');
      end;
      end;
      Memo1.Lines.Add('--------------------');
    end;
finally
    Xls.Free;
end;
end;
</code></pre>
<h3 id="写入excel">写入EXCEL</h3>
<pre><code>uses VCL.FlexCel.Core, FlexCel.XlsAdapter;

procedure TForm1.Button2Click(Sender: TObject);
var
Xls: TXlsFile;
Fmt: TFlxFormat;
XF: Integer;
begin
//创建一个新的空Excel文件,使用Excel 2019格式
//Excel 2003的默认字体为Arial,Excel 2019的默认字体为Calibri。
Xls := TXlsFile.Create(1, TExcelFileFormat.v2019, true);
try
    //添加字体为宋体的格式
    Fmt := Xls.GetDefaultFormat;
    Fmt.Font.Name := '宋体';
    Fmt.Font.Scheme := TFontScheme.None;
    XF := Xls.AddFormat(fmt);
    //在 A1 中输入一个字符串
    Xls.SetCellValue(1, 1, '测试内容', XF);
    //在 A2 中输入一个整数(Excel中的所有数字都是浮点数,整数也会被存储为浮点数)
    Xls.SetCellValue(2, 1, 7);
    //在 A3 中输入一个浮点数
    Xls.SetCellValue(3, 1, 11.3);
    //在 A4 中输入公式
    Xls.SetCellValue(4, 1, TFormula.Create('=Sum(A2:A3)'));
    //保存文件
    Xls.Save('C:\Users\Administrator\Desktop\ceshi.xlsx');
finally
    Xls.Free;
end;
end;
</code></pre><br><br>
来源:https://www.cnblogs.com/txgh/p/16392384.html
頁: [1]
查看完整版本: delphi TMS FlexCel常用功能