过滤从txt文件加载到stringgrid的数据

问题描述

伙计们!

我有这个txt文件,我想将内容加载到stringgrid中。这是我的代码:

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;

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

STRINGGRID

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

解决方法

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

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

Scanner scanner = new Scanner(System.in);
double choice = scanner.nextDouble();

checkFever(choice);

System.out.println(choice);

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

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

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

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

此代码不合我意。我还没有测试! 注意:也许在每行更改网格RowCount效率要比为所有内容预先分配足够的行以及像我所做的最后调整那样更有效。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...