Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Reading Excel files

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
11/15/2012 5:43:24 PM

Reading Excel files is another side of Excel file manipulation. It is equally important as file creation. Usage could vary from importing simple data to processing user-filled files like timesheets, purchase orders, etc.

In this recipe, we will use the file created in the previous recipe. We will read customer data using SysExcel classes and will show it on the screen.

How to do it...

  1. 1. In AOT, create a new class called ReadExcelFile with the following code:

    class ReadExcelFile
    
    {
    }
    public static void main(Args args)
    
    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row;
    CustAccount account;
    CustName name;
    #define.Filename('<documents>\\customers.xlsx')
    ;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    try
    Excel fileExcel filereading, steps{
    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++;
    account = cells.item(row, 1).value().bStr();
    name = cells.item(row, 2).value().bStr();
    info(strfmt('%1 - %2', account, name));
    type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    
    
    					  
  2. }

  1. 2. Run the class and check the results displayed in the Infolog:

How it works...

In the variable declaration section in main(), replace<documents> with your own directory path and customers.xlsx with your file saved in the previous recipe. Do not forget to use double backslashes for folder separation, i.e. \\.

The first few lines of code in the main() method creates a new Excel application object application, opens first workbook, and gets a reference to the cell collection object cells. This is done very much in the same way as in the previous recipe.

Next, we use a do…while loop until the first cell of the next row is empty. Inside the loop, we read the customer account from the first and customer name from the second cell of each row and output them on the screen using the info() function. The value() method of cells returns an object of type COMVariant. We know that both columns are formatted as text, so we use bStr() to get cell content.

Speaking about cell formats, the COMVariant class is used for storing various types of data when dealing with external objects. It could be of any type like string, integer, decimal, etc. In most cases when we do not know what type of data to expect for each cell, we may call variantType(), which returns a COMVariantType enumeration. This enumeration shows what kind of data is stored in the cell, and depending on the result, we may use bStr(), int(), float(), or other relevant methods of COMVariant. Normally, a whole range of checks has to be performed to determine the correct data type. A good example of such checks could be convertVariant2Str() of COSExcelImport or variant2Str() of GanttVcDataTableField in the standard application.

Other -----------------
- Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Creating Excel files
- SQL Server 2008 R2 : Configuring Resource Governor (part 2) - Defining Workload Groups, Creating Workload Groups in T-SQL
- SQL Server 2008 R2 : Configuring Resource Governor (part 1) - Enabling Resource Governor, Defining Resource Pools
- SQL Server 2008 R2 : Overview of Resource Governor, Resource Governor Components
- Microsoft Dynamic GP 2010 : Sales Order Processing (part 2)
- Microsoft Dynamic GP 2010 : Sales Order Processing (part 1) - Sales Order Processing Setup, Sales Document Setup
- Planning and Designing a Public Key Infrastructure : Creating a Certificate Management Plan
- Planning and Designing a Public Key Infrastructure : Designing the CA Hierarchy
- Planning and Designing a Public Key Infrastructure : Identifying PKI Requirements
- Share point 2010 : Managing Data Connections (part 4) - Modifying BDC Objects, Using External System Throttling
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer