ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Convert XLSX file to CSV with Delphi
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

xequte

38180 Posts

Posted - Aug 24 2020 :  18:32:29  Show Profile  Reply
Usage: ConvertExcelFile( 'D:\import.xlsx', 'D:\export.csv', xlCSVUTF8 );
Requires Excel to be installed on system


const
  xlAddIn                       = 18;    // Microsoft Excel 97-2003 Add-In *.xla
  xlAddIn8                      = 18;    // Microsoft Excel 97-2003 Add-In *.xla
  xlCSV                         = 6;     // CSV *.csv
  xlCSVMac                      = 22;    // Macintosh CSV *.csv
  xlCSVMSDOS                    = 24;    // MSDOS CSV *.csv
  xlCSVUTF8                     = 62;    // UTF8 CSV *.csv
  xlCSVWindows                  = 23;    // Windows CSV *.csv
  xlCurrentPlatformText         = -4158; // Current Platform Text *.txt
  xlDBF2                        = 7;     // Dbase 2 format *.dbf
  xlDBF3                        = 8;     // Dbase 3 format *.dbf
  xlDBF4                        = 11;    // Dbase 4 format *.dbf
  xlDIF                         = 9;     // Data Interchange format *.dif
  xlExcel12                     = 50;    // Excel Binary Workbook *.xlsb
  xlExcel2                      = 16;    // Excel version 2.0 (1987) *.xls
  xlExcel2FarEast               = 27;    // Excel version 2.0 far east (1987) *.xls
  xlExcel3                      = 29;    // Excel version 3.0 (1990) *.xls
  xlExcel4                      = 33;    // Excel version 4.0 (1992) *.xls
  xlExcel4Workbook              = 35;    // Excel version 4.0. Workbook format (1992) *.xlw
  xlExcel5                      = 39;    // Excel version 5.0 (1994) *.xls
  xlExcel7                      = 39;    // Excel 95 (version 7.0) *.xls
  xlExcel8                      = 56;    // Excel 97-2003 Workbook *.xls
  xlExcel9795                   = 43;    // Excel version 95 and 97 *.xls
  xlHtml                        = 44;    // HTML format *.htm; *.html
  xlIntlAddIn                   = 26;    // International Add-In
  xlIntlMacro                   = 25;    // International Macro
  xlOpenDocumentSpreadsheet     = 60;    // OpenDocument Spreadsheet *.ods
  xlOpenXMLAddIn                = 55;    // Open XML Add-In *.xlam
  xlOpenXMLStrictWorkbook       = 61;    // Strict Open XML file *.xlsx
  xlOpenXMLTemplate             = 54;    // Open XML Template *.xltx
  xlOpenXMLTemplateMacroEnabled = 53;    // Open XML Template Macro Enabled *.xltm
  xlOpenXMLWorkbook             = 51;    // Open XML Workbook *.xlsx
  xlOpenXMLWorkbookMacroEnabled = 52;    // Open XML Workbook Macro Enabled *.xlsm
  xlSYLK                        = 2;     // Symbolic Link format *.slk
  xlTemplate                    = 17;    // Excel Template format *.xlt
  xlTemplate8                   = 17;    // Template 8 *.xlt
  xlTextMac                     = 19;    // Macintosh Text *.txt
  xlTextMSDOS                   = 21;    // MSDOS Text *.txt
  xlTextPrinter                 = 36;    // Printer Text *.prn
  xlTextWindows                 = 20;    // Windows Text *.txt
  xlUnicodeText                 = 42;    // Unicode Text
  xlWebArchive                  = 45;    // Web Archive *.mht; *.mhtml
  xlWJ2WD1                      = 14;    // Japanese 1-2-3 *.wj2
  xlWJ3                         = 40;    // Japanese 1-2-3 *.wj3
  xlWJ3FJ3                      = 41;    // Japanese 1-2-3 format *.wj3
  xlWK1                         = 5;     // Lotus 1-2-3 format *.wk1
  xlWK1ALL                      = 31;    // Lotus 1-2-3 format *.wk1
  xlWK1FMT                      = 30;    // Lotus 1-2-3 format *.wk1
  xlWK3                         = 15;    // Lotus 1-2-3 format *.wk3
  xlWK3FM3                      = 32;    // Lotus 1-2-3 format *.wk3
  xlWK4                         = 38;    // Lotus 1-2-3 format *.wk4
  xlWKS                         = 4;     // Lotus 1-2-3 format *.wks
  xlWorkbookDefault             = 51;    // Workbook default *.xlsx
  xlWorkbookNormal              = -4143; // Workbook normal *.xls
  xlWorks2FarEast               = 28;    // Microsoft Works 2.0 far east format *.wks
  xlWQ1                         = 34;    // Quattro Pro format *.wq1
  xlXMLSpreadsheet              = 46;    // XML Spreadsheet *.xml

// Usage: ConvertExcelFile( 'D:\import.xlsx', 'D:\export.csv', xlCSVUTF8 );
// Requires Excel to be installed on system
// If file is locked after usage, call Sleep(10), then Application.ProcessMessages();
// Note: Add ComObj,ActiveX to your uses clause
procedure ConvertExcelFile(const ExcelFile, OutFile: string; Format: Integer = xlCSV);
var
  excelApp: OleVariant;
begin
  CoInitialize(nil);
  try
    excelApp := CreateOleObject('Excel.Application');
    if VarIsEmpty( excelApp ) then
      exit;

    excelApp.DisplayAlerts := False;
    excelApp.Visible := False;
    excelApp.Workbooks.Open( ExcelFile,
                             false,   // ConfirmConversions
                             true );  // ReadOnly

    excelApp.ActiveWorkbook.SaveAs( OutFile, Format );

    excelApp.ActiveWorkbook.Saved := True; // Prevent prompt

  finally
    excelApp.Quit;
    excelApp := Unassigned;
    CoUninitialize;
  end;
end;


Nigel
Xequte Software
www.imageen.com
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: