数据的导出 ( 积分: 10 )

  • 主题发起人 主题发起人 fuxin198311
  • 开始时间 开始时间
F

fuxin198311

Unregistered / Unconfirmed
GUEST, unregistred user!
把dbgrideh的数据导出excel 为什么我导出字段显示的是英文呢 而不是dbgrideh中的中文字段 怎样让导出的字段显示的是中文的 还有汇总的数据导不出 代码是这样的
procedure Tyaopinzd.SpeedButton6Click(Sender: TObject);
var i, row, column:integer;
begin
try
ExcelApplication1.Connect;
except
MessageDlg('Excel may not be installed', mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0] := true;
ExcelApplication1.Caption := 'Excel';
ExcelApplication1.Workbooks.Add(Null, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
row:=1;
try
for i:=0 to adoquery1.FieldCount-1 do
begin
ExcelWorksheet1.Cells.Item[1,i+1]:=adoquery1.Fields.DisplayName;
end;
row:=row+1;
adoquery1.First;
while not adoquery1.Eof do
begin
column:=1;
for i:=1 to adoquery1.FieldCount do
begin
ExcelWorksheet1.Cells.Item[row,column]:=adoquery1.Fields[i-1].AsString;
column:=column+1;
end;
adoquery1.Next;
row:=row+1;
end;
application.MessageBox('导出成功','提示',64);
except
application.MessageBox('发生错误,'提示',mb_iconstop);
Abort;
end;
ExcelApplication1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelWorksheet1.Disconnect;
end;
 
adoquery1.Fields.DisplayName的值是从adoquery1取来的,所以在adoquery1时就要把英文换成中文,select 字段1 as 中文1,字段2 as 中文2,....from table
 
直接导出dbgrideh的字段标签名就行了
 
你这是用ole方式导出,dbgrideh可直接导出到excell,并不用安装excell。
 
lyq2276959
那怎么做呀 没做过
 
var
ExpClass:TDBGridEhExportClass;//保存文件的地址
Ext:String;//用于判断保存文件的类型
begin
//打印
//PrtDefend.Preview;
SaveDlgDefend.FileName := 'file1';//文件名称
if SaveDlgDefend.Execute then
begin
case SaveDlgDefend.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(SaveDlgDefend.FileName,Length(SaveDlgDefend.FileName)-2,3)) <> UpperCase(Ext) then
SaveDlgDefend.FileName := SaveDlgDefend.FileName + '.' + Ext;
showmessage('导出数据成功!');
SaveDBGridEhToExportFile(ExpClass,GrdBabyNum,SaveDlgDefend.FileName,True);//导出数据
//其中false为局部数据
end;
end;
 
procedure Tyaopinzd.SpeedButton6Click(Sender: TObject); //导出
var ExpClass:TDBGridEhExportClass;
Ext:String;
begin
SaveDialog1.FileName := 'file1';
if (ActiveControl is TDBGridEh) then
if SaveDialog1.Execute then
begin
case SaveDialog1.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <>
UpperCase(Ext) then
SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;
SaveDBGridEhToExportFile(ExpClass,TDBGridEh(ActiveControl),
SaveDialog1.FileName,False);
end;
end;
end;
可是打开的保存对话框中 保存类型一片空白
 
保存类型空白,是因为你的SaveDialog1的属性没有设置。其中的filter属性写*.xls,*.xls.就OK了
 
后退
顶部