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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Import Excel file through X++ code but got error

(0) ShareShare
ReportReport
Posted on by 875

My code is and got error like

static void RB_ReadExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FilenameOpen fileData = @"C:\import_journal.csv";

int row = 1;
date dat;
real salary;
int vou;
str TransType;
str account;
int value;
str descr;
int deb;
int credit;
str ack_type;
int offset__Account;




int totNoOfLeaves;

;

application = SysExcelApplication::construct();
workbooks = application.workbooks();
fileData = "C:\\import_journal.csv.xlsx"; // file path

try
{
workbooks.open(fileData);
}
catch (Exception::Error)
{
throw error("File not found");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();

//Iterate through cells and get the values
do
{
//Incrementing the row line to next Row
row++;
dat=cells.item(row,1).value().date();
salary=cells.item(row,2).value().decimal();
vou=cells.item(row,3).value().char();
value=cells.item(row,4).value().int() ;
deb=cells.item(row,5).value().int();
credit=cells.item(row,6).value().int();
offset__Account=cells.item(row,7).value().int();






info(strFmt("Date=%1, Salary=%2, Voucher=%3 ,Value=%4 ,Debit=%5,Credit=%6,Offset=%7",dat,salary,vou,value,deb,credit,offset__Account));

// Loads the next row into the variant type and validating that its is empty or not
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);

// quits the application
application.quit();

*This post is locked for comments

I have the same question (0)
  • Verified answer
    DG Profile Picture
    1,226 on at
    RE: Import Excel file through X++ code but got error

    Before I find the cause, can you please let me know

    1. If the error is coming up on the line 'application = SysExcelApplication::construct();'

    2. Is the code running on client or server?

    3. Has excel installed and working properly on client machine?

    4. Which version of excel is installed?

    Regards,

    Deepak

  • Verified answer
    Rudi Hansen Profile Picture
    4,075 on at
    RE: Import Excel file through X++ code but got error

    Try changing the // in the filename to \\ or even better change it to this.

    fileData = @"C:\import_journal.csv.xlsx"; // file path

  • Vikas_AX_Blogs Profile Picture
    875 on at
    RE: Import Excel file through X++ code but got error

    I have done changes  but same error coming.

  • Vikas_AX_Blogs Profile Picture
    875 on at
    RE: Import Excel file through X++ code but got error

    code running on  server side.

  • Verified answer
    dolee Profile Picture
    11,279 on at
    RE: Import Excel file through X++ code but got error

    Does the server have Excel installed? If it doesn't, it won't work.

    And make sure the path you specified can be accessed by the credentials running the code.

    P.S. If it's on the server, C:\\import_journal.csv could be tricky. Consider create a subfolder and put the import file there. e.g. C:\\MyFolder\\import_journal.csv

  • Vikas_AX_Blogs Profile Picture
    875 on at
    RE: Import Excel file through X++ code but got error

    Thanks Dominic and deepak

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 3

#3
Community Member Profile Picture

Community Member 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans