作者:乔山办公网日期:
返回目录:excel表格制作
当然支持,只是你写错了。应该是:
CreateOleObject('Excel.Application');
拿Excel 2010做例子:
首先,需要在最上面的e79fa5e98193e4b893e5b19e362uses 里面 加上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开头表示忽略第一行表头
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;
如果不支持,那就只能使用控件的办法,如zhidao果想使用excel2007,这个delphi7自带的控件是不支持的,解决办法有二:
1. 自己导入excel2007的动态库。
2. 使用第版三方控件,比如NativeExcel。
顺便提一句: 貌似COM方式应该是兼容性最好的吧,怎么不支持权呢?