Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Can anyone help me with a sample code?
I'm writing a program which will open word or excel file upon user's selection.
I need to read column by colum and write it to a structure,
do some parsing and calculation in the structure, and then write the whole
structure back to excel.

Thanks
Posted
Comments
[no name] 3-Aug-12 17:59pm    
And you think that someone here is going to write this code for you?

You could use a .csv file - which is comma seperated values.
Each cell is seperated with a comma, and each row is ended with a CR ( endl).
Here is a simple example:
C++
#include <iostream>
#include <fstream>


using namespace std;


int main(int args, char * argv[])
{
ofstream MyExcelFile;
MyExcelFile.open("C:\\test.csv");

MyExcelFile << "First Name, Last Name, Middle Initial" << endl;
MyExcelFile << "Michael, Jackson, B." << endl;
MyExcelFile.close();
return 0;
}

Also you can use the excellent articles discussing in detail your question :

A brief introduction to C++ and Interfacing with Excel
http://www.maths.manchester.ac.uk/~ahazel/EXCEL_C++.pdf[^]

How To Use MFC to Automate Excel and Navigate Worksheets
http://support.microsoft.com/default.aspx?scid=kb;en-us;178782[^]

A Very Easy to Use Excel XML Import-Export Library
A Very Easy to Use Excel XML Import-Export Library[^]
CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet
CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet[^]

Microsoft Excel Automation Class
http://www.codeguru.com/cpp/data/mfc_database/microsoftexcel/article.php/c11745/Microsoft-Excel-Automation-Class.htm[^]
 
Share this answer
 
Comments
pasztorpisti 4-Aug-12 8:25am    
+5 xml is the way to go
Would add this link too: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats
Excel 2003 xml format is very easy to reverse engineer even without documents, a lot of the tags from the saved xml can be ignored.
Member 9339267 6-Aug-12 19:25pm    
thanks
Volynsky Alex 8-Aug-12 18:51pm    
You are welcome
The codeproject site is actually amazing tool, just search and you will find something interesting, like this:
Accessing Excel Spreadsheets via C++[^]
 
Share this answer
 
Hey I work with similar programs I read excel files, in a .csv (comma seperated values)format and this a piece of code:

C++
Function1()
  {
//This function opens the input file,That is the file which has to be read.   
  CString m_fname;
    CFileDialog dlg(true, NULL, _T("*.csv"), NULL,  false, NULL);
    dlg.m_ofn.lpstrTitle      = _T("Select data file");
    dlg.m_ofn.lpstrFilter     = _T("Npd Files  (*.npd)\0*.npd\0CSVFiles(*.csv)\0*.csv\0All Files\0*.*\0\0");      
  
    dlg.m_ofn.lpstrInitialDir = m_fname; /
    
    if (dlg.DoModal() == IDOK)
    {
      m_fname = dlg.GetPathName();
      function2(m_fname);
    }

}
___________________________________________________________________________
//Open the output file where you want to save your processed values
Function1()
  {
  CStdioFile csf;
  CStdioFile outFile;
  CString    buffer;
  CString outDir;

  CEvoFileDlg flDlg(EVODLG_SAVE, outDir, _T("*.txt"));
  flDlg.SetTitle(_T("Select Report File"));
  flDlg.SetFilter(_T("Text File (*.txt)|*.txt|All Files (*.*)|*.*||"));

  if (flDlg.ShowModal() != IDOK)
    return;

  // Open output file
  CStdioFile out;

  if (!out.Open(dlg.GetPathName(), CFile::typeText | CFile::modeCreate |    CFile::modeWrite | CFile::shareDenyWrite))
  {
    Evo::Alarm(23, ALM_NOTIFY, _T("Unable to open file: %s"), m_outDir);
    return;
  }
  double val1, val2;

  if (csf.Open(fname, CFile::modeRead))
  {
    while (csf.ReadString(buffer) )
    {   
      //read the file
      out = swscanf_s(buffer,_T("%f,%f" ), val1, val2);
      //Your calculations here
      //now write into the chosen file.
      out.WriteString(your function that returns a string of values)
    }
 
    csf.close();
  }
}



Hope this helps
 
Share this answer
 
Comments
Member 13689404 22-Feb-18 18:28pm    
The format of CSV and .xls is different.I am searching for xls.Could you plz let me know xls reading

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900