Dim rRow As Long
Dim lRow As Long
Dim i As Long
rRow = Sheet1.UsedRange.Row
lRow = rRow + Sheet1.UsedRange.Rows.Count - 1
For i = lRow To rRow Step -1
If Application.WorksheetFunction.CountA(Rows(i)) = 0 Then
Rows(i).Delete
End If
Next
上面的VBA代码转换到平台中
var
objApplication,objWorkBooks:Object;
dispatchApplication,dispatchWorkBooks,dispatchSourceWB: System.DispatchHelper;
objSheet, objUsedRange, objRows, objWorkFun, objRow, objWorkFun: Object;
dispatchSheet, dispatchUsedRange, dispatchRows, dispatchWorkFun, dispatchRow, dispatchWorkFun: System.DispatchHelper;
iRowsCount, rRow, lRow, I: Integer;
begin
objApplication := ComObj.CreateOleObject('Excel.Application');
dispatchApplication:= System.DispatchHelper.Create(objApplication);
objWorkBooks := dispatchApplication.PropertyGet('WorkBooks',[]);
dispatchWorkBooks := System.DispatchHelper.Create(objWorkBooks);
dispatchSourceWB := System.DispatchHelper.Create(dispatchWorkBooks.InvokeMethod('Open',[fileName]));
objsheet := dispatchSourceWB.PropertyGet('Sheets',[sheetName]);
dispatchSheet := System.DispatchHelper.Create(objSheet);
//****** 删除空行
objUsedRange := dispatchSheet.PropertyGet('UsedRange',[]);
dispatchUsedRange := System.DispatchHelper.Create(objUsedRange);
rRow := ObjectHelper.ToInt(dispatchUsedRange.PropertyGet('Row',[]));
objRows := dispatchUsedRange.PropertyGet('Rows',[]);
dispatchRows := System.DispatchHelper.Create(objRows);
lRow := rRow + ObjectHelper.ToInt(dispatchRows.PropertyGet('Count',[])) - 1;
objWorkFun := dispatchApplication.PropertyGet('WorksheetFunction',[]);
dispatchWorkFun := System.DispatchHelper.Create(objWorkFun);
for I := lRow downto rRow do
begin
objRow := dispatchUsedRange.PropertyGet('Rows',[I]);
dispatchRow := System.DispatchHelper.Create(objRow);
if ObjectHelper.ToInt(dispatchWorkFun.InvokeMethod('CountA',[objRow])) = 0 then //执行到这句时出错,类型不匹配?怎么办?
dispatchRow.InvokeMethod('Delete',[]);
end;
//*******
end; |