将TXT文本内容直接导入至Excel(100分)

  • 主题发起人 主题发起人 cdj1688
  • 开始时间 开始时间
C

cdj1688

Unregistered / Unconfirmed
GUEST, unregistred user!
1.将TXT文本直接输入Excel文件
2.打开选择TXT文件文件(可多选)
3.输出的Excel文件保存至根目录下,文件名与TXT文件同名.(当选择多个TXT,怎么生成对应文件的Excel文件保存至根目录下呢??)
 
录制一个宏,然后把这段宏转化为DELPHI,然后就是循环.如果是CSV类型的文本,那宏也不需要了,只是处理字符串的问题了.
 
有一个第三方控件,想试一下吗?
QQ:292044357
 
用 SMExport
 
SMExport没用过,能给点代码看看吗?我的问题是要在D7中的程序中实现,最好是做成DLL的,如果用的数据集什么的最好也是ADO的.
 
做一个调试的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.
其他人有更好的方法吗?这个方法感觉不怎么好.都是一次性导入,没有控制.
 
楼上的不错.
 
接受答案了.
 
后退
顶部