Hi Piyush,
Please check this code and let me know. What I was did wrongly?
Static void ImportFromExcel
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
System.DateTime ShlefDate;
FilenameOpen filename;
dialogField dialogFilename;
Dialog dialog;
//Table Declarations Starts
ERB_InvoiceStagingTestTable erb_InvoiceStagingTestTable;
//Table Declarations Ends
int row = 1;
str Customer,SINO, NumberSequenceGroup, Currency, InvoiceNo, ItemSalestaxgroup, GSTNumber,SASCode, Sales1, IGST, SGST, CGST, InvAmt, Sales;
date InvDate, WorkMonth;
#Excel
// convert into str from excel cell value
str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
{
switch (_cv.variantType())
{
case (COMVariantType::VT_BSTR):
return _cv.bStr();
case (COMVariantType::VT_R4):
return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_R8):
return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DECIMAL):
return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DATE):
return date2str(_cv.date(),123,2,1,2,1,4);
case (COMVariantType::VT_EMPTY):
return "";
default:
throw error(strfmt("@SYS26908", _cv.variantType()));
}
return "";
}
;
dialog = new Dialog("Excel Upoad");
dialogFilename = dialog.addField(typeId(FilenameOpen));
dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS28576",#XLS]);
dialog.filenameLookupTitle("Upload from Excel");
dialog.caption("Excel Upload");
dialogFilename.value(filename);
if(!dialog.run())
return;
filename = dialogFilename.value();
application = SysExcelApplication::construct();
workbooks = application.workbooks();
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
try
{
ttsbegin;
do
{
row++;
SINO = COMVariant2Str(cells.item(row, 1).value());
Customer = COMVariant2Str(cells.item(row,2).value());
NumberSequenceGroup = cells.item(row, 3).value().bStr();
InvDate = cells.item(row, 4).value().date();
Currency = cells.item(row, 5).value().bStr();
InvoiceNo = cells.item(row, 6).value().bStr();
WorkMonth = cells.item(row, 7).value().date();
Sales = COMVariant2Str(cells.item(row, 8).value());
IGST = COMVariant2Str(cells.item(row, 9).value());
SGST = COMVariant2Str(cells.item(row, 10).value());
CGST = COMVariant2Str(cells.item(row, 11).value());
InvAmt = COMVariant2Str(cells.item(row, 12).value());
ItemSalestaxgroup = cells.item(row, 13).value().bStr();
SASCode = COMVariant2Str(cells.item(row, 14).value());
GSTNumber = cells.item(row, 15).value().bStr();
if(row > 1)
{
//Insert into InventSize Table
select firstOnly erb_InvoiceStagingTestTable where erb_InvoiceStagingTestTable.InvoiceNo == InvoiceNo;
if(erb_InvoiceStagingTestTable)
{
erb_InvoiceStagingTestTable.InvoiceNo = InvoiceNo;
erb_InvoiceStagingTable.SINO = SINO;
erb_InvoiceStagingTable.Customer = Customer;
erb_InvoiceStagingTable.NumberSequenceGroup = NumberSequenceGroup;
erb_InvoiceStagingTable.InvoiceDate = InvDate;
erb_InvoiceStagingTable.Currency = Currency;
erb_InvoiceStagingTable.InvoiceNo = InvoiceNo;
erb_InvoiceStagingTable.WorkMonth = WorkMonth;
erb_InvoiceStagingTable.Sales = any2real(Sales);
erb_InvoiceStagingTable.IGST = any2real(IGST);
erb_InvoiceStagingTable.SGST = any2real(SGST);
erb_InvoiceStagingTable.CGST = any2real(CGST);
erb_InvoiceStagingTable.InvoiceAmount = any2real(InvAmt);
erb_InvoiceStagingTable.ItemSalesTaxGroup = ItemSalestaxgroup;
erb_InvoiceStagingTable.SASCode = SASCode;
erb_InvoiceStagingTable.GSTNumber = GSTNumber;
erb_InvoiceStagingTable.insert();
info(strfmt(“Item(%1) uploaded successfully”,InvoiceNo));
}
type = cells.item(row+1, 1).value().variantType();
}while (type != COMVariantType::VT_EMPTY);
application.quit();
ttscommit;
}
catch
{
Error("Upload Failed");
}
}
Best Regards,
Vignesh