Wednesday, May 20, 2015

Importing warehouses from CSV file

Here is the code for importing warehouses from excel file,

static void InventWarehouseImportFromExcel(Args _args)
{
    SysExcelApplication   application;
    SysExcelWorkbooks   workbooks;
    SysExcelWorkbook     workbook;
    SysExcelWorksheets   worksheets;
    SysExcelWorksheet     worksheet;
    SysExcelCells              cells;
    COMVariantType        type;
    FileName                     filename;
    int                                 row =0 ;
    str                                 ProductName;
    str                 _              ProductCode;
 
    InventSiteId                 inventSiteId;
    InventLocationName   inventLocationName;
    InventLocationId         inventLocationId;
    InventLocation            inventLocation;
 
 
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
 
    //specify the file path that you want to read
    filename = "C:\\ sample path \\test.xlsx"; //add file path here.
 
    try
    {
        //  Adds the file as the first document in the collection.
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
 
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);   // represents the current worksheet in my case it's (1)
    cells = worksheet.cells();
 
    // Fetches data from each cell that contains data.
    do
    {
        row++;
 
        inventSiteId = cells.item(row, 1).value().bStr();
        InventLocationId = cells.item(row, 3).value().bStr();
        InventLocationName = cells.item(row, 4).value().bStr();
 
        ttsBegin;
 
        inventLocation.InventLocationId = InventLocationId;
        inventLocation.Name = InventLocationName;
        inventLocation.InventSiteId = inventSiteId;
        inventLocation.insert();
 
        ttsCommit;
 
        type = cells.item(row+1, 1).value().variantType();
    }
    // Runs until the COMVarientType doesnot contains a data field.
    while (type != COMVariantType::VT_EMPTY);
 
    // Closes the Instance of Excel.
    application.quit();
}

No comments:

Post a Comment