-3

I need to write the contents of a TFDMemTable to a text file for later import into Excel. Is there a way to do this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ashley
  • 43
  • 9
  • 1
    Write the data into a CSV file, then load that into Excel. – MartynA Feb 16 '18 at 15:09
  • Don't you want to direct data export to Excel (using ODBC driver)? If not, then what @MartynA suggests is the way (you can use `TFDBatchMove` with `TFDBatchMoveTextWriter`). – Victoria Feb 16 '18 at 15:52
  • `TFDMemTable.SaveToFile()` can save to XML or JSON format. Excel can import XML, and there are 3rd party JSON-to-CSV converters available. Otherwise, you will have to loop through the table records manually writing each record to a file in whatever format you want, such as with a `TStreamWriter` – Remy Lebeau Feb 16 '18 at 16:00
  • @Remy, you don't have to do those extra steps. FireDAC has optimized use for exports implemented in the `TFDBatchMove` component. – Victoria Feb 16 '18 at 16:01
  • So im using the TKBMCSVSTREAMFORMAT and I get an error on this line: Inventory.mInventoryCount.SaveToFile('Inventory'); How do i specify the file type? – Ashley Feb 16 '18 at 16:07

1 Answers1

1

With FireDAC, you can use the TFDBatchMove component with TFDBatchMoveDataSetReader as Reader and TFDBatchMoveTextWriter as Writer. So as you can copy data to Excel DBMS by using ODBC driver with the mentioned component.

The TFDBatchMove component is optimized for performance and I would personally recommend its use whenever you need to copy data from one data source to another including this case since you can specify format, field mappings and others in a flexible way.

Victoria
  • 7,822
  • 2
  • 21
  • 44
  • So I have had some success, I found out where the access violation is coming from, for some reason if use the third party memtable component everything reads and writes fine to a .csv file. If i use the TFDMemtable component for some reason it is unable to access it. – Ashley Feb 19 '18 at 13:56
  • I'm sorry, but it's impossible for me to help without knowing what's happening (or knowing your code) :-) Just debug your application, let the access violation exception raise and inspect the call stack to find the source of the problem. – Victoria Feb 19 '18 at 14:02