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 :
Microsoft Dynamics AX (Archived)

find the last row of excel sheet - dynamics AX 2009

(0) ShareShare
ReportReport
Posted on by

How can I find the last row of excel sheet - dynamics AX 2009?

Object SysExcelSheet ... (Excel objects).

Thanks :)

*This post is locked for comments

I have the same question (0)
  • MukeshHirwani Profile Picture
    1,180 on at

    static void ReadExcel(Args _args)

    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row;
    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++;
        type = cells.item(row+1, 1).value().variantType();
        if(type == COMVariantType::VT_EMPTY)
            info(strfmt("Last row %1", row));
    }
    while (type != COMVariantType::VT_EMPTY);
         application.quit();
    }

  • Community Member Profile Picture
    on at

    I there any chance that the question type == COMVariantType::VT_EMPTY happens on the middle of the workshhet (suppose there is blank line on the middle).

    And second thing - should I search the whole sheet (until I reach the last ... there is possibility of lot of records, and this involves time cosuming issue ...)

    Thanks :)

  • Suggested answer
    MukeshHirwani Profile Picture
    1,180 on at

    Hi,

    U can add the logic for figuring out couple of blank rows, say about 10 blank rows occurred then u break the read from excel.

    No clues about tht kind of logic in X++, rather u can create c# code which does performs same thing.

    int columns;

    int rows;

    Microsoft.Office.Interop.Sheet sheet;

    sheet  = Globals.ThisWorkbook.ActiveSheet as Microsoft.Office.Interop.Sheet;

    if (sheet != null)

    {

      int NumberOfColumns = sheet.Cells.Find("*", Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, false, false, Type.Missing).Column;

    }

    Follow attached URL to create, deploy and call C# code in AX 2009:

    blogs.solidq.com/.../post.aspx

  • Verified answer
    PA-22040759-0 Profile Picture
    6,194 on at

    The ExcelHelper class from this blog post does the trick: natepaine.blogspot.dk/.../excel-helper-class-for-ax.html.

    I have used this in a current implementation.

  • Suggested answer
    Community Member Profile Picture
    on at

    There is much more efficient way to get this than looping through all cells until the end – using Excel’s special cells, and returning the value of last used row. It is fast enough to be used e.g. to get the maximum number of rows and initialize the progress bar, before processing the whole Excel document.

    Below is the code that returns the row number of the last cell in Excel document, and for more information you can search MSDN for SpeciallCells method in Excel Developer Reference.

  • Suggested answer
    Community Member Profile Picture
    on at

    // Created on 07 Mar 2013 by Jovan Bulajic

    // Returns the last used row in Excel file, using the Excel's Special Cells

    static int Excel_LastRow(Filename excelFile = @"C:\Temp\Testw.xls")

    {

       SysExcelApplication     excelApp;

       SysExcelWorkbooks       excelWorkBooks;

       SysExcelWorksheet       excelWorkSheet;

       SysExcelCells           excelCells;

       COM                     excelCOM;

       int                     lastRow;

       ;

       // Initialize Excel, open the first worksheet and get the last row information

       excelApp = SysExcelApplication::construct();

       excelWorkBooks = excelApp.workbooks();

       try

       {

           excelWorkbooks.open(excelFile);

       }

       catch (Exception::Error)

       {

           throw error(strFmt("Error opening Excel document (%1).", excelFile));

       }

       excelWorkSheet = excelWorkBooks.item(1).worksheets().itemFromNum(1);

       excelCells = excelWorkSheet.cells();

       excelCOM = excelCells.comObject();

       excelCOM = excelCOM.SpecialCells(11);   // 11 = xlCellTypeLastCell

       lastRow = excelCOM.Row();               // Row for the last cell

       excelApp.quit();

       Box::info(strFmt("Last used row in Excel file is %1.", lastRow));

       return lastRow;

    }

  • Community Member Profile Picture
    on at

    Hey Jovan Bulajic,

    this code works fine in my program but i change the records in my excel sheet the lastRow dont refresh the value.

    Example:

    I had a output in a SysOperationProgress Bar.

    Record *current series* of *lastRow*

    I change the rows from 700 to 39 but the variable lastRow stand on 700.

    I reset lastRow at the end of my code

    lastRow = 0;

    but the stand on 700.

    Need solutions.

    Thanks for any help.

    Felix

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans