AX export the 'CSV' file by three ways
Way 1: Use 'TextBuffer' to Export CSV from AX.
1 static void ExportToCSVFile01(Args _args) 2 { 3 TextBuffer textBuffer = new TextBuffer(); 4 InventTable inventTable; 5 FileIoPermission perm; 6 counter Lines; 7 8 #define.ExampleFile(@"c:\\AX2009.csv") 9 #define.ExampleOpenMode("W") 10 ; 11 try 12 { 13 perm = new FileIoPermission(#ExampleFile, #ExampleOpenMode); 14 perm.assert(); 15 16 17 textBuffer.appendText("Item Id,");//必须用逗号分开 18 textBuffer.appendText("Item Name,");//必须用逗号分开 19 textBuffer.appendText("Item Type"); 20 textBuffer.appendText("\n");//下一行必须以回车分开 21 22 while select InventTable 23 where inventTable.ItemType == ItemType::BOM 24 && inventTable.ItemId like "20*" 25 { 26 textBuffer.appendText(strfmt("%1,",inventTable.ItemId)); 27 28 textBuffer.appendText(strfmt("%1,",global::strReplace(inventTable.ItemName,",",""))); 29 30 textBuffer.appendText(enum2str(inventTable.ItemType)); 31 textBuffer.appendText("\n"); 32 } 33 Lines = textBuffer.numLines(); 34 35 try 36 { 37 if (textBuffer.toFile(#ExampleFile)) 38 info( strfmt("File Generated as %1.total insert %2 Lines",#ExampleFile,Lines)); 39 } 40 catch ( Exception::Error ) 41 { 42 error ("Generated file error."); 43 } 44 CodeAccessPermission::revertAssert(); 45 } 46 catch (Exception::Deadlock) 47 { 48 retry; 49 } 50 }
Way 2: Use dialog to get the saving path and save the file.
static void 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); } }
Way 3: Use the system class 'SysExcelApplication' to export CSV file, lowest performance.
1 static void ExportToCSVFile02(Args _args) 2 { 3 SysExcelApplication application; 4 SysExcelWorkBooks workBooks; 5 SysExcelWorkBook workBook; 6 SysExcelWorkSheet workSheet; 7 SysExcelCells cell; 8 str filename = @"c:\\test"; 9 10 ; 11 application = SysExcelApplication::construct(); 12 workBooks = application.workbooks(); 13 workBook = workBooks.add(); 14 workSheet = workBook.worksheets().itemFromNum(1); 15 cell = worksheet.cells(); 16 cell.item(1,1).value("itemid"); 17 cell.item(1,2).value("ItemName"); 18 19 workbook.saveAs(filename,6);//6 just CSV excel file format 20 application.quit(); 21 22 }