如何将sql server数据库中的数据取出来放到EXCEL表,是否可以直接在excel中完成 急求,在EXCEL中如何实现? 10
2个回答
展开全部
RITEEXCEL是需要你再PRIVATE里定义的一个函数,BDE或者ADO都无所谓,你只要把你的表对应好就可以了。
procedure Tqueryform.Button5Click(Sender: TObject);
var
sname:string; //这里是用来取你界面上输入的EXCEL表名
begin
sname:=edit1.Text;
if sname<>'' then //用户输入了表名
// title:='所需数据';
writeexcel(sname)
else
writeexcel('查询结果'); //否则存为查询结果.exl
end;
procedure Tqueryform.writeexcel(sname:string);
var
j,row: integer;
filename: string;
begin
filename := concat((extractfilepath(application.exename)+'excel\'), sName, '.xls');
//保存在程序所在文件夹下的一个叫做excel的文件夹下面
//初始化
try
excelapplication1:=Texcelapplication.Create(application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR + mb_Ok);
Abort;
end;
//数据的导出
try
begin
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
//给表格的每个单元附值
excelworksheet1.Cells.Item[1,1]:='工号'; //这个根据你的数据表对应的变量名
excelworksheet1.Cells.Item[1,2]:='日期';
excelworksheet1.Cells.Item[1,3]:='表型';
excelworksheet1.Cells.Item[1,4]:='地址';
excelworksheet1.Cells.Item[1,5]:='原因';
row:=2;
maindm.ADOQuery2.First;
while not maindm.ADOQuery2.Eof do
begin
for j:=1 to 5 do
begin
excelworksheet1.Cells.Item[row,j]:=maindm.ADOQuery2.Fields[j-1].AsString;
end; //end ...for
maindm.ADOQuery2.Next;
row:=row+1; //得到查询后的行数
end;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), 'Hello',mb_Ok);
end //end try...finally
//结束释放空间
finally
ExcelApplication1.Disconnect; //断开连接
ExcelApplication1.Quit; //退出EXCEL
ExcelApplication1.Free;//释放空间
ExcelWorksheet1.Free; //释放空间
ExcelWorkbook1.Free; //释放空间
end;
end;
procedure Tqueryform.Button5Click(Sender: TObject);
var
sname:string; //这里是用来取你界面上输入的EXCEL表名
begin
sname:=edit1.Text;
if sname<>'' then //用户输入了表名
// title:='所需数据';
writeexcel(sname)
else
writeexcel('查询结果'); //否则存为查询结果.exl
end;
procedure Tqueryform.writeexcel(sname:string);
var
j,row: integer;
filename: string;
begin
filename := concat((extractfilepath(application.exename)+'excel\'), sName, '.xls');
//保存在程序所在文件夹下的一个叫做excel的文件夹下面
//初始化
try
excelapplication1:=Texcelapplication.Create(application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR + mb_Ok);
Abort;
end;
//数据的导出
try
begin
ExcelApplication1.Workbooks.Add(EmptyParam, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
//给表格的每个单元附值
excelworksheet1.Cells.Item[1,1]:='工号'; //这个根据你的数据表对应的变量名
excelworksheet1.Cells.Item[1,2]:='日期';
excelworksheet1.Cells.Item[1,3]:='表型';
excelworksheet1.Cells.Item[1,4]:='地址';
excelworksheet1.Cells.Item[1,5]:='原因';
row:=2;
maindm.ADOQuery2.First;
while not maindm.ADOQuery2.Eof do
begin
for j:=1 to 5 do
begin
excelworksheet1.Cells.Item[row,j]:=maindm.ADOQuery2.Fields[j-1].AsString;
end; //end ...for
maindm.ADOQuery2.Next;
row:=row+1; //得到查询后的行数
end;
ExcelWorksheet1.SaveAs(filename);
Application.Messagebox(pchar('数据成功导出' + filename), 'Hello',mb_Ok);
end //end try...finally
//结束释放空间
finally
ExcelApplication1.Disconnect; //断开连接
ExcelApplication1.Quit; //退出EXCEL
ExcelApplication1.Free;//释放空间
ExcelWorksheet1.Free; //释放空间
ExcelWorkbook1.Free; //释放空间
end;
end;
展开全部
可以 vba
Public Function openCn()
'----------------------------
' 打开数据库
Set CN = New Connection
constr = "Provider=SQLOLEDB.1;Password=*************;Persist Security Info=True;User ID=sa;Initial Catalog=" & ConnectName & ";Data Source=******"
CN.Open constr
If CN.State <> 1 Then
openCn = False
MsgBox "打开数据库失败!"
Set CN = Nothing
Exit Function
End If
'----------------------------
End Function
Public Function openCn()
'----------------------------
' 打开数据库
Set CN = New Connection
constr = "Provider=SQLOLEDB.1;Password=*************;Persist Security Info=True;User ID=sa;Initial Catalog=" & ConnectName & ";Data Source=******"
CN.Open constr
If CN.State <> 1 Then
openCn = False
MsgBox "打开数据库失败!"
Set CN = Nothing
Exit Function
End If
'----------------------------
End Function
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询