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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Weird problem while load excel file to AX

(1) ShareShare
ReportReport
Posted on by 1,559

Hi Friends,

I am reading from a simple excel file into AX table. Here is standard code, my excel file has only one column. The problem I found is that, after I created a test file, it return empty from "cells.item(row, 1).value().bStr()" while reading it.  So I tried to add second column, then, it worked.

My version of Office is 2016, so I am not sure that played a role here. My AX is 2009.

===============================

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
ItemId itemid;
Name name;
FileName filename;
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "C:\\item.xls";
try
{
    workbooks.open(filename);
}
catch (Exception::Error)
{
    throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
    row++;
    itemId = cells.item(row, 1).value().bStr();
    info(strfmt('%1', itemId));
    type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You seem to be using the old Excel format, try xlsx instead.

    Also this link shows that you could refer columns by alphabetic values, try that too.

    http://www.homeandlearn.org/the_cells_property.html

  • André Arnaud de Calavon Profile Picture
    304,138 Super User 2026 Season 1 on at

    Hi Ganriver,

    What values are stored in the first column? If they are considered as numbers, the bStr() function is incorrect. If you look at standard classes from AX2009 related to Excel import and export you will notice other functions to get values from Excel cells depending on the cell value type.

  • Ganriver1 Profile Picture
    1,559 on at

    first column is string, and my file format is .xlsx.

    Thanks.

  • Suggested answer
    Zain Bokhari Profile Picture
    3,208 Moderator on at

    Hi,

    In the code you pasted above the extension you mentioned is filename = "C:\\item.xls"; it should be item.xlsx if your file is .xlsx

    Secondly select your first column in excel file and change the number formating to General or Text.

    2161.Capture.PNG

    I've tested your code it works fine. Except for the part that I don't have your excel file and that in the file path you put extension as .xls instead of .xlsx

  • Ganriver1 Profile Picture
    1,559 on at

    Vilmos,

    I tried alphabetic value, AX doesn't like it. It won't compile.

  • Ganriver1 Profile Picture
    1,559 on at

    Zain,

    I tried with a new file. I got empty in first row, but, rest of rows are fine:-(

    Weird thing is when I reload file again, then, it get first row/column value correctly.

    format is default to general, so I changed it to 'Text', that worked with a new file.

  • Ganriver1 Profile Picture
    1,559 on at

    Now even format is 'text', it's not working.

    I have done a lot of testing, I can only say this code is very unstable, sometimes work, sometimes don't.

    Not sure where is wrong.

  • Suggested answer
    Zain Bokhari Profile Picture
    3,208 Moderator on at

    I have tried your code and it works fine for me.

    Did you try to debug? Try that and

    Please take a look at this Microsoft Dynamics AX 2009 Development Cookbook on how to work with excel files in AX 2009

    Visit these too. 

    Link 1

    Link 2

    Good luck.

  • Ganriver1 Profile Picture
    1,559 on at

    I would think it's my office version, we have 365.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    Office 2016 is supported with certain service packs and hotfixes in AX 2009, refer to this article to confirm if you have them or not:

    blogs.technet.microsoft.com/.../announcing-support-for-office-2016-with-ax-2009-sp1

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
GiacomoRovai Profile Picture

GiacomoRovai 4

#3
Douglas Noel Profile Picture

Douglas Noel 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans