中易网

delphi7 打开excel

答案:1  悬赏:0  
解决时间 2021-01-07 02:10
delphi7 打开excel
最佳答案
拿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开头表示忽略第一行表头
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;
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
台风又要来了吗?
双色球2015第001期至第097期开奖结果查询
北京工商大学嘉华学院金融专业到底是怎么样的
为什么我用TeamViewer连不上别人的电脑
零基础过英语四级,基础真的是差到没朋友了。
章华中路51号潜江大酒店属于湖北省潜江市哪个
小学二年级开始学习奥数好不好?
中餐厅取名!要突出鱼。但是有要包含中餐
大家知道芝麻官吗?可以介绍下他的老板曹代树
大庆市东源印刷有限公司我想知道这个在什么地
昨天cba赢了还是输了?
汽车未来会不会飞
碧玉美眼spa养生会馆地址有知道的么?有点事
我要买07年款的耐克鞋
孔子家族出自商王族,孟子家族出自周王族,哪
推荐资讯
问秦淮旧日窗寮,破纸迎风,坏槛当潮 “槛”
什么叫做紫薇入命,贪狼入命。什么叫入命?
魔兽香炉可以买什么坐骑
知道保单号查保险公司
青春豆是怎么形成的
imb notes使用中出现circular USE:DocExproce
泰州水,电,天然气费过户在哪办
加油站(湖坎线)地址在什么地方,我要处理点事
求一台适合办公学习用的电脑,之前在surface
求《圈养娱美人》全本
三星9002价格多少
led触摸宽屏控制和led数码屏显示哪个好
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?