Friday 11 April 2014

Edit MS Excel cell data using NAV 2009 C/AL code

Well I have imported Excel Data to the NAV Table and Exported data from NAV to Excel for so many times as required.

But here i would like to share Editing excel cell data and saving that file which has multiple sheets through NAV C/AL code.

Sample Code(Tested & Approved):

Variables:
Name DataType Subtype Length
SheetName Text 250
XlWrkSht Automation Unknown Automation Server.Worksheet
XlRange Automation Unknown Automation Server.Range
XlApp Automation Unknown Automation Server.Application
XlWrkBk Automation Unknown Automation Server.Workbook

XlWrkshts Automation Unknown Automation Server._Worksheet

CREATE(XlApp,TRUE,TRUE);
XlApp.Workbooks.Open('C:\Sample\Test.xlsx');  //Excel Sheet fully qualified path
XlWrkBk := XlApp.ActiveWorkbook;
XlWrkshts := XlWrkBk.Worksheets.Item(1);       //selecting the particular sheet among multiple Worksheets
SheetName := XlWrkshts.Name;
XlWrkSht := XlWrkBk.Worksheets.Item(SheetName);
XlRange := XlWrkSht.Range('AH7');                   //select the particular cell
XlRange.Value := 'updated Test Value';               //Cell value will be updated with the 'updated Test Value'
XlWrkBk.Close(TRUE);                                    //file will be saved and closed.
XlApp.Quit;

CLEAR(XlApp);

No comments:

Post a Comment