最近我做了个系统,用的ACCESS数据库,但有许多用户以前的都是用的EXCEL,现在我想做一个数据导入模块,不知道怎么做,请各位大哥帮忙一下,万分感谢。
解决方案 »
- Clientdataset + DBGrid 实现多表头...
- 一个form里有多个Image 同时更新内容.....进来看吧,题目发不下
- 急,那里有delphi7的汉化补丁啊?
- 如何打包和解包
- 怎么在弹出一个对话框时让声卡发出“咚”的一声响?
- HOWTO Edit Excel Data with ADO -- 如何删除一行?(使用了ADODataSet连接,DBGrid显示)
- 看看这两段代码有何不同!!!
- 编了一个播放器,想做出像WINAMP或MediaPlayer那样的SKIN,我该用什么控件?
- 紧急:请问高手们,如何屏蔽掉别的程序的鼠标右建功能?
- 提议我们搞软件的成立一个组织,及时沟通交流,大家看如何?
- idftp的问题,头疼!!!
- Delphi下如何读写多字符串值(REG_MULTI_SZ类型)的键值
1、使用ADO读取Excel,然后写入Access
2、使用Delphi中Server页中的控件或自己创建OLE对象读取Excel,然后写入Access
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
ComObj;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure setEclStyle;
procedure drawImg;
function getColChar(col: Integer): string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}var
eclApp: Variant;
imgFile: string;const
rowStart = 6;
colStart = 3;
//生成99乘法表
procedure TForm1.Button1Click(Sender: TObject);
var
i,j: Integer;
row,col: Integer;
begin
try
//创建OLE对象Excel Application与 WorkBook
eclApp:= CreateOleObject('Excel.Application');
eclApp.workBooks.Add;
except
messagebox(handle,'您的机器里可能未安装Microsoft Excel或异常','无法生成',MB_OK or MB_ICONWARNING);
Exit;
end; eclApp.visible:= True; Hide; //生成过程将程序隐藏
try
for i:= 1 to 9 do
begin
eclApp.Cells(rowStart, colStart + i):= i;
eclApp.Cells(rowStart + i, colStart):= i;
for j:= 1 to 9 do
begin
row:= rowStart + i;
col:= colStart + j;
eclApp.Cells(row, col):= i * j; //设置字体,底框颜色 颜色值为 $00BBGGRR
if i < j then
begin
eclApp.Cells.item[row,col].Font.Color:= $00FF0000;
eclApp.Cells.item[row,col].Interior.Pattern:= 1;
eclApp.cells.item[row,col].Interior.ColorIndex:= 12;
end
else if i > j then
begin
eclApp.Cells.item[row,col].Font.Color:= $0000FF00;
eclApp.Cells.item[row,col].Interior.Pattern:= 1;
eclApp.cells.item[row,col].Interior.ColorIndex:= 9;
end
else begin
eclApp.Cells.item[row,col].Font.Color:= $000000FF;
eclApp.Cells.item[row,col].Interior.Pattern:= 1;
eclApp.cells.item[row,col].Interior.ColorIndex:= 6;
end;
end;
end;
SetEclStyle;
//设置纸线为横向
eclApp.ActiveSheet.PageSetup.Orientation:= 2;
finally
Show;
end;
Application.BringToFront; //将应用程序提前
messagebox(handle,'生成完毕,您可以再进行编辑并存盘','生成成功',MB_OK or MB_ICONINFORMATION);
end;//关闭程序时,释放Excel应用对象
procedure TForm1.FormDestroy(Sender: TObject);
begin
//关闭eclApp
//若不进行这一步,在任务管理器中会驻留Excel进程
//但可以不必关闭Excel (eclApp.Quit),这样,操作员可以继续编辑或打印表格
eclApp:= Unassigned;
end;//设置表格的显示样式
procedure TForm1.setEclStyle;
var
Sheet, Range:Variant;
s: string;
rct: TRect;
begin Sheet:= eclApp.WorkBooks[1].WorkSheets[1]; s:= Format('%s%d:%s%d',[getColChar(colStart),
rowStart,
getColChar(colStart + 9),
rowStart + 9]);
Range:= Sheet.Range[s]; //得到区域对象 Range.Font.Name:= 'Tahoma';
Range.Font.Bold:= True; //设置区域的外框线型
// 1: Left, 2: Right, 3: Top, 4: bottom
// 5: \, 6: / (斜线)
// 7: Left, 8: Top, 9: bottom, 10: Right (不包括内部单元格)
// 11, 12 , 内部单元格线条 (分别是竖线和横线,12我用了没效果)
Range.Borders[7].LineStyle:= 1;
Range.Borders[7].Weight:= 3;
Range.Borders[8].LineStyle:= 1;
Range.Borders[8].Weight:= 3;
Range.Borders[9].LineStyle:= 1;
Range.Borders[9].Weight:= 3;
Range.Borders[10].LineStyle:= 1;
Range.Borders[10].Weight:= 3; Range.Borders[11].LineStyle:= 1;
Range.Borders[11].Weight:= 2;
Range.Borders[11].ColorIndex:= 2;
Range.Borders[12].LineStyle:= 1;
Range.Borders[12].Weight:= 2;
Range.Borders[12].ColorIndex:= 2; //水平,垂直对齐方式
Range.HorizontalAlignment:= 3;
Range.VerticalAlignment:= 2;
//单元格行高,列宽
Range.ColumnWidth:= 9;
Range.RowHeight:= 18; //设置表头
s:= Format('%s%d:%0:s%2:d',[getColChar(colStart),
rowStart,
rowStart + 9]);
Range:= Sheet.Range[s]; //得到区域对象
Range.ColumnWidth:= 5;
Range.Interior.Pattern:= 1;
Range.Interior.ColorIndex:= 3; s:= Format('%s%d:%s%1:d',[getColChar(colStart + 1),
rowStart,
getColChar(colStart + 9)]);
Range:= Sheet.Range[s]; //得到区域对象
Range.Interior.Pattern:= 1;
Range.Interior.ColorIndex:= 3; //表格标题
s:= Format('%s%d:%s%1:d',[getColChar(colStart),
rowStart - 2,
getColChar(colStart + 9)]);
Range:= Sheet.Range[s]; //合并单元格
Range.Merge;
Range.Select;
//============================================
//在当前选定单元格插入图片
Sheet.Pictures.Insert(imgFile).Select;
//移动图片
eclApp.Selection.ShapeRange.IncrementLeft(180);
eclApp.Selection.ShapeRange.IncrementTop(-30); rct.Left:= Range.Left;
rct.Top:= Range.Top;
rct.Right:= rct.Left + Range.Width;
rct.Bottom:= rct.Top + Range.Height;
//在指定坐标添加线条
Sheet.Shapes.AddLine(rct.Left,rct.Top,rct.Right,rct.Top).Select;
eclApp.Selection.ShapeRange.IncrementTop(18);
eclApp.Selection.ShapeRange.Line.Weight:= 2.25;
//在指定坐标添加文本框 并选定
Sheet.Shapes.AddTextBox(1,rct.Right - 160,rct.Top,160,16).Select;
eclApp.Selection.Characters.Text:= 'Design By Mostone.Jiang';
//选中字符的起始位置与长度(长度省去则包含所有后面的字符)
eclApp.Selection.Characters(11,13).Font.FontStyle:= '加粗 倾斜';
eclApp.Selection.Characters(11,13).Font.Underline:= 1;
eclApp.Selection.Characters(11,13).Font.colorIndex:= 46;
eclApp.Selection.Font.Name:= 'Tahoma';
eclApp.Selection.ShapeRange.Fill.Visible := 0;
eclApp.Selection.ShapeRange.Line.Visible := 0;
end;//程序启动时,创建一张图片,用来插入到生成的Excel表格中
procedure TForm1.FormCreate(Sender: TObject);
var
d: Word;
buf: array[0..255] of char;
begin
d:= GetTempPath(sizeof(buf),buf);
if d > 0 then
begin
imgFile:= buf + 'imgTL.jpg';
drawImg; //调用自定义过程,绘制图形并保存到临时目录
//也可以考虑使用剪贴板
end
else messagebox(handle,'无法得到临时目录!\n你可以继续运行程序\n但是生成的Excel表格将没有图例','取临时文件出错',MB_OK or MB_ICONINFORMATION);
end;//随便画张图
procedure TForm1.drawImg;
var
bitMap: TBitMap;
w,h: Integer;
begin
W:= 268;
H:= 50;
bitMap:= TBitMap.Create;
try
bitMap.Width:= W;
bitMap.Height:= H;
with bitMap.Canvas do
begin
Font.Size:= 36;
Font.Name:= '隶书'; Brush.Style:= bsClear; TextOut(5,9, '九九乘法表');
TextOut(5,5, '○○');
end;
bitMap.SaveToFile(imgFile);
finally
bitMap.Free;
end;
end;//返回列的字符表示符
function TForm1.getColChar(col: Integer): string;
const
Ai = 65; //'A'的ASCII码值
begin
if col > 256 then
begin
//允许的最大列数
raise ERangeError.CreateFmt('列(%d)超出允许的最大列数[256]', [col]);
exit
end;
if col <= 26 then Result:= chr(Ai + col -1) else
Result:= chr(Ai + col div 26 - 1) + chr(Ai + (col - 1) mod 26);
end;end. =========================================
//关闭程序时,释放Excel应用对象
procedure TForm1.FormDestroy(Sender: TObject);
begin
//关闭eclApp
//若不进行这一步,在任务管理器中会驻留Excel进程
//但可以不必关闭Excel (eclApp.Quit),这样,操作员可以继续编辑或打印表格
eclApp:= Unassigned;
end;
//合并单元格
Range.Merge;
Range.Select;
数据导入导出procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
TxtConnection.Close;
DBFConnection.Close;
AccessConnection.Close;
ExcelConnection.Close;
end;procedure TForm1.AccessConnectionExecuteComplete(Connection: TADOConnection;
RecordsAffected: Integer; const Error: Error;
var EventStatus: TEventStatus; const Command: _Command;
const Recordset: _Recordset);
begin
//记时1
iT2 := GetTickCount;
StatusBar.Panels[1].Text := FormatFloat('#,##', iT2 - iT1) + '毫秒';
StatusBar.Panels[2].Text := '共导记录:' + IntToStr(RecordsAffected) + '条';
end;procedure TForm1.AccessConnectionWillExecute(Connection: TADOConnection;
var CommandText: WideString; var CursorType: TCursorType;
var LockType: TADOLockType; var CommandType: TCommandType;
var ExecuteOptions: TExecuteOptions; var EventStatus: TEventStatus;
const Command: _Command; const Recordset: _Recordset);
begin
iT1 := GetTickCount;
end;//=================================================================
//Access
//Access->TXT
procedure TForm1.Button1Click(Sender: TObject);
begin
sSql := 'select * into [Text;Database=f:\].aaa.txt from demo';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Access->DBF
procedure TForm1.Button3Click(Sender: TObject);
begin
sSql := 'select * into aaa in ''f:\'' ''dbase 5.0;'' from demo';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Access->Excel
//注意:前一个aaa为Excel文件中的aaa页,后一个aaa为Access文件demo.mdb中的一个表名
procedure TForm1.Button9Click(Sender: TObject);
begin
sSql :=
'select * into aaa from aaa in ''E:\Delphilx\ADOSQL\PH\demo.mdb''';
with ExcelConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Access->Access
procedure TForm1.Button4Click(Sender: TObject);
begin
sSql := 'select * into aaa from demo in ''E:\Delphilx\ADOSQL\PH\demo.mdb''';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//TXT->Access
procedure TForm1.Button2Click(Sender: TObject);
begin
sSql := 'select * into uform from [Text;Database=f:\].form.txt';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//DBF->Access
procedure TForm1.Button5Click(Sender: TObject);
begin
sSql := 'select * into aaa from aaa in ''f:\'' ''dbase 5.0;''';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Excel->Access
//[aaa$]->Excel文件aaa.xls中的aaa页
procedure TForm1.Button10Click(Sender: TObject);
begin
sSql :=
'select * into bbb from [excel 8.0;database=f:\aaa.xls].[aaa$]';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//=================================================================
//Oracle
//Oracle->Access
procedure TForm1.Button6Click(Sender: TObject);
begin
sSql :=
'select * into ouform from ouform in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bklskf;PWD=bklskf;SERVER=bkls;]';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Oracle->TXT
procedure TForm1.Button7Click(Sender: TObject);
begin
sSql := 'select VGH,VXM,VMM into lsygb.txt from (select * from lsygb in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=BKLS;PWD=BKLS;SERVER=BKLS;])';
// sSql :=
// 'select * into form.txt from ouform in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bklskf;PWD=bklskf;SERVER=bkls;]';
with TxtConnection do
begin
Close;
Connected := True;
Execute(sSql);
end;
end;//Oracle->Excel
procedure TForm1.Button8Click(Sender: TObject);
begin
sSql :=
'select * into aaa from lsygb in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;]';
with ExcelConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Oracle->DBF
procedure TForm1.Button11Click(Sender: TObject);
begin
sSql :=
'select * into aaa from lsygb in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;]';
with DBFConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//TXT->Oracle
//Oracle数据库注意要将表名大写!不然创建出来的表可以看到表名但无法打开
procedure TForm1.Button12Click(Sender: TObject);
begin
sSql := 'select * into PH_TXT in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;] from ph_txt.txt';
with TxtConnection do
begin
Close;
Connected := True;
Execute(sSql);
end;
end;//DBF->Oracle
procedure TForm1.Button13Click(Sender: TObject);
begin
sSql :=
'select * into PH_DBF in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;] from aaa';
with DBFConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Access->Oracle
procedure TForm1.Button14Click(Sender: TObject);
begin
sSql :=
'select * into PH_ACCESS in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;] from aaa';
with AccessConnection do
begin
Connected := True;
Execute(sSql);
end;
end;//Excel->Oracle
procedure TForm1.Button15Click(Sender: TObject);
begin
// 'select * into MLB in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=BKLS;PWD=BKLS;Server=BKLS;] from MLB'
//Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\aaa.xls;Extended Properties=Excel 8.0;Persist Security Info=False
sSql :=
'select * into PH_EXCEL in [ODBC][ODBC;Driver={Microsoft ODBC for Oracle};UID=bkls;PWD=bkls;SERVER=bkls;] from aaa';
with ExcelConnection do
begin
Connected := True;
Execute(sSql);
end;
end;procedure TForm1.Button16Click(Sender: TObject);
begin
sSql := 'select * into qxb in ''f:\'' ''dbase 5.0;'' from lsqxb.txt';
with TXTConnection do
begin
// Close;
// ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=f:\temp;Extended Properties="text;HDR=YES;FMT=Delimited";Persist Security Info=True';
Connected := True;
Execute(sSql);
end;
end;end.