请问:如果DBGRIDEH的列超过26,导出EXCEL表就会出错(20分)

  • 主题发起人 主题发起人 mkbss
  • 开始时间 开始时间
M

mkbss

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位大侠:
以下是我自己写的EXCEL表导出代码,如果在26列内是没有问题的,但超过26列就会出错
procedure TForm1.Button3Click(Sender: TObject);
var empty:oleVariant;
Col:char;
Row,j,I:integer;
R:String;
ExcelWorksheet1:TExcelWorksheet;
ExcelWorkbook1:TExcelWorkbook;
ExcelApplication1:TExcelApplication;
begin
inherited;
ExcelWorksheet1:=TExcelWorksheet.Create(self);
ExcelWorkbook1:=TExcelWorkbook.Create(self);
ExcelApplication1:=TExcelApplication.Create(self);
try
EXCELAPPLICATION1.Connect;
EXCEPT
MESSAGEDLG('Excel确认安装吗?',mtError,[mbOk],0);
Abort;
END;
EXCELAPPLICATION1.Visible[0]:=TRUE;

TRY
EMPTY:=EMPTYPARAM;
EXCELAPPLICATION1.Workbooks.Add(EMPTY,0);
EXCELWORKBOOK1.ConnectTo(EXCELAPPLICATION1.Workbooks.Item[EXCELAPPLICATION1.Workbooks.COUNT]);
EXCELWORKBOOK1.Activate;
ExcelWorksheet1.ConnectTo((EXCELWORKBOOK1.Worksheets[1]as _WorkSheet));
Col:='A';
ROW:=1;
FOR I:=0 TO DBGridEh1.FieldCount-1 DO
BEGIN
R:=STRING(Col)+INTTOSTR(ROW);
ExcelWorksheet1.Range[R,R].value2:=DBGridEh1.columns.title.caption;
Col:=chr(ord(col)+1);
END;

WITH adoquery1 DO
BEGIN
FIRST;
Col:='A';
ROW:=2;
WHILE not eof do
BEGIN
FOR j:=0 TO FIELDS.COUNT-1 DO
BEGIN
R:=STRING(Col)+INTTOSTR(ROW);
ExcelWorksheet1.Range[R,R].Value2:=Fields.Fields[j].Value;
// EXCELWORKSSHEET1.RANGE[R,R].VAULE2:=FIELDS.FIELDS[j].VALUE;
Col:=chr(ord(col)+1);
END;
Inc(Row);
Col:='A';
NEXT;
END;
END;
EXCEPT
EXCELWORKBOOK1.Close(xlDoNotSaveChanges);
EXCELAPPLICATION1.Disconnect;
END;
ExcelApplication1.Disconnect;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
ExcelApplication1.Free;
end;
能否帮我解决呢
 
DBGRIDEH自带有这功能呀
uses DBGridEhImpExp

SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,dbgrideh1,SaveDialog1.FileName+'.xls',true);
 
勇者兄:
能否提供具体的代码是怎样的呢?
谢谢
 
dbgrideh 自带强大的导入导出功能了 . 可以网上搜索下帮助
 
R:=STRING(Col)+INTTOSTR(ROW);
更改为:
R:=format('A%d',[Row]);
试一试!
 
To leehq兄:
将 R:=STRING(Col)+INTTOSTR(ROW);
更改为:
R:=format('A%d',[Row]);
后导出来的数据全部都覆盖在第一列A上了
 
uses DBGridEhImpExp

SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,dbgrideh1,FileName+'.xls',true);
这两句就是呀,
 
http://www5.skycn.com/soft/29751.html
这项技术可较好地解决你的问题
QQ:292044357
 
TO 勇者:
uses DBGridEhImpExp

SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,dbgrideh1,FileName+'.xls',true);

运行后提示[Error] Unit1.pas(763): Undeclared identifier: 'FileName';
是什么问题呀
 
FileName是定义的输出文件名啊
 
//uses Grids, ComObj;
procedure CopyDbgridToExcel(Target: TDBGridEh; Title: string);
var
i, j: integer;
XLApp: Variant;
Sheet: Variant;
begin

Screen.Cursor := crHourGlass;

if not VarIsEmpty(XLApp) then
begin

XLApp.DisplayAlerts := False;

XLApp.Quit;

VarClear(XLApp);

end;

//通过ole创建Excel对象
try

XLApp := CreateOleObject('Excel.Application');

except

MessageDlg('创建Excel对象失败,请检查你的系统是否正确安装了Excel软件!', mtError, [mbOk], 0);

Screen.Cursor := crDefault;

Exit;

end;

XLApp.WorkBooks.Add[XLWBatWorksheet];

XLApp.WorkBooks[1].WorkSheets[1].Name := Title;

Sheet := XLApp.Workbooks[1].WorkSheets[Title];


for i := 0 to Target.ColCount-1 do

Sheet.cells[1, i+1] := Target.cells[i, 0];


for i := 1 to Target.RowCount-1 do

for j := 0 to Target.ColCount-1 do

Sheet.Cells[i+1, j+1] := Target.Cells[j, i];


XlApp.Visible := True;

Screen.Cursor := crDefault;

end;
 
后退
顶部