delphi7 打开excel

delphi7 打开excel,第1张

拿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('ExcelApplication');
try
// Hide Excel
XLAppVisible := False;
// Open the Workbook
XLAppWorkbooksOpen(AXLSFile);
Sheet := XLAppWorkbooks[ExtractFileName(AXLSFile)]WorkSheets[1];
// In order to know the dimension of the WorkSheet, ie the number of rows
// and the number of columns, we activate the last non-empty cell of it
SheetCellsSpecialCells(xlCellTypeLastCell, EmptyParam)Activate;
// Get the value of the last row
x := XLAppActiveCellRow;
// Get the value of the last column
y := XLAppActiveCellColumn;
// Assign the Variant associated with the WorkSheet to the Delphi Variant
RangeMatrix := XLAppRange['A1', XLAppCellsItem[X, Y]]Value;
// Define the loop for filling in the TMOStringGrid
//假设是导入到表格中, 2开头表示忽略第一行表头
for k := 2 to x do
begin
eGridCells[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
// XLAppDisplayAlerts := False;
XLAppQuit;
XLAPP := Unassigned;
Sheet := Unassigned;
end;
end;
end;


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12594546.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存