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的容器。 作为一个容器,它不知道任何有关行和列的内容。 因为你想要replace一个StringGrid我build议使用(非数据库)TableView。

得到一个TableView …

  • …打开网格自定义表单(单击表单devise器中网格的“结构导航器”中的“自定义…”)。
  • 转到“视图”标签,然后点击“添加视图…”。 select“表”( 而不是 “数据库表”!)。
  • 在“Structure”选项卡上,右键单击cxGrid1Level1符号,select“Select View”和新的TableView。
  • 现在可以回到“视图”选项卡并删除数据库表视图(cxGrid1DBTableView1)。

(另请参阅DX帮助主题“网格层次”,取消标题“指定关联的网格视图”。)

而不是“RowCount”,然后使用YourView.DataController.RecordCount 。 “ColCount”将是YourView.ColumnCount 。 您可以使用YourView.DataController.Values[...]来访问单元格值。 为了加速填充视图,我将它包围

 YourView.DataController.BeginUpdate; try YourView.DataController.RecordCount := x; // ... finally YourView.DataController.EndUpdate; end;