M
muxj
Unregistered / Unconfirmed
GUEST, unregistred user!
如何在一个窗体中打开EXCEL表,并可编辑(用什么控件?)
既然大家都在这里将自己的东西贴出来,那我就再贴一个,将DBGrid中的文件转换到Excel中或者转换到Txt中的控件。
我自己编写的,希望大家讨论一下。
unit DBGridExport;
interface
uses
SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBGrids, Comobj, extctrls, comctrls, ActiveX;
type
TSpaceMark = (csComma, csSemicolon, csTab, csBlank, csEnter);
TDBGridExport = class(TComponent)
private
FDB_Grid: TDBGrid; {读取DBGrid的源}
FTxtFileName: string; {文本文件名}
FSpaceMark: TSpaceMark; {间隔符号}
FSpace_Ord: Integer; {间隔符号的Asc数值}
FTitle: string; {显示的标题}
FSheetName: string; {工作表标题}
FExcel_Handle: OleVariant; {Excel的句柄}
FWorkbook_Handle: OleVariant; {书签的句柄}
FShow_Progress: Boolean; {是否显示插入进度}
FProgress_Form: TForm; {进度窗体}
FRun_Excel_Form: TForm; {启动Excel提示窗口}
FProgressBar: TProgressBar; {进度条}
function Connect_Excel: Boolean; {启动Excel}
function New_Workbook: Boolean; {插入新的工作博}
function InsertData_To_Excel: Boolean; {插入数据}
procedure Create_ProgressForm(AOwner: TComponent); {创建进度显示窗口}
procedure Create_Run_Excel_Form(AOwner: TComponent); {创建启动Excel窗口}
procedure SetSpaceMark(Value: TSpaceMark); {设置导出时的间隔符号}
protected
public
constructor Create(AOwner: TComponent); override; {新建}
destructor Destroy; override; {销毁}
function Export_To_Excel: Boolean; overload; {导出到Excel中}
function Export_To_Excel(DB_Grid: TDBGrid): Boolean; overload;
function Export_To_Txt(NewFile: Boolean = True): Boolean; overload; {导出到文本文件中}
function Export_To_Txt(FileName: string; NewFile: Boolean = True): Boolean; overload;
function Export_To_Txt(DB_Grid: TDBGrid; NewFile: Boolean = True): Boolean; overload;
function Export_To_Txt(FileName: string; DB_Grid: TDBGrid; NewFile: Boolean = True): Boolean; overload;
published
property DB_Grid: TDBGrid read FDB_Grid write FDB_Grid;
property Show_Progress: Boolean read FShow_Progress write FShow_Progress;
property TxtFileName: string read FTxtFileName write FTxtFileName;
property SpaceMark: TSpaceMark read FSpaceMark write SetSpaceMark;
property Title: string read FTitle write FTitle;
property SheetName: string read FSheetName write FSheetName;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Stone', [TDBGridExport]);
end;
{-------------------------------------------------------------------------------}
{新建}
constructor TDBGridExport.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShow_Progress := True;
FSpaceMark := csTab;
end;
{销毁}
destructor TDBGridExport.Destroy;
begin
varClear(FExcel_Handle);
varClear(FWorkbook_Handle);
inherited Destroy;
end;
{===============================================================================}
{导出到文本文件中}
function TDBGridExport.Export_To_Txt(NewFile: Boolean = True): Boolean;
var
Txt: TStrings;
Tmp_Str: string;
data_Str: string;
i, j: Integer;
Column_name: string;
Data_Set: TDataSet;
bookmark: pointer;
Before_Scroll, Afrer_Scroll: TDataSetNotifyEvent;
begin
Result := False;
if NewFile = True then
FTxtFileName := '';
if FTxtFileName = '' then
begin
with TSaveDialog.Create(nil) do
begin
Title := '请选择输出文件名';
DefaultExt := 'txt';
Filter := '文本文件(*.Txt)|*.txt';
Options :=