Excel file operations functions cpp

Excel file operations functions cpp

#include <fstream>
#include <string>

class CExcelFile
{
public:
CExcelFile(void);
~CExcelFile(void);
private:
bool m_bIsHeaderPresent;
std::vector<std::vector<CString>> m_vtCSVData;//Row vector containing vector of Columns

public:
int ReadCSVFile(CString csFilePath);
void SetIsHeaderPresent(bool bPresent);
CString GetNode(int Row, int Col);
bool UploadCSVFile(CString csfilePath);

int MoveCSVFile(CString csCurrentfilePath,CString csNewfilePath);
int DeleteFile(CString csFilePath);
};

CExcelFile::CExcelFile(void)
{
m_bIsHeaderPresent = false;
}

CExcelFile::~CExcelFile(void)
{
m_bIsHeaderPresent = false;
}

//Function to Read CSV File and store all the data(col1,col2;col1,col2…) 
int CExcelFile::ReadCSVFile(CString csFilePath)
{
int iret = -1;
std::string line;
std::ifstream myfile (csFilePath);
std::vector<CString> vtCol;//vector containing all Column data of a single row
if (myfile.is_open())
{
while (getline(myfile,line))
{
CString csTempHeaderText = (CString)line.c_str();
CString Seperator = “,”;
int Position = 0;
CString Token=””;
if(!csTempHeaderText.IsEmpty())
{
vtCol.clear();
Token = csTempHeaderText.Tokenize(Seperator, Position);
Token.Trim();

CString csTemp = “”;
//Get comma seperated values
while(!Token.IsEmpty())
{
//remove inverted commas
Token.Trim(‘”‘);

if(!csTemp.IsEmpty())
{
csTemp+=”;”;
}
vtCol.push_back(Token);
csTemp = csTemp + Token ;

// Get next column data.
Token = csTempHeaderText.Tokenize(Seperator, Position);
Token.Trim();
}
if((vtCol.size() != 2) &&  (vtCol.size() != 0)) //number of col=2
{
myfile.close();
iret = 3; //COLUMN_DATA_INCORRECT
return iret;
}
if(vtCol.size() != 0)
{
vtCol.push_back(“”);
m_vtCSVData.push_back(vtCol);//Vector Storing vector of Col Data
}
}
}
myfile.close();
iret = 1;//SUCCESS
}
else
{
iret = 2;//UNABLE_TO_OPEN_FILE;
}

return iret;
}

void CExcelFile::SetIsHeaderPresent(bool bPresent)
{
m_bIsHeaderPresent = bPresent;
}

CString CExcelFile::GetNode(int Row, int Col)
{
CString csData = “”;
for(int iRow =0; iRow < m_vtCSVData.size(); iRow++)
{
for(int iCol =0; iCol < m_vtCSVData[iRow].size(); iCol++)
{
if(true == m_bIsHeaderPresent)
{
if((iRow == Row+1) && (iCol == Col))
{
csData = m_vtCSVData[iRow][iCol];
break;
}
}
else
{
if((iRow == Row) && (iCol == Col))
{
csData = m_vtCSVData[iRow][iCol];
break;
}
}
}
}

return csData;
}

//Function to upload Data into CSV File
bool CExcelFile::UploadCSVFile(CString csfilePath)
{
errno_t err;
FILE *f;
err = fopen_s(&f,csfilePath, “w”);
if (f == NULL)
{
return false;
}
int iRow=0,iCol =0;
for (iRow = 0; iRow < m_vtCSVData.size(); ++iRow)
{
// for each column
for (iCol = 0; iCol < m_vtCSVData[iRow].size()-1; iCol++)
{
fprintf(f,”%s,”,m_vtCSVData[iRow][iCol]);
}
if(iCol == m_vtCSVData[iRow].size()-1)
{
fprintf(f,”%s”,m_vtCSVData[iRow][iCol]);
}
fprintf(f,”\n”);
}
fclose(f);
return true;
}

//Function to Delete a File
int CExcelFile::DeleteFile(CString csFilePath)
{
int iret = -1;
if(::PathFileExists(csFilePath) == false)
{
//file doesn’t exists
iret = FILE_NOT_PRESENT;
return iret;
}
if( ::remove(csFilePath) == 0 )
{
//printf( “File successfully moved. Thank you for using this mini app” ) ;
iret = SUCCESS;
return iret;
}
else
{
DWORD ld = ::GetLastError();
//printf( “An error occured while moving the file!!!” ) ;
iret = FAILED_TO_REMOVE_FILE;
return iret;
}
}
//Function to copy file1 into file2 and then delete the file1
int CExcelFile::MoveCSVFile(CString csCurrentfilePath,CString csNewfilePath)
{
//size_t len = 0 ;
//char buffer[BUFSIZ] = { ‘\0’ } ;
errno_t err;
int iret = -1;
FILE* in ;
err = fopen_s(&in, csCurrentfilePath, “rb” ) ;
FILE* out;
err = fopen_s(&out, csNewfilePath, “wb” ) ;
if( in == NULL )
{

//perror( “An error occured while opening files!!!” ) ;
in = out = 0 ;
iret = UNABLE_TO_OPEN_FILE;
return iret;
}
else if( out == NULL )
{

//perror( “An error occured while opening files!!!” ) ;
in = out = 0 ;
iret = UNABLE_TO_OPEN_FILE;
return iret;
}
else // add this else clause
{
if(CopyFile(csCurrentfilePath,csNewfilePath,FALSE) > 0)
{
//Success
fclose(in) ;
fclose(out) ;
//return true;
}
else
{
//Failure
fclose(in) ;
fclose(out) ;
iret = FAILED_TO_COPY_FILE;

return iret;
}

if( ::remove(csCurrentfilePath) == 0 )
{
//printf( “File successfully moved. Thank you for using this mini app” ) ;
iret = SUCCESS;
return iret;
}
else
{
DWORD ld = ::GetLastError();
//printf( “An error occured while moving the file!!!” ) ;
iret = FAILED_TO_REMOVE_FILE;

return iret;
}
}
//iret = SUCCESS;
//return iret;
}

 

 

C++ Webkit C Qt C# concepts