【问题标题】:filtering data loaded from txt file to stringgrid过滤从txt文件加载到stringgrid的数据
【发布时间】:2020-09-22 08:42:51
【问题描述】:

伙计们!

我有这个 txt 文件,我想将内容加载到字符串网格中。这是我的代码:

procedure PopulateStringGrid(Grid: TStringGrid; const FileName: string);
var
  TextFile, Line: TStringList;
  Row, col: Integer;
begin

  Grid.RowCount := 0;//clear any previous data
  TextFile := TStringList.Create;
  try
    Line := TStringList.Create;
    try
      Line.Delimiter := ' ';
      TextFile.LoadFromFile(FileName);
      Grid.RowCount := TextFile.Count;
      for Row := 0 to TextFile.Count-1 do
      begin
        Line.DelimitedText := TextFile[Row];
        for Col := 0 to Grid.ColCount-1 do
          if Col<Line.Count then
            Grid.Cells[Col, Row] := Line[Col]
          else
            Grid.Cells[Col, Row] := '0';
      end;
    finally
      Line.Free;
    end;
  finally
    TextFile.Free;
  end;
end;

procedure TForm5.sButton1Click(Sender: TObject);
var filename1:string;
begin
if opendialog1.Execute then
begin
sedit1.Text:=opendialog1.FileName;
filename1:=sedit1.Text;
PopulateStringGrid(StringGrid1, FileName1);
showmessage(filename1);
end
else showmessage('file failed to load');
end;

效果很好,但是数据太大,所以我想使用我需要的数据。我需要获取日期范围内的数据。 这是解释的图片:

我只想显示我在顶部的 dateedit 中选择的日期范围内的数据。知道怎么做吗? 先感谢您!我正在为此使用 delphi 7。

【问题讨论】:

    标签: text-files delphi-7 tstringgrid


    【解决方案1】:

    看起来存储在文本文件中的日期格式为YYYY-MM-DD,你可以像字符串一样比较日期(字符串的顺序与日期相同)。

    您应该在 PopulateStringGrid 函数中添加两个参数:

    procedure PopulateStringGrid(
        Grid: TStringGrid; 
        const FileName: string;
        const DateFrom: string;
        const DateTo: String);
    

    当您调用 PopulateStringGrid 时,您会从两个日期字段中传递日期:

    PopulateStringGrid(StringGrid1, FileName1, 
                       FormatDateTime('YYYY-MM-DD', DateFrom.Date),
                       FormatDateTime('YYYY-MM-DD', DateTo.Date));
    

    最后,在PopulateStringGrid中的循环中,检查包含日期的列号并检查日期范围,并为网格保持不同的行号,因为有些行没有添加。最后,将行数固定为找到的确切行数:

    procedure PopulateStringGrid(
        Grid: TStringGrid; 
        const FileName: string;
        const DateFrom: string;
        const DateTo: String);
    var
      TextFile, Line: TStringList;
      Row, col: Integer;
      RowGrid : Integer;
    begin
      Grid.RowCount := 0;//clear any previous data
      TextFile := TStringList.Create;
      try
        Line := TStringList.Create;
        try
          Line.Delimiter := ' ';
          TextFile.LoadFromFile(FileName);
          Grid.RowCount := TextFile.Count;
          RowGrid := 0;
          for Row := 0 to TextFile.Count-1 dobegin
            Line.DelimitedText := TextFile[Row];
            if (Line[COL_WITH_DATE] >= DateFrom) and
               (Line[COL_WITH_DATE] <= DateTo) then begin
                for Col := 0 to Grid.ColCount-1 do begin
                  if Col<Line.Count then
                    Grid.Cells[Col, RowGrid] := Line[Col]
                  else
                    Grid.Cells[Col, RowGrid] := '0';
                end;
                Inc(RowGrid);
            end;
          end;
          Grid.RowCount := RowGrid;
        finally
          Line.Free;
        end;
      finally
        TextFile.Free;
      end;
    end;
    

    这段代码让我想不通。我没有测试过! 注意:也许在每一行更改网格 RowCount 比为所有内容预先分配足够的行并像我所做的那样在最后进行调整更有效。

    【讨论】:

    • 嗨,你的回答真的很有帮助!它确实有效,但是我遇到了这样的情况。假设我想要 20-08-2020 和 21-08-2020 的数据范围。该文件还包含 2020 年 8 月 19 日的数据。所以我的 stringgrid 将返回与 19-08-2020 文件一样多的空网格。如何使空白不显示而我的 stringgrid 只显示所需的数据?非常感谢!
    • @mizkyd “你的回答真的很有帮助!它确实有效”请将答案标记为“已接受”(答案左侧的复选标记)。
    • @mizkyd 对不起,我不明白你的最后评论。我的代码只为范围内的数据添加了一个网格行。也许您在复制/调整我的代码时犯了一个小错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多