Translate

Thursday, July 28, 2011

Reading Data from Excel sheet(.xls)

//Read Data from Excelsheet to AX.

static void ReadFromExcelFile(Args _args)
{
SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
SysExcelCells cells;
COMVariantType type;
int row;
ItemId itemid;
Name name;
FileName filename;
;
excel = SysExcelApplication::construct();
books = excel.workbooks();
//specify the file path that you want to read
filename = "U:\\item.xls";
try
{
books.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
book = books.item(1);
sheets = book.worksheets();
sheet = sheets.itemFromNum(1);
cells = sheet.cells();
do
{
row++;
itemId = cells.item(row, 1).value().bStr();
name = cells.item(row, 2).value().bStr();
info(strfmt('%1 - %2', itemId, name));
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
excel.quit();
}

Good Luck :)

No comments:

Post a Comment