Author Topic: Exporting data to Excel by Code x++  (Read 2717 times)

0 Members and 1 Guest are viewing this topic.

smahdy

  • Guest
Exporting data to Excel by Code x++
« on: March 24, 2014, 12:22:51 AM »
Below is sample code how to export data to Excel File by code x++
static void DASCreateExcelDocument(Args _args)
{
  SynonecelApplication  xlsApplication;
  SynonecelWorkBooks    xlsWorkBookCollection;
  SynonecelWorkBook    xlsWorkBook;
  SynonecelWorkSheets  xlsWorkSheetCollection;
  SynonecelWorkSheet    xlsWorkSheet;
  SynonecelRange        xlasRange;
  CustTable            custTable;
  int                  row = 1;
  str                  fileName;
  ;
  //Filename
  fileName = "C:\Test.xlsx";
  //Initialize Excel instance
  xlsApplication          = SynonecelApplication::construct();
  //Open Excel document
  //xlsApplication.visible(true);
  //Create Excel WorkBook and WorkSheet
  xlsWorkBookCollection    = xlsApplication.workbooks();
  xlsWorkBook              = xlsWorkBookCollection.add();
  xlsWorkSheetCollection  = xlsWorkBook.worksheets();
  xlsWorkSheet            = xlsWorkSheetCollection.itemFromNum(1);
  //Excel columns captions
  xlsWorkSheet.cells().item(row,1).value("Account Num");
  xlsWorkSheet.cells().item(row,2).value("Name");
  row++;
  //Fill Excel with CustTable AccountNum and Name fields (only 10 records)
  while select custTable
  {
      if(row == 10)
        break;
      xlsWorkSheet.cells().item(row,1).value(custTable.AccountNum);
      xlsWorkSheet.cells().item(row,2).value(custTable.Name);
      row++;
  }
  //Check whether the document already exists
  if(WinApi::fileExists(fileName))
      WinApi::deleteFile(fileName);
  //Save Excel document
  xlsWorkbook.saveAs(fileName);
  //Open Excel document
  xlsApplication.visible(true);
  //Close Excel
  //xlsApplication.quit();
  //xlsApplication.finalize();
}