【问题标题】:Delphi cxGrid rowcountDelphi cxGrid 行数
【发布时间】:2012-11-20 16:19:29
【问题描述】:

我使用 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;

【问题讨论】:

    标签: excel delphi rowcount


    【解决方案1】:

    cxGrid 只是 ChartViews、TableViews、CardViews 和 BandedTableViews 的容器。作为一个容器,它对行和列一无所知。因为您想替换 StringGrid,所以我建议使用(非 DB)TableView。

    获取 TableView...

    • ...打开网格自定义表单(在表单设计器的网格“结构导航器”中单击“自定义...”)。
    • 转到“视图”选项卡并单击“添加视图...”。选择“表”(不是“数据库表”!)。
    • 在“结构”选项卡上,右键单击 cxGrid1Level1 符号,选择“选择视图”和新的 TableView。
    • 您现在可以返回“视图”选项卡并删除 DB TableView (cxGrid1DBTableView1)。

    (另请参阅标题“指定关联的网格视图”下的 DX 帮助主题“网格级别”。)

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

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

    【讨论】:

    • cxGrid 只是 ChartViews、TableViews、CardViews 和 BandedTableViews 的容器。作为一个容器,它对行和列一无所知。
    • @alzaimar,我采纳了你的评论。
    • 我的表格视图名称是:cxGrid1DBTableView1,我使用的是:cxGrid1DBTableView1.DataController.RecordCount := x;但它未声明,我尝试了 AGrid.cxGrid1DBTableView1.DataController.RecordCount := x;,但它也是。我该如何使用,你写给我的?而且我不想使用 StringGrid,因为我有一个 StringGrid,我想替换那个新的数据库网格。谢谢! :)
    • @GodGirl:正如我所写,您应该使用 non-DB 视图。我将通过一些步骤来编辑我的答案。
    • "•在“结构”选项卡上右键单击 cxGrid1Level1 符号,选择“选择视图”和新的 TableView。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多