//+------------------------------------------------------------------+
//|                                                        Excel.mqh |
//+------------------------------------------------------------------+
#property link      "https://nonrandomwalking.wordpress.com/"
#property version   "1.00"
#property strict



/// Uses code developed by Nondisclosure007, wraps it in a class and adds portability between MQL4 and MQL5
/// CExcel constructor opens an existing Excel file for Read/Write
///
/// Acknowledgments
/// ---------------
/// Nondisclosure007: 
///     Original MetatraderToExcel dll for MQL4 -- https://www.forexfactory.com/showthread.php?p=9995615#post9995615 
/// Vasiliy Sokolov: 
///     Article on how to develop dlls for MQL5 -- https://www.mql5.com/en/articles/5563
///
/// \TODO It would be desirable to develop Save() and Close() methods and to include these in the class destructor




////////////////////////////////////////////////////////////////////////////////////////
/// CExcel declaration
////////////////////////////////////////////////////////////////////////////////////////
class CExcel
  {
public:
                     CExcel(const string strFileName,const string strLogFileName);
   void              PutDouble(double dbl,string strSheet,string strCell);
   void              PutDouble(double dbl,string strSheet,int nRowIndex,int nColumnIndex);
   void              PutDouble(double dbl,string strSheet,int nRowIndex,string strColumn);
   void              PutInt(int Integer,string strSheet,string strCell);
   void              PutInt(int Integer,string strSheet,int nRowIndex,int nColumnIndex);
   void              PutInt(int Integer,string strSheet,int nRowIndex,string strColumn);
   void              PutStr(string str,string strSheet,string strCell);
   void              PutStr(string str,string strSheet,int nRowIndex,int nColumnIndex);
   void              PutStr(string str,string strSheet,int nRowIndex,string strColumn);
   double            GetDouble(string strSheet,string strCell);
   double            GetDouble(string strSheet,int nRowIndex,int nColumnIndex);
   double            GetDouble(string strSheet,int nRowIndex,string strColumn);
   int               GetInt(string strSheet,string strCell);
   int               GetInt(string strSheet,int nRowIndex,int nColumnIndex);
   int               GetInt(string strSheet,int nRowIndex,string strColumn);
   string            GetStr(string strSheet,string strCell);
   string            GetStr(string strSheet,int nRowIndex,int nColumnIndex);
   string            GetStr(string strSheet,int nRowIndex,string strColumn);
  };
  
  
