[英]Delphi cxGrid rowcount
我使用 cxGrid,我想将一个 excel 文件导入 cxgrid。 我把这段代码写成了一个函数。
但是,这是错误的,因为 cxGrid 不知道 RowCount 和 ColCount。 我想知道,我可以使用什么,有什么相似之处? 帮我! 谢谢!
function Xls_To_cxGrid(AGrid: TcxGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
XLApp := CreateOleObject('Excel.Application');
try
XLApp.Visible := False;
XLApp.Workbooks.Open(AXLSFile);
Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
x := XLApp.ActiveCell.Row;
y := XLApp.ActiveCell.Column;
AGrid.RowCount := x;
AGrid.ColCount := y;
RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
k := 1;
repeat
for r := 1 to y do
AGrid.Cells[(r - 1), (k - 1)] := RangeMatrix[K, R];
Inc(k, 1);
AGrid.RowCount := k + 1;
until k > x;
RangeMatrix := Unassigned;
finally
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;
cxGrid 只是 ChartViews、TableViews、CardViews 和 BandedTableViews 的容器。 作为一个容器,它对行和列一无所知。 因为要替换 StringGrid,所以我建议使用(非 DB)TableView。
要获得一个 TableView...
(另请参阅标题“指定关联的网格视图”下的 DX 帮助主题“网格级别”。)
然后您将使用YourView.DataController.RecordCount
而不是“RowCount”。 “ColCount”将是YourView.ColumnCount
。 您可以使用YourView.DataController.Values[...]
访问单元格值。 为了加快填充视图,我会用它包围它
YourView.DataController.BeginUpdate;
try
YourView.DataController.RecordCount := x;
// ...
finally
YourView.DataController.EndUpdate;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.