Create or Export data in CSV File from NAV/Business Central on-premises

Hi folks,

This is a very important article that helps a lot to developers when the user wants data in a CSV file. To achieve this CSV export I will create a new function ExportCustomersToCSV() this method will export the data in CSV file to the specified path, for this I will use the CSV Buffer table (table 1234). Using this table I will use it's 'InsertEntry' function to add data by specifying the line number, field number, and value. And then will call the 'SaveData' function to create the CSV file. The 'SaveData' function is called with a specified path with the file name and field separator, in this article I use a comma as a field separator(for creating comma ',' delimited CSV file). 
 
This article is related to the following: 
* Create CSV File using the CSV Buffer Table in NAV/Business Central on-premises
* Export data in CSV format from Navision
* Export data in CSV format file from Business Central on-premises

Now let's have a look to the below code, you can call this function on the desired action:
// Code to Export data in CSV file

    procedure ExportCustomersToCSV()
    var
        LineNo: Integer;
        TempCSVBuffer: Record "CSV Buffer" temporary;
        Customer: Record Customer;
        filePath: Text;
    begin
        LineNo := 1;
        TempCSVBuffer.InsertEntry(LineNo, 1, 'Customer No.');
        TempCSVBuffer.InsertEntry(LineNo, 2, 'Customer Name');
        TempCSVBuffer.InsertEntry(LineNo, 3, 'Customer Address');
        if Customer.FindSet() then
            repeat
                LineNo += 1;                
                TempCSVBuffer.InsertEntry(LineNo, 1, Customer."No.");
                TempCSVBuffer.InsertEntry(LineNo, 2, Customer.Name);
                TempCSVBuffer.InsertEntry(LineNo, 3, Customer.Address);
            until Customer.Next() = 0;
        TempCSVBuffer.SaveData('C:\ExportedCSV\Customer.csv', ',');
    end;
OUTPUT:
After successfully calling of this function this will create a file called Customer.csv in the given folder i.e. 'C:\ExportedCSV\' and the result will look like the below screenshot:
 

Done!

I hope this article will help you whenever clients want Business Central Data in CSV format or in an excel file.

 

Stay Tuned!

 
 
 
 

Comments (3) -

518256 618403Wow, great blog layout! How long have you been blogging for? you make blogging look simple. The overall look of your web website is great, let alone the content! 591727

Hi Tabrez,
This code worked perfectly in business central 14.0. But, when i try to implement the same code in business central latest version it is not allowing at line "CSVBuffer".SaveData with following error.

"The type or method 'SaveData' cannot be used for 'Extension' development".
Could you please help me on this ?

Hi Murali,
In which version you are facing this issue, can you please confirm?

Add comment