乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > delphi打开excel再关闭,桌面会有excel快速闪过...-delphi连接excel,delphi excel

delphi打开excel再关闭,桌面会有excel快速闪过...-delphi连接excel,delphi excel

作者:乔山办公网日期:

返回目录:excel表格制作


xls文件不能直接打开,原因:

        1、打开方式不正确;

        2、office软件出错。


解决办法:

        1、更改打开方式,操作步骤如下

                ❶右键点击xls文件,选择【打开方式】;


                ❷点击【选择计算机上的程序】


                ❸选择excel打开方式,勾选【始终使用选择的程序打开这种文件】,点击【确定】;


                ❹再次使用鼠标左键双击xls文件,即可正常打开。


        2、重新安装office办公软件,并重新打开xls文件。



关闭之前,设置ole对象的某个属性(不记得是Visible还是AppVisible了)为False。
拿Excel 2010做例子:
首先,需要在最上面的uses 里面 加上comObj

//AXLSFile 表示文件路径
procedure Xls_To_StringGrid(AXLSFile: string);
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
vLen : integer;
begin
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Open the Workbook
XLApp.Workbooks.Open(AXLSFile);

Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];

// In order to know the dimension of the WorkSheet, i.e the number of rows
// and the number of columns, we activate the last non-empty cell of it

Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
// Get the value of the last row
x := XLApp.ActiveCell.Row;
// Get the value of the last column
y := XLApp.ActiveCell.Column;

// Assign the Variant associated with the WorkSheet to the Delphi Variant

RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
// Define the loop for filling in the TMOStringGrid

//假设是导入到表格中, 2开头表636f70797a686964616f362示忽略第一行表头
for k := 2 to x do
begin
eGrid.Cells[1, k - 1] := RangeMatrix[k, 1]; //Excel中第一行第一列都是从1 开始
end;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
// XLApp.DisplayAlerts := False;
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
end;
end;
end;

use
    ...,Excel2000, OleServer, ExcelXP;
type
  TForm1 = class(TForm)
    ExcelApplication1: TExcelApplication;
    ExcelWorkbook1: TExcelWorkbook;
    ExcelWorksheet1: TExcelWorksheet;
    ...
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorkbook1    := TExcelWorkbook.Create(Application);
ExcelWorksheet1   := TExcelWorksheet.Create(Application);
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Open(CurInfo.CurFile,null,null,null,null,null,null,null,null,null,null,null,null,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
    ...
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
ExcelApplication1.Workbooks.Close(0);
ExcelApplication1.Quit;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
ExcelApplication1.Free;

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网