做一个调试的DLL给你.
library TxtToExcelDll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Dialogs;
{$R *.res}
procedure TxtToExcel(s:string);stdcall
//s为分隔符号
var
TxtFileName:array[0..30]of string;
ExcelFileName:array[0..30]of string;
TxtOpenDlg: TOpenDialog;
i:Integer;
strlst:tstringlist;
StarTime:TDateTime;
begin
strlst:=tstringlist.create;
TxtOpenDlg:= TOpenDialog.Create(nil);
TxtOpenDlg.Filter:='txt file(*.txt)|*txt';
TxtOpenDlg.Options:=[ofAllowMultiSelect];
if TxtOpenDlg.Execute then
for i:=0 to TxtOpenDlg.Files.Count-1 do
begin
TxtFileName:=TxtOpenDlg.Files;
ExcelFileName:=copy(ExtractFileDrive(TxtOpenDlg.Files)+'/'+ExtractfileName(TxtOpenDlg.Files),1,Length(ExtractFileDrive(TxtOpenDlg.Files)+'/'+ExtractfileName(TxtOpenDlg.Files))-3)+'xls';
end;
for i:=0 to TxtOpenDlg.Files.Count-1 do
if not (FileExists(TxtFileName)) then
exit;
try
StarTime:=Now;
for i:=0 to TxtOpenDlg.Files.Count-1 do
begin
strlst.loadfromfile(TxtFileName);
strlst.text:=stringreplace(strlst.text,s,#9,[rfreplaceall]);
// strlst.text:=stringreplace(strlst.text,' ',#9,[rfreplaceall])
//tab分隔
// strlst.text:=stringreplace(strlst.text,',',#9,[rfreplaceall])
//逗号分隔
// strlst.text:=stringreplace(strlst.text,'|',#9,[rfreplaceall])
// |分隔
// strlst.text:=stringreplace(strlst.text,'/',#9,[rfreplaceall])
///分隔
strlst.savetofile(ExcelFileName);
if i=TxtOpenDlg.Files.Count-1 then
ShowMessage('共有'+inttostr(TxtOpenDlg.Files.Count)+'个TXT文件成功导入Excel文件,用时:'+formatDateTime('hh:nn:ss zzz',Now-StarTime)+',Excel文件保存在:'+ExtractFileDrive(TxtOpenDlg.Files)+'/的根目录下。');
// ShowMessage('共有'+inttostr(TxtOpenDlg.Files.Count)+'个TXT文件,现在导入的是第'+inttostr(i+1)+'文件'+TxtFileName+'文件成功导入Excel文件,用时:'+formatDateTime('hh:nn:ss zzz',Now-StarTime)+'Excel文件保存在'+ExcelFileName);
end;
finally
TxtOpenDlg.Free;
strlst.free;
end;
end;
//导出表
exports
TxtToExcel;
begin
end.
主程序调用DLL.
unit totxttoexcel;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ComObj,ActiveX, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TTxtToExcel=procedure(s:string);stdcall;
procedure TForm1.Button1Click(Sender:TObject);
Var
t_handle:Thandle;
TxtToExcel:TTxtToExcel;
s:string;
begin
if Edit1.Text<>'' then
s:=Edit1.Text
else
begin
showmessage('分隔符不为空,请重新输入!');
exit;
end;
t_handle:=loadlibrary('E:/cs/TXTtoExcel/TxtToExcelDll.dll');
if t_handle<32 then
begin
application.MessageBox('TxtToExcelDll.dll动态连接库掉失!','提示',64);
exit;
end
else
try
coinitialize(nil);
@TxtToExcel:=Getprocaddress(t_handle,'TxtToExcel');
TxtToExcel(s);
finally
FreeLibrary(t_handle);
couninitialize();
end;
end;
end.
其他人有更好的方法吗?这个方法感觉不怎么好.都是一次性导入,没有控制.