M
mkbss
Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位大侠:
以下是我自己写的EXCEL表导出代码,如果在26列内是没有问题的,但超过26列就会出错
procedure TForm1.Button3Click(Sender: TObject);
var emptyleVariant;
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;
能否帮我解决呢
以下是我自己写的EXCEL表导出代码,如果在26列内是没有问题的,但超过26列就会出错
procedure TForm1.Button3Click(Sender: TObject);
var emptyleVariant;
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;
能否帮我解决呢