Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Export data to csv x++

Posted on by 190

Hello everyone,

I am working in Dynamics 2012, trying to export some data in CSV format.

What I am trying to do is to export data of Field1 from TableA. Field1 is of type string, size 10. The problem is that one of the values that contains Field1 is  6549030E82 and when i open it in csv(excel) it is saved as scientific, as in the image here.  pastedimage1575572201314v1.png

What I want is that when I open  the file, to be shown as 6549030E82. 

To achieve this I have tried, both the following scenarios, but both of them show the scientific value. 

Scenario1.

public void exportCSV()
{
#File
commaIO commaIO;
FileIOPermission permission;

Table tableA;
container record;

permission = new FileIOPermission(fileName,'RW');
permission.assert();

commaIO = new commaIO(fileName,'W');
commaIO.outFieldDelimiter(';');

commaIO.write("@MIS95","@SYS59532","@SYS80949", 'line');


while select tableA
{
record = [tableA.Field1];
commaIO.writeExp(record);
}
CodeAccessPermission::revertAssert();
}

Scenario 2

public void exportExcel()
{

FileIOPermission fioPermission;
SysExcelApplication application;
SysExcelWorkBooks workBooks;
SysExcelWorkBook workBook;
SysExcelWorkSheet workSheet;
SysExcelCells cells;

int i;

application = SysExcelApplication::construct();
workBooks = application.workbooks();
workBook = workBooks.add();
workSheet = workBook.worksheets().itemFromNum(1);
cells = worksheet.cells();


while select TableA
{
cells.item(i,1).value(TableA.Field1);
i++;
}

workSheet .columns().autoFit();
application.displayAlerts(false);
workbook.saveAs(filename, 6);
workbook.comObject().save();
workbook.saved(true);
application.quit();
}

In the above scenario, I have even tried to format the whole excel column cell.range('A1:A100').NumberFormat('  ');, but it ignores the value 6549030E82 and shows empty cell. 

Can anyone suggest something in order to show the right value, i mean to show the string value and not convert it to scientific format. . I can use any of the scenarios, it is important to show the right value. 

Thanks in advance.

  • Elda Mataj Profile Picture
    Elda Mataj 190 on at
    RE: Export data to csv x++

    Thank you for your answer @Martin Drab.

    I tried your suggestion, but in this case it doesn't show the 6549030E82 at all(it shows empty cell). I need to know the right argument to pass to the numberFormat() function. I have searched, but I haven't found any documentation about it.

  • Ludwig Reinhard Profile Picture
    Ludwig Reinhard Microsoft Employee on at
    RE: Export data to csv x++

    Hi Eva,

    If you export a lot of data is a CSV/Excel export then the right instrument?

    The export might take long and slow down your whole system and maybe the data don't fit into a single excel document because of the row number limitation?!

    Best regards,

    Ludwig

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Export data to csv x++

    I think the problem is in your number format - you use just a space without any placeholder for the value. Try '@' instead.

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Export data to csv x++

    This blog explains how you can determine the formatting of the cells if you export to Excel via x++: http://patrikluca.blogspot.com/2010/04/export-to-excel-with-x-code-sequel.html

  • Elda Mataj Profile Picture
    Elda Mataj 190 on at
    RE: Export data to csv x++

    Thank you for your answer Nikolaos. Actually I have tried to export it in CSV and it takes the correct values. The problem is that the client wants to open the CSV directly in Excel. That's why I was trying to use the second scenario as above, but still I can't format the cells of the excel via code. That's what I am trying to achieve, to format the cell of Excel via code in order that shows the string value and not convert it to scientific format.

  • Elda Mataj Profile Picture
    Elda Mataj 190 on at
    RE: Export data to csv x++

    Thank you for your answer Ludwig. Actually, I am exporting a lot of data from a form, where Table A is one of the datasources. I have shown here, only Field1 because there is the problem.

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: Export data to csv x++

    CSV file itself doesn't contain any formatting information. You can verify this by opening the file in Notepad - you will see the values displayed correctly. When you open CSV in Excel, you can let Excel automatically determine the column data types, or you can define them yourself. In this scenario you need to tell Excel that this column is a string.

    In Excel, create a new blank document, go to Data -> From Text/CSV and choose your CSV file. Here you can define the column data types. Or, you can define them any time by selecting the column, Right click -> Format cells.

  • Suggested answer
    Ludwig Reinhard Profile Picture
    Ludwig Reinhard Microsoft Employee on at
    RE: Export data to csv x++

    Hi,

    Why you need to develop this.

    Does the normal CTRL + T Excel export not work for your table?

    Best regards,

    Ludwig

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans