mercredi 26 janvier 2011

How to export Data from a table to an Excel file from X++

Use this code :)

static void ExcelFile(Args _args)
{
    int                 i;
    InventTrans         Inventtrans;
    SysExcelApplication excelApp;
    SysExcelWorkbooks   books;
    SysExcelWorkbook    book;
    SysExcelWorksheets  sheets;
    SysExcelWorksheet   sheet;
    SysExcelCells       cells;
    SysExcelCell        cell;
    ;
    i=2;
    excelApp = SysExcelApplication::construct();
    books    = excelApp.workbooks();
    book     = books.add();
    sheets   = excelApp.worksheets();
    sheet    = sheets.itemFromNum(1);
    sheet.name("XMC");
    cells    = sheet.cells();
    cell     = cells.item(1,1);
    cell.value("ItemId");
    cell     = cells.item(1,2);
    cell.value("StatusIssue");
    cell     = cells.item(1,3);
    cell.value("TransType");
    cell     = cells.item(1,4);
    cell.value("CostAmountPosted");
    cell     = cells.item(1,5);
    cell.value("Qty");
    while select ItemId, StatusIssue, TransType, CostAmountPosted, Qty from Inventtrans 
    {
      cells    = sheet.cells();
      cell     = cells.item(i,1);
      cell.value(Inventtrans.ItemId);
      cell     = cells.item(i,2);
      cell.value(Inventtrans.StatusIssue);
      cell     = cells.item(i,3);
      cell.value(Inventtrans.TransType);
      cell     = cells.item(i,4);
      cell.value(Inventtrans.CostAmountPosted);
      cell     = cells.item(i,5);
      cell.value(Inventtrans.Qty);
      i++; 
    }
    excelApp.visible(true);
    book.saveAs("C:\\xmc_ExelFile");

}

Happy Daxing!

Aucun commentaire:

Enregistrer un commentaire