//+------------------------------------------------------------------+
/// Metatrader 4 code.  Requires MetatraderToExcel.dll developed by Nondisclosure007 in MQL4/Libraries folder.
//+------------------------------------------------------------------+
#ifdef __MQL4__
#import "MetatraderToExcel.dll"
void Initialize(string wkb,string logfile);
void PutDouble_Cell(double dbl,string shts,string cell);
void PutDouble_intidx(double dbl,string shts,int rowindex,int columnindex);
void PutDouble_intCell(double dbl,string shts,int rowindex,string column);
void PutInt_Cell(int Integer,string shts,string cell);
void PutInt_intidx(int Integer,string shts,int rowindex,int columnindex);
void PutInt_intCell(int Integer,string shts,int rowindex,string column);
void PutStr_Cell(string str,string shts,string cell);
void PutStr_intidx(string str,string shts,int rowindex,int columnindex);
void PutStr_intCell(string str,string shts,int rowindex,string column);
double GetDouble_Cell(string shts,string cell);
double GetDouble_intidx(string shts,int rowindex,int columnindex);
double GetDouble_intCell(string shts,int rowindex,string column);
int GetInt_Cell(string shts,string cell);
int GetInt_intidx(string shts,int rowindex,int columnindex);
int GetInt_intCell(string shts,int rowindex,string column);
string GetStr_Cell(string shts,string cell);
string GetStr_intidx(string shts,int rowindex,int columnindex);
string GetStr_intCell(string shts,int rowindex,string column);
#import
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CExcel::CExcel(const string strFileName,const string strLogFileName)
  {
   Initialize(strFileName,strLogFileName);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,string strCell)
  {
   PutDouble_Cell(dbl,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,int nRowIndex,int nColumnIndex)
  {
   PutDouble_intidx(dbl,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,int nRowIndex,string strColumn)
  {
   PutDouble_intCell(dbl,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,string strCell)
  {
   PutInt_Cell(Integer,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,int nRowIndex,int nColumnIndex)
  {
   PutInt_intidx(Integer,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,int nRowIndex,string strColumn)
  {
   PutInt_intCell(Integer,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,string strCell)
  {
   PutStr_Cell(str,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,int nRowIndex,int nColumnIndex)
  {
   PutStr_intidx(str,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,int nRowIndex,string strColumn)
  {
   PutStr_intCell(str,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CExcel::GetDouble(string strSheet,string strCell)
  {
   return(GetDouble_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CExcel::GetDouble(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(GetDouble_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CExcel::GetDouble(string strSheet,int nRowIndex,string strColumn)
  {
   return(GetDouble_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CExcel::GetInt(string strSheet,string strCell)
  {
   return(GetInt_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CExcel::GetInt(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(GetInt_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CExcel::GetInt(string strSheet,int nRowIndex,string strColumn)
  {
   return(GetInt_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string CExcel::GetStr(string strSheet,string strCell)
  {
   return(GetStr_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string CExcel::GetStr(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(GetStr_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string CExcel::GetStr(string strSheet,int nRowIndex,string strColumn)
  {
   return(GetStr_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
#endif // __MQL4__



//+------------------------------------------------------------------+
/// Metatrader 5 code.  Requires Metatrader5ToExcel.dll. See https://www.forexfactory.com/showthread.php?p=9995615#post9995615
//+------------------------------------------------------------------+
#ifdef __MQL5__
#import "Metatrader5ToExcel.dll"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CExcel::CExcel(const string strFileName,const string strLogFileName)
  {
   Metatrader5ToExcel::Initialize(strFileName,strLogFileName);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,string strCell)
  {
   Metatrader5ToExcel::PutDouble_Cell(dbl,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,int nRowIndex,int nColumnIndex)
  {
   Metatrader5ToExcel::PutDouble_intidx(dbl,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutDouble(double dbl,string strSheet,int nRowIndex,string strColumn)
  {
   Metatrader5ToExcel::PutDouble_intCell(dbl,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,string strCell)
  {
   Metatrader5ToExcel::PutInt_Cell(Integer,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,int nRowIndex,int nColumnIndex)
  {
   Metatrader5ToExcel::PutInt_intidx(Integer,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutInt(int Integer,string strSheet,int nRowIndex,string strColumn)
  {
   Metatrader5ToExcel::PutInt_intCell(Integer,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,string strCell)
  {
   Metatrader5ToExcel::PutStr_Cell(str,strSheet,strCell);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,int nRowIndex,int nColumnIndex)
  {
   Metatrader5ToExcel::PutStr_intidx(str,strSheet,nRowIndex,nColumnIndex);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CExcel::PutStr(string str,string strSheet,int nRowIndex,string strColumn)
  {
   Metatrader5ToExcel::PutStr_intCell(str,strSheet,nRowIndex,strColumn);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetDouble(string strSheet,string strCell)
  {
   return(Metatrader5ToExcel::GetDouble_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetDouble(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(Metatrader5ToExcel::GetDouble_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetDouble(string strSheet,int nRowIndex,string strColumn)
  {
   return(Metatrader5ToExcel::GetDouble_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetInt(string strSheet,string strCell)
  {
   return(Metatrader5ToExcel::GetInt_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetInt(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(Metatrader5ToExcel::GetInt_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetInt(string strSheet,int nRowIndex,string strColumn)
  {
   return(Metatrader5ToExcel::GetInt_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string GetStr(string strSheet,string strCell)
  {
   return(Metatrader5ToExcel::GetStr_Cell(strSheet, strCell));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string GetStr(string strSheet,int nRowIndex,int nColumnIndex)
  {
   return(Metatrader5ToExcel::GetStr_intidx(strSheet, nRowIndex, nColumnIndex));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string GetStr(string strSheet,int nRowIndex,string strColumn)
  {
   return(Metatrader5ToExcel::GetStr_intCell(strSheet, nRowIndex, strColumn));
  }
//+------------------------------------------------------------------+
#endif // __MQL5__