Using x++ code export to CSV file from dynamics AX 2009
Wrote by Jimmy on DEC 31 2010
1)Using x++ code export to CSV file from dynamics AX 2009 (TextBuffer)
remark : must be init setup file path.
static void Jimmy_ExportToCSVFile01(Args _args) { TextBuffer textBuffer = new TextBuffer(); InventTable inventTable; FileIoPermission perm; counter Lines; #define.ExampleFile(@"c:\test.csv") #define.ExampleOpenMode("W") ; try { // Set code access permission to help protect the use of perm = new FileIoPermission(#ExampleFile, #ExampleOpenMode); perm.assert(); textBuffer.appendText("Item Id,");//must be "," suffix textBuffer.appendText("Item Name,");//must be "," suffix textBuffer.appendText("Item Type"); textBuffer.appendText("\n");//next line must be "\n" suffix while select InventTable where inventTable.ItemType == ItemType::BOM && inventTable.ItemId like "10*" { textBuffer.appendText(strfmt("%1,",inventTable.ItemId)); textBuffer.appendText(strfmt("%1,",global::strReplace(inventTable.ItemName,",","")));//must be repace "," to "" textBuffer.appendText(enum2str(inventTable.ItemType)); textBuffer.appendText("\n"); } Lines = textBuffer.numLines(); try { if (textBuffer.toFile(#ExampleFile)) info( strfmt("File Generated as %1.total insert %2 Lines",#ExampleFile,Lines)); } catch ( Exception::Error ) { error ("Generated file error."); } // Close the code access permission scope. CodeAccessPermission::revertAssert(); } catch (Exception::Deadlock) { retry; } }
2)Using x++ code export to CSV file from dynamics AX 2009(TextBuffer)
remark : dialog saved as csv file path
static void Jimmy_ExportToCSVFile03(Args _args) { TextBuffer textBuffer = new TextBuffer(); CustTable CustTable; FileNameFilter Filter = ["CSV file", "*.csv"]; FileName FileName; #WinAPI ; FileName = winapi::getSaveFileName(infolog.hWnd(), filter, @"c:\...\desktop", "Save as CSV file","csv","Customer Infomation"); if(!FileName) return ; textBuffer.appendText("Customer,"); textBuffer.appendText("Group,"); textBuffer.appendText("Currency,"); textBuffer.appendText("RecId\n"); while select CustTable { textBuffer.appendText(strfmt("%1,", CustTable.AccountNum)); textBuffer.appendText(strfmt("%1,", CustTable.CustGroup)); textBuffer.appendText(strfmt("%1,", CustTable.Currency)); textBuffer.appendText(strfmt("%1", CustTable.RecId)); textBuffer.appendText("\n");//next row } if (textBuffer.toFile(FileName)) { info( strfmt("File Generated as %1.Total %2 Lines",FileName,textBuffer.numLines())); winapi::shellExecute(FileName); } }
3)Using x++ code export to CSV file from dynamics AX 2009(SysExcelApplication)
remark : pop up csv file saved as dialog
static void Jimmy_ExportToCSVFile02(Args _args) { SysExcelApplication application; SysExcelWorkBooks workBooks; SysExcelWorkBook workBook; SysExcelWorkSheet workSheet; SysExcelCells cell; str filename = @"D:\test"; ; application = SysExcelApplication::construct(); workBooks = application.workbooks(); workBook = workBooks.add(); workSheet = workBook.worksheets().itemFromNum(1); cell = worksheet.cells(); cell.item(1,1).value("itemid"); cell.item(1,2).value("ItemName"); workbook.saveAs(filename,6);//6 just CSV excel file format application.quit(); }