简体   繁体   English

如何在 Lazarus/Free Pascal 的 DBGrid 中加载 XML/CSV/ODB 文件?

[英]How to load a XML/CSV/ODB file in DBGrid in Lazarus/Free Pascal?

I want to open a database/spreadsheet in a TDBGrid in Lazarus, and I've tried DBGrid1.LoadFromFile(filename);我想在 Lazarus 的 TDBGrid 中打开一个数据库/电子表格,我试过DBGrid1.LoadFromFile(filename); with xml, csv, xlsx, odb.与 xml、csv、xlsx、odb。 But with every file I get an error.但是对于每个文件,我都会收到错误消息。

This is the code I'm using, with 'xml' being different each time I try:这是我正在使用的代码,每次尝试时“xml”都不同:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.LoadFromFile('/home/pi/Jesse Gielen/PWS/Databeesjes/WoahWeerGegevens.xml');
end; 

When I try xml, I get this error: EDOMHierarchyRequest in Document.InsertBefore当我尝试 xml 时,我收到此错误: EDOMHierarchyRequest in Document.InsertBefore

When I try csv, this error appears:当我尝试 csv 时,出现此错误:

Project project1 raised exception class 'EXMLReadError' (line 1 pos 1): Illegal at document level

When xlsx: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character当 xlsx: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character

When odb same as xlsx: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character当 odb 与 xlsx 相同时: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character

So: How can I open a file in a DBGrid in Lazarus?那么:如何在 Lazarus 的 DBGrid 中打开文件? If this isn't possible in any way (which I would find stange because why is there a procedure for it then?) then is my question: How can I translate a TStringgrid to a TDBGrid?如果这不可能以任何方式(我会发现 stange,因为那为什么有一个程序呢?)那么我的问题是:如何将 TStringgrid 转换为 TDBGrid? (because loading a file into TStringgrid worked fine) (因为将文件加载到 TStringgrid 工作正常)

You are asking the wrong question.你问错问题了。 The way to think about this is that the TDBGrid displays the data fed to it via a TDataSource from a TDataSet descendant.考虑这一点的方法是,TDBGrid 显示通过 TDataSource 从 TDataSet 后代提供给它的数据。 Therefore what you need to do is to load the TDataSet descendant from the file, and the data will then automatically display in the TDBGrid (while the dataset is open).因此,您需要做的是从文件中加载 TDataSet 后代,然后数据将自动显示在 TDBGrid 中(当数据集打开时)。

Lazarus's standard components include several TDataSet descendants, including TMemDataSet and TCSVDataSet, with LoadFromFile and LoadFromStream methods. Lazarus 的标准组件包括几个 TDataSet 后代,包括 TMemDataSet 和 TCSVDataSet,带有 LoadFromFile 和 LoadFromStream 方法。 Check the online documentation for which one is best suited to your needs.查看最适合您需求的在线文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM