用delphi开发的程序,由于查询出来的数据太大,现在想把数据导出到execl表格中。能不能分几个工作表导出。
2个回答
展开全部
如果是想创建多个工作表的话,当然可以实现。
在导出的时候创建多个Sheet,然后根据条件判断每个sheet页中导入多少条数据。
导入数据至Excel的代码,我的空间中有三种导出例子,其中使用普通循环导入和Ole方式导入的话可方便控制sheet页。
在导出的时候创建多个Sheet,然后根据条件判断每个sheet页中导入多少条数据。
导入数据至Excel的代码,我的空间中有三种导出例子,其中使用普通循环导入和Ole方式导入的话可方便控制sheet页。
参考资料: http://hi.baidu.com/yueyun889/blog/item/f60d8423310f614fac34dedd.html
展开全部
你是用什么方式导出的,记录大概有多少,用全选后粘贴的方式 会快很多
---
给你代码参考一下
ar
asheet,range:variant;
i,K,M,N,y :integer;
ls_FileName:string;
tsList :TStringList;
s :string;
begin
if SaveDialog1.Execute then begin
Screen.Cursor:=crHourGlass;
ls_FileName:=SaveDialog1.FileName;
ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Workbooks.Add(xlWBATWorksheet,0);
asheet:=ExcelApplication1.Worksheets.Item[1];
i:=DBGrid1.DataSource.DataSet.FieldCount;
range:=asheet.range[asheet.cells[1,1],asheet.cells[1,i]];
range.merge;
range.HorizontalAlignment:=xlCenter;
range.Font.Size:=14;
frmDataModule.TsqlSum.Locate('Flags',1,[]);
range.value:=Trim(frmDataModule.TsqlSum.FieldByName('sqlName').Value);
for i:=0 to DBGrid1.Columns.Count-1 do
asheet.cells[2,i+1].value:=Trim(DBGrid1.Columns.Items[i].Title.Caption);
K:=1;
N:=DBGrid1.Columns.count;
I:=DBGrid1.DataSource.DataSet.RecordCount;
tsList:=TStringList.Create;
try
DBGrid1.DataSource.DataSet.first;
while not DBGrid1.DataSource.DataSet.Eof do
begin
s:='';
for y:=0 to n-1 do
begin
s:=s+Trim(DBGrid1.DataSource.DataSet.Fields[y].AsString)+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
DBGrid1.DataSource.DataSet.next;
end;
Clipboard.AsText:=tsList.Text;
finally
tsList.Free;
end;
ExcelApplication1.Disconnect;
asheet.cells[3,1].select;
aSheet.Paste;
range:=asheet.range[asheet.cells[2,1],asheet.cells[DBGrid1.DataSource.DataSet.RecordCount+2,DBGrid1.DataSource.DataSet.FieldCount]];
range.select;
range.borders.linestyle:=1;
for i:=1 to DBGrid1.DataSource.DataSet.FieldCount do begin
range:=asheet.range[asheet.cells[1,i],asheet.cells[DBGrid1.DataSource.DataSet.RecordCount,i]];
range.EntireColumn.AutoFit;
end;
aSheet.Saveas(ls_FileName);
MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK);
Screen.Cursor:=crDefault;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
aSheet:=Unassigned; //释放VARIANT变量
DBGrid1.DataSource.DataSet.First;
end;
---
给你代码参考一下
ar
asheet,range:variant;
i,K,M,N,y :integer;
ls_FileName:string;
tsList :TStringList;
s :string;
begin
if SaveDialog1.Execute then begin
Screen.Cursor:=crHourGlass;
ls_FileName:=SaveDialog1.FileName;
ExcelApplication1.Visible[0]:=False;
ExcelApplication1.Workbooks.Add(xlWBATWorksheet,0);
asheet:=ExcelApplication1.Worksheets.Item[1];
i:=DBGrid1.DataSource.DataSet.FieldCount;
range:=asheet.range[asheet.cells[1,1],asheet.cells[1,i]];
range.merge;
range.HorizontalAlignment:=xlCenter;
range.Font.Size:=14;
frmDataModule.TsqlSum.Locate('Flags',1,[]);
range.value:=Trim(frmDataModule.TsqlSum.FieldByName('sqlName').Value);
for i:=0 to DBGrid1.Columns.Count-1 do
asheet.cells[2,i+1].value:=Trim(DBGrid1.Columns.Items[i].Title.Caption);
K:=1;
N:=DBGrid1.Columns.count;
I:=DBGrid1.DataSource.DataSet.RecordCount;
tsList:=TStringList.Create;
try
DBGrid1.DataSource.DataSet.first;
while not DBGrid1.DataSource.DataSet.Eof do
begin
s:='';
for y:=0 to n-1 do
begin
s:=s+Trim(DBGrid1.DataSource.DataSet.Fields[y].AsString)+#9;
Application.ProcessMessages;
end;
tsList.Add(s);
DBGrid1.DataSource.DataSet.next;
end;
Clipboard.AsText:=tsList.Text;
finally
tsList.Free;
end;
ExcelApplication1.Disconnect;
asheet.cells[3,1].select;
aSheet.Paste;
range:=asheet.range[asheet.cells[2,1],asheet.cells[DBGrid1.DataSource.DataSet.RecordCount+2,DBGrid1.DataSource.DataSet.FieldCount]];
range.select;
range.borders.linestyle:=1;
for i:=1 to DBGrid1.DataSource.DataSet.FieldCount do begin
range:=asheet.range[asheet.cells[1,i],asheet.cells[DBGrid1.DataSource.DataSet.RecordCount,i]];
range.EntireColumn.AutoFit;
end;
aSheet.Saveas(ls_FileName);
MessageBox(Application.Handle,'数据导出完毕!','系统提示',MB_ICONINFORMATION or MB_OK);
Screen.Cursor:=crDefault;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
aSheet:=Unassigned; //释放VARIANT变量
DBGrid1.DataSource.DataSet.First;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询