web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Export data to csv x++

(0) ShareShare
ReportReport
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.

I have the same question (0)
  • Suggested answer
    Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Hi,

    Why you need to develop this.

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

    Best regards,

    Ludwig

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    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.

  • Elda Mataj Profile Picture
    190 on at

    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.

  • Elda Mataj Profile Picture
    190 on at

    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.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    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

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

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

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    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

  • Elda Mataj Profile Picture
    190 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans