Thursday, 2 March 2017

Code for Import from Excel to AxTable


  1. First Create Two tables  header and line  
  2. Create Two forms as header and line 
  3. In the header form and line form  add the respective datasource  i.e header and line (in both the form)
  4. Create the xcel sheet  with you values according to the field in the table
  5. Create the job and paste the coding below  run it


static void TestImport(Args _args)
{

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;

int i;
str com;
COMVariantType type;
FileName filename;
TestImport header;
TestImportline   line;
int row;
//int UnitQty;
//int    VehicleNumber;
//str AccountNumber;
//str Name;
//real Amount
;

application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "G:\\Test.xlsx";
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}

workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1); //Here 3 is the worksheet Number
cells = worksheet.cells();
do
{
row++;
com = cells.item(row, 1).value().bStr();
if(com!="0")
{
header.AccountNum=cells.item(row, 1).value().bStr();
header.Name = cells.item(row, 2).value().bStr();
header.Amount =(cells.item(row,3).value().double());
header.insert();
    }
if(conitem!="0")
    {
        line.AccountNum=  cells.item(row, 1).value().bStr();
        line.Amount= (cells.item(row,3).value().double());
        line.UnitQty= any2int(cells.item(row, 4).value().toString()); //(cells.item(row,4).value().int());
        line.VehicleNumber=any2int(cells.item(row, 5).value().toString());
        line.insert();
    }

type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}

No comments:

Post a Comment