如何解决过滤从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;
效果很好,但是数据太大,因此我想使用所需的数据。我需要获取日期范围内的数据。 这是图片来解释:
我只想显示我在顶部的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效率要比为所有内容预先分配足够的行以及像我所做的最后调整那样更有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。