我有个例子,是从excel读取数据的,该一下就可以往excel写数据
关于流,我就不会了
unit UCreateSql;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ComObj, StdCtrls, ComCtrls;
type
TCreateSqlFom = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
EX_Start_Line: TEdit;
Label2: TLabel;
EX_Last_Line: TEdit;
Label3: TLabel;
EX_EN_FieldName: TEdit;
Label4: TLabel;
EX_Type_Line: TEdit;
Label5: TLabel;
EX_Length_Line: TEdit;
Label6: TLabel;
EX_Memo_Line: TEdit;
Label7: TLabel;
StartCreate: TButton;
GroupBox2: TGroupBox;
DO_Process: TProgressBar;
Label8: TLabel;
EX_FileName: TEdit;
OpenFile: TButton;
GroupBox3: TGroupBox;
StatusBar1: TStatusBar;
SQL_String: TMemo;
EX_CH_FieldName: TEdit;
EX_File_Open: TOpenDialog;
Label9: TLabel;
DataBase: TEdit;
procedure OpenFileClick(Sender: TObject);
procedure StartCreateClick(Sender: TObject);
private
{ Private declarations }
MyExcelApp : Variant;
MySheet : Variant;
EXFileName : String;
EXStartRow : Integer;
EXLastRow : Integer;
EXCHFileNameCol: Integer;
EXENFileNameCol: Integer;
EXTypeCol : Integer;
EXLengthCol : Integer;
EXMemoRow : Integer;
EXTableName : String;
EXKeyField : Array[0..5] of String;
EXKeyCount :Integer;
function InitOleObject(FileName:String):Boolean;
function FinalOleObject:Boolean;
function CheckInput:Boolean;
procedure GetEXInfo;
procedure BeforeCreateSql;
procedure StartCreateSql;
public
{ Public declarations }
end;
var
CreateSqlFom: TCreateSqlFom;
implementation
{$R *.dfm}
function TCreateSqlFom.FinalOleObject: Boolean;
begin
try
MyExcelApp.WorkBooks.Close;
Result:=True;
except
end;
end;
procedure TCreateSqlFom.GetEXInfo;
begin
SQL_String.Text:='';
EXFileName := EX_FileName.Text;
EXStartRow := StrToInt(EX_Start_Line.Text);
EXLastRow := StrToInt(EX_Last_Line.Text);
EXCHFileNameCol := StrToInt(EX_CH_FieldName.Text);
EXENFileNameCol := StrToInt(EX_EN_FieldName.Text);
EXTypeCol := StrToInt(EX_Type_Line.Text);
EXLengthCol := StrToInt(EX_Length_Line.Text);
EXMemoRow := StrToInt(EX_Memo_Line.Text);
EXTableName :='';
EXKeyCount :=0;
DO_Process.Position:=0;
DO_Process.Max := EXLastRow-EXStartRow+1;
end;
function TCreateSqlFom.InitOleObject(FileName:String): Boolean;
begin
Result:=False;
try
MyExcelApp := CreateOleObject( 'Excel.Application' );
except
messagebox(0,'Excel没有正确安装!','错误',mb_iconerror);
exit;
end;
MyExcelApp.Visible := false; // 让对象Excel不可视
try
MyExcelApp.WorkBooks.Open(FileName) ;
except
messagebox(0,'Excel文件不存在!','错误',mb_iconerror);
exit;
end;
MySheet:=MyExcelApp.ActiveSheet;
Result:=True;
end;
function TCreateSqlFom.CheckInput: Boolean;
begin
Result:=False;
if Trim(EX_FileName.Text)='' then
begin
MessageBox(Handle,'文件名没有填','注意!',MB_OK);
Exit;
end;
Result:=True;
end;
procedure TCreateSqlFom.OpenFileClick(Sender: TObject);
begin
if EX_File_Open.Execute then
begin
EX_FileName.Text:=EX_File_Open.FileName;
end;
end;
procedure TCreateSqlFom.StartCreateSql;
var
RowCount : Integer;
KeyCount : Integer;
KeyStr : String;
begin
try
for RowCount:=EXStartRow to EXLastRow Do
begin
if Trim(Mysheet.cells[RowCount, 1].value)='TableName' then
begin
EXTableName:=Mysheet.cells[RowCount, EXENFileNameCol].value;
SQL_String.Lines.Add('/*-- =============================================*/');
SQL_String.Lines.Add('/*'+Mysheet.cells[RowCount, EXENFileNameCol+1].value
+',表名:'
+Mysheet.cells[RowCount, EXENFileNameCol].value
+'*/');
SQL_String.Lines.Add('/*-- =============================================*/');
SQL_String.Lines.Add('CREATE TABLE '+Mysheet.cells[RowCount, EXENFileNameCol].value);
SQL_String.Lines.Add('(');
end;
if Trim(Mysheet.cells[RowCount, 1].value)='FieldName' then
begin
if Trim(Mysheet.cells[RowCount, EXTypeCol].value)='Nvarchar' then
begin
if Trim(Mysheet.cells[RowCount, EXMemoRow].value)='PK' then
begin
EXKeyField[EXKeyCount]:=Mysheet.cells[RowCount, EXENFileNameCol].value;
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value
+'('
+Trim(Mysheet.cells[RowCount, EXLengthCol].value)
+')'
+' Not Null,'
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
EXKeyCount:=EXKeyCount+1;
end else if Trim(Mysheet.cells[RowCount, EXMemoRow].value)='PK,自增长' then
begin
EXKeyField[EXKeyCount]:=Mysheet.cells[RowCount, EXENFileNameCol].value;
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value
+'('
+Trim(Mysheet.cells[RowCount, EXLengthCol].value)
+')'
+'identity (1, 1) Not Null,'
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
EXKeyCount:=EXKeyCount+1;
end else
begin
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value
+'('
+Trim(Mysheet.cells[RowCount, EXLengthCol].value)
+')'
+','
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
end;
end else
begin
if Trim(Mysheet.cells[RowCount, EXMemoRow].value)='PK' then
begin
EXKeyField[EXKeyCount]:=Mysheet.cells[RowCount, EXENFileNameCol].value;
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value
+' Not Null,'
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
EXKeyCount:=EXKeyCount+1;
end else if Trim(Mysheet.cells[RowCount, EXMemoRow].value)='PK,自增长' then
begin
EXKeyField[EXKeyCount]:=Mysheet.cells[RowCount, EXENFileNameCol].value;
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value
+'identity (1, 1) Not Null,'
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
EXKeyCount:=EXKeyCount+1;
end else
begin
SQL_String.Lines.Add(' '
+Mysheet.cells[RowCount, EXENFileNameCol].value
+' '
+Mysheet.cells[RowCount, EXTypeCol].value+','
+' '
+'/*'
+Mysheet.cells[RowCount, EXCHFileNameCol].value
+'*/');
end;
end;
end;
KeyStr:='';
if (Trim(Mysheet.cells[RowCount, 1].value)='') and (EXTableName<>'') then
begin
if EXKeyCount>0 then
begin
for KeyCount:=0 to EXKeyCount-1 do
begin
KeyStr:=KeyStr+EXKeyField[KeyCount]+',';
end;
while copy(KeyStr,Length(KeyStr),1)=',' do
begin
KeyStr:=copy(KeyStr,1,Length(KeyStr)-1)
end;
SQL_String.Lines.Add(' '+'primary key ('+KeyStr+')');
end;
SQL_String.Lines.Add(')');
SQL_String.Lines.Add('');
EXTableName:='';
EXKeyCount:=0;
end;
if (RowCount= EXLastRow) and (EXTableName<>'') then
begin
if EXKeyCount>0 then
begin
for KeyCount:=0 to EXKeyCount-1 do
begin
KeyStr:=KeyStr+EXKeyField[KeyCount]+',';
end;
while copy(KeyStr,Length(KeyStr),1)=',' do
begin
KeyStr:=copy(KeyStr,1,Length(KeyStr)-1)
end;
SQL_String.Lines.Add(' '+'primary key ('+KeyStr+')');
end;
SQL_String.Lines.Add(')');
SQL_String.Lines.Add('');
EXTableName:='';
EXKeyCount:=0;
end;
DO_Process.Position:=DO_Process.Position+1;
Application.ProcessMessages;
end;
except
end;
end;
procedure TCreateSqlFom.BeforeCreateSql;
begin
SQL_String.Lines.Add('USE master');
SQL_String.Lines.Add('GO');
SQL_String.Lines.Add('CREATE DATABASE'+' '+DataBase.Text);
SQL_String.Lines.Add('ON');
SQL_String.Lines.Add(' (NAME = '+DataBase.Text+'_Dat,');
SQL_String.Lines.Add(' FILENAME = ''c:/program files/microsoft sql server/mssql/data/'+DataBase.Text+'dat.mdf'',');
SQL_String.Lines.Add(' SIZE = 10,');
SQL_String.Lines.Add(' MAXSIZE = 50,');
SQL_String.Lines.Add(' FILEGROWTH = 5 )');
SQL_String.Lines.Add('LOG ON');
SQL_String.Lines.Add(' ( NAME = '''+DataBase.Text+'_log'',');
SQL_String.Lines.Add(' FILENAME = ''c:/program files/microsoft sql server/mssql/data/'+DataBase.Text+'log.ldf'',');
SQL_String.Lines.Add(' SIZE = 5MB,');
SQL_String.Lines.Add(' MAXSIZE = 25MB,');
SQL_String.Lines.Add(' FILEGROWTH = 5MB )');
SQL_String.Lines.Add('GO');
SQL_String.Lines.Add('');
SQL_String.Lines.Add('USE '+' '+DataBase.Text);
SQL_String.Lines.Add('GO');
end;
procedure TCreateSqlFom.StartCreateClick(Sender: TObject);
begin
if Not CheckInput then Exit;
try
GetEXInfo;
InitOleObject(EXFileName);
BeforeCreateSql;
StartCreateSql;
finally
FinalOleObject;
end;
end;
end.