代码如下,但是在运行数次后会出现只看得到EXCEL的标题和上面一排MENU的,具体的表格内容看不见的情况,麻烦大家指点
ExcelApplication1:=tExcelApplication.create(dbgrid1);
ExcelWorkbook1:=tExcelWorkbook.create(dbgrid1);
ExcelWorksheet1:=tExcelWorksheet.create(dbgrid1);
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Workbooks.Add(extractfilepath(application.exename)+'text\book1.xlt',0);
.....
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.free;
ExcelWorkbook1.free;
ExcelWorksheet1.free;
end;

解决方案 »

  1. ExcelApplication1.Open;
    ExcelWorkbook1.Open;
      

  2. ExcelApplication1:=tExcelApplication.create(dbgrid1);
    ExcelWorkbook1:=tExcelWorkbook.create(dbgrid1);
    ExcelWorksheet1:=tExcelWorksheet.create(dbgrid1);
    ExcelApplication1.Connect;
    ExcelApplication1.Visible[0]:=True;
    ExcelApplication1.Workbooks.Add('d:\a.xls',0);
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    ExcelApplication1.free;
    ExcelWorkbook1.free;
    ExcelWorksheet1.free;
    没发现什么不妥呀!firetoucher(风焱) ExcelApplication1.Open;
    ExcelWorkbook1.Open;这两个SERVER控件没有Open属性呀,你使用的是D7吗
      

  3. 用下面的方法试一试:步聚 创建一个普通Application。 在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。 连接Excel 97,具体方法如下: 
    打开Excel97。 
    Try 
    ExcelApplication1.Connect; 
    Except 
    End; 
    ExcelApplication1.Visible[0]:=True; 
    增加一个Workbook。 
    ExcelWorkbook1.ConnectTo(ExcelApplication1. 
    Workbooks.Add(EmptyParam,0)); 添加一个Worksheet。 
    var 
    Temp_Worksheet: _WorkSheet; 
    begin 
    Try 
    Temp_Worksheet:=ExcelWorkbook1. 
    WorkSheets.Add(EmptyParam, 
    EmptyParam,EmptyParam,EmptyParam,0) 
    as _WorkSheet;//(注意) 
    ExcelWorkSheet1.ConnectTo(Temp_WorkSheet); 
    Except 
    ShowMessage('Failure'); 
    End; 
    end; 
    关闭Excel. 
    Try 
    ExcelApplication1.Quit; 
    ExcelWorksheet1.Disconnect; 
    ExcelWorkbook1.Disconnect; 
    ExcelApplication1.Disconnect; 
    Except 
    End; ---- 4. 对Excel的一些操作: 
    选择当前Workbook的某一Worksheet. 
    procedure TForm1.ComboBox1DropDown 
    (Sender: TObject); 
    var 
    i: Integer; 
    begin 
    ComboBox1.Clear; 
    For i:=1 to ExcelWorkbook1. 
    Worksheets.Count do 
    ComboBox1.Items.Add 
    ((ExcelWorkbook1.Worksheets.Item[i] 
    as _WorkSheet).Name); 
    end; procedure TForm1.ComboBox1Change 
    (Sender: TObject); 
    begin 
    ExcelWorkSheet1.ConnectTo 
    (ExcelWorkbook1.Worksheets.Item 
    [ComboBox1.ItemIndex+1] as _WorkSheet); 
    ExcelWorkSheet1.Activate; 
    end; 选择某一Workbook: 
    procedure TForm1.ComboBox2DropDown 
    (Sender: TObject); 
    var 
    i: Integer; 
    begin 
    ComboBox2.Clear; 
    if ExcelApplication1.Workbooks.Count > 0 then 
    For i:=1 to ExcelApplication1.Workbooks.Count do 
    Combobox2.Items.Add(ExcelApplication1. 
    Workbooks.Item[i].Name); 
    end; procedure TForm1.ComboBox2Change(Sender: TObject); 
    begin 
    ExcelWorkSheet1.Disconnect; 
    ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks. 
    Item[Combobox2.ItemIndex+1]); 
    ExcelWorkBook1.Activate; 
    ExcelWorksheet1.ConnectTo(ExcelWorkBook1. 
    ActiveSheet as _WorkSheet); 
    ExcelWorkSheet1.Activate; 
    end; 对某一单元格进行赋值及取值。 
    procedure TForm1.Button5Click(Sender: TObject); 
    begin 
    ExcelWorksheet1.Cells.Item[SpinEdit2.Value, 
    SpinEdit1.Value]:=Edit1.Text; 
    end; procedure TForm1.Button6Click(Sender: TObject); 
    begin 
    Edit1.Text:=ExcelWorksheet1.Cells.Item[ 
    SpinEdit2.Value,SpinEdit1.Value]; 
    end; 选择某一区域 
    ExcelWorkSheet1.Range['A1','C1'].Select; 打开一个Excel文件。 
    if OpenDialog1.Execute then 
    Begin 
    Try 
    ExcelWorkBook1.ConnectTo 
    (ExcelApplication1.Workbooks.Open 
    (OpenDialog1.FileName, 
    EmptyParam,EmptyParam,EmptyParam, 
    EmptyParam,EmptyParam, 
    EmptyParam,EmptyParam,EmptyParam, 
    EmptyParam,EmptyParam, 
    EmptyParam,EmptyParam,0)); 
    ExcelWorkSheet1.ConnectTo 
    (ExcelWorkBook1.Activesheet 
    as _Worksheet); 
    Except; 
    End; 
    End; 
      

  4. 还是有这个现象
    一般情况下是开始调用EXCEL的头几次很正常,但是多调用了几次问题就出现了
    好象和内存没有完全释放有关?
      

类似问题 »