Bookstore

Recent

Author Topic: Exporting xlsx spreasheet into a tab-separated flat file?  (Read 180 times)

tatamata

  • Hero Member
  • *****
  • Posts: 713
    • ZMSQL - SQL enhanced in-memory database
Exporting xlsx spreasheet into a tab-separated flat file?
« on: February 16, 2020, 08:23:54 pm »
Hi, trying to export a spreadsheet to a tab-delimited file (CSV file with "\t" as field separator).
How to specify specific field separator ("\t") for the TsWorkbook.WriteToFile (vDestFileName, sfCSV, True) ?
Any other convenient way to export xlsx spreadsheet into a tab-separated file?
Code: Pascal  [Select]
  1. function ExportSpreadsheetToTabFile(pFileName:string): string;
  2. var
  3.   MyWorkbook: TsWorkbook;
  4.   vSourceFileName: string;
  5.   vDestFileName: string;
  6. begin
  7.   vSourceFileName:=pFileName;
  8.   vDestFileName:= ExtractFileNameWithoutExt(pFileName) + '.tab';
  9.   try
  10.     MyWorkbook := TsWorkbook.Create;
  11.     MyWorkbook.ReadFromFile(vSourceFileName);
  12.  
  13.     MyWorkbook.WriteToFile(vDestFileName, sfCSV, True); //How to specify field delimiter?!?
  14.  
  15.     ExportSpreadsheetToTabFile:=vDestFileName;
  16.  
  17.   finally
  18.     MyWorkbook.Free;
  19.   end;
  20. end;  
  21.  

winni

  • Hero Member
  • *****
  • Posts: 1115
Re: Exporting xlsx spreasheet into a tab-separated flat file?
« Reply #1 on: February 16, 2020, 09:13:08 pm »
Hi!

The record TsCSVParams has a member  called Delimeter.
Code: Pascal  [Select]
  1. var Para : TsCSVParams;
  2. begin
  3. Para.Delimeter := #9; // TAB
  4. ....

This page has a good explanation  about fpSpeadSheet

https://wiki.freepascal.org/FPSpreadsheet

Winni