给你一个参考procedure TForm1.Button1Click(Sender: TObject); var // ExcelApplication1: TExcelApplication; // ExcelWorkbook1: TExcelWorkbook; // ExcelWorkSheet1: TExcelWorksheet; I,j : integer; Str : String; workbook: OleVariant; xlscolcount,RowsCounts : integer; begin //Myexcel.xls { try ExcelApplication1:=TExcelApplication.Create(self); ExcelWorkbook1 := TExcelWorkbook.Create(Self); ExcelWorkSheet1 := TExcelWorksheet.Create(Self); except Application.MessageBox('打开Excel异常!','提示',mb_ok+mb_iconinformation); exit; end; } ExcelApplication1.Connect; workbook:=ExcelApplication1.Workbooks.Add('D:/Myexcel.xls',0); Combobox1.Items.Clear; // Showmessage(InttoStr( ExcelApplication1.Sheets.Count)); // workbook:=ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]); // ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _WorkSheet);{ For I:=0 to ExcelApplication1.Workbooks.Count-1 do begin Str:=[i].values; Combobox1.Items.Add(ExcelWorkBook1.Worksheets[i].); end;} // Combobox1.Items.Add(ExcelApplication1.Worksheets[i].Name); for i := 1 to workbook.worksheets.count do ComboBox1.Items.Add(WorkBook.WorkSheets[i].Name); workbook.WorkSheets[3].Activate; RowsCounts:=workbook.activesheet.UsedRange.Rows.Count;//行Fexcel.Application.Worksheets[SheetName].UsedRange.Rows.Count xlscolcount:=workbook.activesheet.UsedRange.Columns.count;//列 StringGrid1.RowCount:=RowsCounts+1; StringGrid1.ColCount:=xlscolcount+1; For I:=0 to RowsCounts do begin StringGrid1.Cells[0,i]:=InttoStr(i); // StringGrid1.RowHeights:=workbook.activesheet.Row.Width; end; For J:=1 to xlscolcount do begin StringGrid1.Cells[j,0]:=Chr(j+Ord('A')-1); end;
For i:=1 to RowsCounts do For j:=1 to xlscolcount do begin StringGrid1.Cells[j,i]:=workbook.activesheet.cells[i,j]; end; ExcelApplication1.Quit; ExcelApplication1.Disconnect; end;
我估计可能其他机器上的Office版本不一样?你可以把你的控制Excel的程序贴一下看看。
这是在EXCEL里显示的代码:
procedure TForm1.SetDataSetToExcelApp(strConnectionString:string;strSQL:string;isShowHeader:boolean=true);
var
xlApp,xlBook,xlSheet,xlQuery: Variant;
adoConnection,adoRecordset: Variant;
begin
adoConnection := CreateOleObject('ADODB.Connection');
adoRecordset := CreateOleObject('ADODB.Recordset');
adoConnection.Open(strConnectionString);
adoRecordset.CursorLocation := 3;//adUseClient;
adoRecordset.Open(strSQL,adoConnection,1,3);
xlApp := CreateOleObject('Excel.Application');
xlBook := xlApp.Workbooks.Add;
xlSheet := xlBook.Worksheets['sheet1'];
xlApp.Visible := True;
{把数据集导入EXCEL数据} xlQuery := xlSheet.QueryTables.Add(adoRecordset,xlSheet.Range['A1']);
{关键是以上这一句} xlQuery.FieldNames := isShowHeader;
xlQuery.RowNumbers := False;
xlQuery.FillAdjacentFormulas := False;
xlQuery.PreserveFormatting := True;
xlQuery.RefreshOnFileOpen := False;
xlQuery.BackgroundQuery := True;
//xlQuery.RefreshStyle := xlInsertDeleteCells;
xlQuery.SavePassword := True;
xlQuery.SaveData := True;
xlQuery.AdjustColumnWidth := True;
xlQuery.RefreshPeriod := 0;
xlQuery.PreserveColumnInfo := True;
xlQuery.FieldNames := True;
xlQuery.Refresh; xlApp := Unassigned;
xlBook := Unassigned;
xlSheet := Unassigned;
xlQuery := Unassigned;end;
感觉不至于,检查下数据库连接是不是有问题,
把table累的组件重新设置为true;重新编译
TO:DDGG(叮叮当当) 加我QQ,我发给你,帮我看一下好吗?
var
// ExcelApplication1: TExcelApplication;
// ExcelWorkbook1: TExcelWorkbook;
// ExcelWorkSheet1: TExcelWorksheet;
I,j : integer;
Str : String;
workbook: OleVariant;
xlscolcount,RowsCounts : integer;
begin //Myexcel.xls
{ try
ExcelApplication1:=TExcelApplication.Create(self);
ExcelWorkbook1 := TExcelWorkbook.Create(Self);
ExcelWorkSheet1 := TExcelWorksheet.Create(Self);
except
Application.MessageBox('打开Excel异常!','提示',mb_ok+mb_iconinformation);
exit;
end; }
ExcelApplication1.Connect;
workbook:=ExcelApplication1.Workbooks.Add('D:/Myexcel.xls',0); Combobox1.Items.Clear;
// Showmessage(InttoStr( ExcelApplication1.Sheets.Count));
// workbook:=ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
// ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _WorkSheet);{ For I:=0 to ExcelApplication1.Workbooks.Count-1 do
begin
Str:=[i].values;
Combobox1.Items.Add(ExcelWorkBook1.Worksheets[i].);
end;}
// Combobox1.Items.Add(ExcelApplication1.Worksheets[i].Name);
for i := 1 to workbook.worksheets.count do
ComboBox1.Items.Add(WorkBook.WorkSheets[i].Name); workbook.WorkSheets[3].Activate;
RowsCounts:=workbook.activesheet.UsedRange.Rows.Count;//行Fexcel.Application.Worksheets[SheetName].UsedRange.Rows.Count
xlscolcount:=workbook.activesheet.UsedRange.Columns.count;//列 StringGrid1.RowCount:=RowsCounts+1;
StringGrid1.ColCount:=xlscolcount+1;
For I:=0 to RowsCounts do
begin
StringGrid1.Cells[0,i]:=InttoStr(i);
// StringGrid1.RowHeights:=workbook.activesheet.Row.Width;
end; For J:=1 to xlscolcount do
begin
StringGrid1.Cells[j,0]:=Chr(j+Ord('A')-1);
end;
For i:=1 to RowsCounts do
For j:=1 to xlscolcount do
begin
StringGrid1.Cells[j,i]:=workbook.activesheet.cells[i,j];
end;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
end;
该成true了,再编译应该可以了的