Recently i created a job to import sites from excel file, here is the code.
static void InventSiteImportFromExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FileName filename;
int row =0 ;
str ProductName;
str _ ProductCode;
InventSite inventSite;
InventSiteId inventSiteId;
InventSiteName inventSiteName;
ReqSitePolicy reqSitePolicy;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "C:\\sample path \\test.xlsx"; // add file path here.
try
{
// Adds the file as the first document in the collection.
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1); // represents the current worksheet in my case it's (1)
cells = worksheet.cells();
// Fetches data from each cell that contains data.
do
{
row++;
inventSiteId = cells.item(row, 1).value().bStr();
inventSiteName = cells.item(row, 2).value().bStr();
ttsBegin;
inventSite.SiteId = inventSiteId;
inventSite.Name = inventSiteName;
inventSite.insert();
ttsCommit;
type = cells.item(row+1, 1).value().variantType();
}
// Runs until the COMVarientType doesnot contains a data field.
while (type != COMVariantType::VT_EMPTY);
// Closes the Instance of Excel.
application.quit();
//site will not be visible on WHM->Setup->WH setup-> sites
// unless you add recoelds in ReqSitePolicy table
insert_recordset reqSitePolicy (InventSiteId)
select SiteId from InventSite;
}
Cheers!
static void InventSiteImportFromExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FileName filename;
int row =0 ;
str ProductName;
str _ ProductCode;
InventSite inventSite;
InventSiteId inventSiteId;
InventSiteName inventSiteName;
ReqSitePolicy reqSitePolicy;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "C:\\sample path \\test.xlsx"; // add file path here.
try
{
// Adds the file as the first document in the collection.
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1); // represents the current worksheet in my case it's (1)
cells = worksheet.cells();
// Fetches data from each cell that contains data.
do
{
row++;
inventSiteId = cells.item(row, 1).value().bStr();
inventSiteName = cells.item(row, 2).value().bStr();
ttsBegin;
inventSite.SiteId = inventSiteId;
inventSite.Name = inventSiteName;
inventSite.insert();
ttsCommit;
type = cells.item(row+1, 1).value().variantType();
}
// Runs until the COMVarientType doesnot contains a data field.
while (type != COMVariantType::VT_EMPTY);
// Closes the Instance of Excel.
application.quit();
//site will not be visible on WHM->Setup->WH setup-> sites
// unless you add recoelds in ReqSitePolicy table
insert_recordset reqSitePolicy (InventSiteId)
select SiteId from InventSite;
}
Cheers!
No comments:
Post a Comment