to gobiz :我现在已经能够实现用你所提示的方法1实现写入;但是每次的写入都是写到一个新的excel表中,请问有没有办法实现写到指定的表吗? 我的实现方法如下(参考了网上的资料): while not eof(txtfile) do begin readln(txtfile,readStr); readfloat := strtofloat(readstr); arXlsNumber[2] := row arXlsNumber[3] := column; aFileStream.WriteBuffer(arXlsNumber, SizeOf(arXlsNumber)); aFileStream.WriteBuffer(readfloat,8); end; aFileStream.WriteBuffer(arXlsEnd,sizeof(arXlsEnd)); closefile(txtfile); aFileStream.Free;
Creates an instance of TFileStream.Delphi syntax:constructor Create(const FileName: string; Mode: Word); overload; constructor Create(const FileName: string; Mode: Word; Rights: Cardinal); overload; DescriptionCall Create to instantiate a file stream for reading from or writing to the named file. Specify the name of the file and the way the file should be opened as parameters.The Mode parameter indicates how the file is to be opened. The Mode parameter consists of an open mode and (possibly) a share mode or抏d together. The open mode must be one of the following values:Value MeaningfmCreate Create a file with the given name. If a file with the given name exists, open the file in write mode. fmOpenRead Open the file for reading only. fmOpenWrite Open the file for writing only. Writing to the file completely replaces the current contents. fmOpenReadWrite Open the file to modify the current contents rather than replace them.The share mode must be one of the following values:Value MeaningfmShareCompat Sharing is compatible with the way FCBs are opened. fmShareExclusive Other applications can not open the file for any reason. fmShareDenyWrite Other applications can open the file for reading but not for writing. fmShareDenyRead Other applications can open the file for writing but not for reading. fmShareDenyNone No attempt is made to prevent other applications from reading from or writing to the file.The Rights parameter indicates the permission bits with which to create the file on Linux when Mode is fmCreate. Rights is ignored when used on the Windows platform.If the file can not be opened, Create raises an exception.
to gobiz :谢谢你的关注,但是我做的还是不对,能继续帮我看看吗? 我创建流的时候是, aFileStream := TFileStream.Create(DensiFile,fmopenreadwrite or fmsharedenynone); //写文件头 aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin)); 然后执行我上面写的程序,但是最后出来的还是只有一个表格,本来是有三个表的。
to gobiz :谢谢你的关注,但是我做的还是不对,能继续帮我看看吗? 我创建流的时候是, aFileStream := TFileStream.Create(DensiFile,fmopenreadwrite or fmsharedenynone); //写文件头 aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin)); 然后执行我上面写的程序,但是最后出来的还是只有一个表格,本来是有三个表的。 ===========================================================================================三个表格是什么意思?你是说三个WorkSheet吗?用流模式生成的Excel目前好像只能生成一个WorkSheet的。如果你想生成3个WorkSheet,那么你可以去网上放狗找EMS Excel Report或者XLSReadWriteII!我印象中好像可以实现的,但是速度要稍微慢一些的!
方法2:如果你的文本文件是使用类似一行一个记录,每个字段之间用,分割的话,那么你干脆就把你的文本文件的后缀名修改为.csv,然后就可以直接用excel打开了!
我的实现方法如下(参考了网上的资料):
while not eof(txtfile) do
begin
readln(txtfile,readStr);
readfloat := strtofloat(readstr);
arXlsNumber[2] := row
arXlsNumber[3] := column;
aFileStream.WriteBuffer(arXlsNumber, SizeOf(arXlsNumber));
aFileStream.WriteBuffer(readfloat,8);
end;
aFileStream.WriteBuffer(arXlsEnd,sizeof(arXlsEnd));
closefile(txtfile);
aFileStream.Free;
constructor Create(const FileName: string; Mode: Word; Rights: Cardinal); overload; DescriptionCall Create to instantiate a file stream for reading from or writing to the named file. Specify the name of the file and the way the file should be opened as parameters.The Mode parameter indicates how the file is to be opened. The Mode parameter consists of an open mode and (possibly) a share mode or抏d together. The open mode must be one of the following values:Value MeaningfmCreate Create a file with the given name. If a file with the given name exists, open the file in write mode.
fmOpenRead Open the file for reading only.
fmOpenWrite Open the file for writing only. Writing to the file completely replaces the current contents.
fmOpenReadWrite Open the file to modify the current contents rather than replace them.The share mode must be one of the following values:Value MeaningfmShareCompat Sharing is compatible with the way FCBs are opened.
fmShareExclusive Other applications can not open the file for any reason.
fmShareDenyWrite Other applications can open the file for reading but not for writing.
fmShareDenyRead Other applications can open the file for writing but not for reading.
fmShareDenyNone No attempt is made to prevent other applications from reading from or writing to the file.The Rights parameter indicates the permission bits with which to create the file on Linux when Mode is fmCreate. Rights is ignored when used on the Windows platform.If the file can not be opened, Create raises an exception.
我创建流的时候是, aFileStream := TFileStream.Create(DensiFile,fmopenreadwrite or fmsharedenynone);
//写文件头
aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin));
然后执行我上面写的程序,但是最后出来的还是只有一个表格,本来是有三个表的。
我创建流的时候是, aFileStream := TFileStream.Create(DensiFile,fmopenreadwrite or fmsharedenynone);
//写文件头
aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin));
然后执行我上面写的程序,但是最后出来的还是只有一个表格,本来是有三个表的。
===========================================================================================三个表格是什么意思?你是说三个WorkSheet吗?用流模式生成的Excel目前好像只能生成一个WorkSheet的。如果你想生成3个WorkSheet,那么你可以去网上放狗找EMS Excel Report或者XLSReadWriteII!我印象中好像可以实现的,但是速度要稍微慢一些的!