delphi TMS FlexCel 保存Excel
<h1 id="tms-flexcel-保存excel">TMS FlexCel 保存Excel</h1><h2 id="属性和方法">属性和方法</h2>
<h3 id="texcelfileallowoverwritingfiles">TExcelFile.AllowOverwritingFiles</h3>
<pre><code class="language-delphi">property AllowOverwritingFiles: Boolean
</code></pre>
<p>调用<code>Save()</code>是否会自动覆盖现有文件。</p>
<h3 id="texcelfilesave">TExcelFile.Save</h3>
<pre><code class="language-delphi">procedure Save(const fileName: string);
procedure Save(const aStream: TStream);
procedure Save(const fileName: string; const fileFormat: TFileFormats);
procedure Save(const aStream: TStream; const fileFormat: TFileFormats);
procedure Save(const fileName: string; const fileFormat: TFileFormats; const delimiter: Char);
procedure Save(const aStream: TStream; const fileFormat: TFileFormats; const delimiter: Char);
procedure Save(const fileName: string; const fileFormat: TFileFormats; const delimiter: Char; const fileEncoding: TEncoding);
procedure Save(const aStream: TStream; const fileFormat: TFileFormats; const delimiter: Char; const fileEncoding: TEncoding);
</code></pre>
<p>将文件保存到磁盘或流。</p>
<p><strong>重载</strong></p>
<p><code>Save(string)</code> 、 <code>Save(TStream)</code></p>
<p>使用<code>TFileFormats.Automatic</code>文件格式进行保存</p>
<p><strong>参数</strong></p>
<p><em>fileName</em>要保存的文件。</p>
<blockquote>
<p>如果覆盖现有文件 <code>AllowOverwritingFiles</code> 为 <em>false</em>,则<em>fileName</em> 中指定的文件必须不存在。</p>
</blockquote>
<p><em>aStream</em>保存文件的流。 必须是可搜索的流。</p>
<p><em>fileFormat</em>文件格式。 如果<em>fileName</em>存在,自动将尝试从文件名中猜测它。如果文件格式为文本,则将使用制表符作为分隔符。</p>
<p><em>delimiter</em>文件格式为<code>TFil Formats.Text</code>时使用的分隔符</p>
<p><em>fileEncoding</em>编写文本分隔文件(csv 或 txt)时生成的文件的编码。该参数对xls/x文件无效。如果省略,将使用 <code>Encoding.UTF8</code>。</p>
<blockquote>
<p>请注意,要使用 BOM(字节顺序标记)创建文件,您需要在此处指定编码,与使用<code>StreamWriter</code>一样。</p>
</blockquote>
<h3 id="texcelfiledefaultfileformat">TExcelFile.DefaultFileFormat</h3>
<pre><code class="language-delphi">property DefaultFileFormat: TFileFormats
</code></pre>
<p>确定在保存文件时未指定文件格式,以及无法通过文件扩展名确定文件格式时Excel使用的默认文件格式。如果设置为<em>Automatic</em>,文件将以打开时相同的格式保存。也就是说,如果您打开一个xlsx文件,它将被保存为xlsx。如果您打开了一个 xls 文件(或使用<code>XlsFile.NewFile()</code>创建它的),它将被保存为 xls。 当此属性为 <em>Automatic</em> 时,文本文件将保存为 xls。<strong>默认值</strong> <em>Automatic</em>。</p>
<h3 id="tfileformats">TFileFormats</h3>
<p>支持读取和写入文件的文件格式。</p>
<p><strong>unit</strong></p>
<p>FlexCel.Core</p>
<p><em>Automatic</em>打开文件时自动检测文件的类型。如果在保存时使用,FlexCel 将根据文件扩展名(保存到文件时)或 <code>TExcelFile.DefaultFileFormat</code> 的值(保存到流时或无法从扩展名确定格式时)选择使用 xls 还是 xlsx .</p>
<p><em>Xls</em>Excel 97-2000-XP-2003</p>
<p><em>Text</em>分隔符分隔值。根据分隔符的不同,这可以是csv、tab分隔的文本等。</p>
<p><em>Pxl</em>Pocket Excel 1.0 or 2.0</p>
<p><em>Xlsx</em>Excel 2007标准文件格式。注意,这不是一个启用宏的文件格式。如果希望保存带有宏的文件,则需要改用Xlsm。</p>
<p><em>Xlsm</em>Excel 2007 启用宏的文件格式。</p>
<h2 id="例子">例子</h2>
<h3 id="另存为">另存为</h3>
<pre><code class="language-delphi">uses VCL.FlexCel.Core, FlexCel.XlsAdapter;
procedure TForm1.Button7Click(Sender: TObject);
var
Xls: TXlsFile;
begin
//读取Excel文件
Xls := TXlsFile.Create('C:\Users\Administrator\Desktop\ceshi.xlsx');
try
//覆盖已存在文件
Xls.AllowOverwritingFiles := True;
//另存为其他文件名
Xls.Save('C:\Users\Administrator\Desktop\ceshi_副本.xlsx');
//保存为xls格式文件
Xls.Save('C:\Users\Administrator\Desktop\ceshi_.xls');
//保存为csv文件
Xls.Save('C:\Users\Administrator\Desktop\ceshi_.csv', TFileFormats.Text, ',');
//保存为文本文件 ANSI编码
Xls.Save('C:\Users\Administrator\Desktop\ceshi_.txt', TFileFormats.Text, #9, TEncoding.ANSI);
finally
Xls.Free;
end;
end;
</code></pre><br><br>
来源:https://www.cnblogs.com/txgh/p/16466466.html
頁:
[1]