Excel file to CSV file conversion using X++ in Ax 2012

static void ExcelToCSVFile(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell cell;
str filename1 = @”E:\Johnkrish\AnswerBook”;
Filename filename;
COMVariantType type;

int row=0;
CustAccount custAccount;
Name name;
DynamicField dynamicField;

int i;
;

application = SysExcelApplication::construct();

workbooks = application.workbooks();

filename =@”E:\Johnkrish\AnswerBook.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++;

cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());
cells.item(row,1).value(cells.item(row, 1).value().bStr());

type = cells.item(row+1, 1).value().variantType();

}
while (type != COMVariantType::VT_EMPTY);
workbook.saveAs(filename1,6);//6 just CSV excel file format
application.quit();

info(“All Converted as CSV file !”);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s