Solved

Reading/Writing from/to Excel from Visual C++.NET 2005

Posted on 2006-12-01
7
4,559 Views
Last Modified: 2012-08-13
Hi Experts,

    I need to know how to open an existing Excel file, write data into it, format its looks and save it under another filename.  I need to do all of this from Visual C++.NET 2005.

Thanks,
axnst2
0
Comment
Question by:axnst2
7 Comments
 

Author Comment

by:axnst2
ID: 18053980
I do have MFC support..
0
 

Author Comment

by:axnst2
ID: 18054569
Is this a really hard question or is there just no one looking at this thread?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 18059789
I suggest you to ask in MFC area. This area is used mostly for questions specific to new VC++ 8.0 features like C++/CLI syntax.
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 18090073
Have a look at this: http://support.microsoft.com/kb/306686

It lets you call a macro from your application. That way you can do your writing and formatting in VBA.
0
 
LVL 3

Accepted Solution

by:
sreenathk earned 500 total points
ID: 18201651
Hi axnst2,

 I have got a solution for your probelm. Actually I am also searching for same solution.

http://www.codeproject.com/useritems/BasicExcel.asp

From above site.. download the "Download demo project - 80 Kb"
It has 4 files
Use only BasicExcelVC6.cpp and BasicExcelVC6.hpp

For main function use the following code..in separate cpp file. Build them using VC6.0. Create one excel sheet by name example1.xls in your directory. It will work.

#include <BasicExcel.hpp>
using namespace YExcel;

int main(int argc, char* argv[])
{
      BasicExcel e;

      // Load a workbook with one sheet, display its contents and save into another file.
      e.Load("example1.xls");      
      BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet1");
      if (sheet1)
      {
            size_t maxRows = sheet1->GetTotalRows();
            size_t maxCols = sheet1->GetTotalCols();
            cout << "Dimension of " << sheet1->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;

            printf("          ");
            for (size_t c=0; c<maxCols; ++c) printf("%10d", c+1);
            cout << endl;

            for (size_t r=0; r<maxRows; ++r)
            {
                  printf("%10d", r+1);
                  for (size_t c=0; c<maxCols; ++c)
                  {
                        BasicExcelCell* cell = sheet1->Cell(r,c);
                        switch (cell->Type())
                        {
                              case BasicExcelCell::UNDEFINED:
                                    printf("          ");
                                    break;

                              case BasicExcelCell::INT:
                                    printf("%10d", cell->GetInteger());
                                    break;

                              case BasicExcelCell::DOUBLE:
                                    printf("%10.6lf", cell->GetDouble());
                                    break;

                              case BasicExcelCell::STRING:
                                    printf("%10s", cell->GetString());
                                    break;

                              case BasicExcelCell::WSTRING:
                                    wprintf(L"%10s", cell->GetWString());
                                    break;
                        }
                  }
                  cout << endl;
            }
      }
      cout << endl;
      e.SaveAs("example2.xls");

      // Create a new workbook with 2 worksheets and write some contents.
      e.New(2);
      e.RenameWorksheet("Sheet1", "Test1");
      BasicExcelWorksheet* sheet = e.GetWorksheet("Test1");
      BasicExcelCell* cell;
      if (sheet)
      {
            for (size_t c=0; c<4; ++c)
            {
                  cell = sheet->Cell(0,c);
                  cell->Set((int)c);
            }

            cell = sheet->Cell(1,3);
            cell->SetDouble(3.141592654);

            sheet->Cell(1,4)->SetString("Test str1");
            sheet->Cell(2,0)->SetString("Test str2");
            sheet->Cell(2,5)->SetString("Test str1");

            sheet->Cell(4,0)->SetDouble(1.1);
            sheet->Cell(4,1)->SetDouble(2.2);
            sheet->Cell(4,2)->SetDouble(3.3);
            sheet->Cell(4,3)->SetDouble(4.4);
            sheet->Cell(4,4)->SetDouble(5.5);

            sheet->Cell(4,4)->EraseContents();
      }

      sheet = e.AddWorksheet("Test2", 1);
      sheet = e.GetWorksheet(1);
      if (sheet)
      {
            sheet->Cell(1,1)->SetDouble(1.1);
            sheet->Cell(2,2)->SetDouble(2.2);
            sheet->Cell(3,3)->SetDouble(3.3);
            sheet->Cell(4,4)->SetDouble(4.4);
            sheet->Cell(70,2)->SetDouble(5.5);
      }
      e.SaveAs("example3.xls");

      // Load the newly created sheet and display its contents
      e.Load("example3.xls");

      size_t maxSheets = e.GetTotalWorkSheets();
      cout << "Total number of worksheets: " << e.GetTotalWorkSheets() << endl;
      for (size_t i=0; i<maxSheets; ++i)
      {
            BasicExcelWorksheet* sheet = e.GetWorksheet(i);
            if (sheet)
            {
                  size_t maxRows = sheet->GetTotalRows();
                  size_t maxCols = sheet->GetTotalCols();
                  cout << "Dimension of " << sheet->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;

                  if (maxRows>0)
                  {
                        printf("          ");
                        for (size_t c=0; c<maxCols; ++c) printf("%10d", c+1);
                        cout << endl;
                  }

                  for (size_t r=0; r<maxRows; ++r)
                  {
                        printf("%10d", r+1);
                        for (size_t c=0; c<maxCols; ++c)
                        {
                              cout << setw(10) << *(sheet->Cell(r,c));      // Another way of printing a cell content.                        
                        }
                        cout << endl;
                  }
                  if (i==0)
                  {
                        ofstream f("example4.csv");
                        sheet->Print(f, ',', '\"');      // Save the first sheet as a CSV file.
                        f.close();
                  }
            }
            cout << endl;
      }
      return 0;
}

Thanks,
Sreenath

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now