Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Import Excel file through X++ code but got error

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

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

    Thanks Dominic and deepak

  • Verified answer
    dolee Profile Picture
    dolee 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
    Vikas_AX_Blogs 875 on at
    RE: Import Excel file through X++ code but got error

    code running on  server side.

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

    I have done changes  but same error coming.

  • Verified answer
    Rudi Hansen Profile Picture
    Rudi Hansen 3,987 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

  • Verified answer
    DG Profile Picture
    DG 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

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans