Solved

Automation to Excel in C++ using Direct-To-COM

Posted on 2000-03-06
6
379 Views
Last Modified: 2008-02-20
I need to export some tabular data to Excel.8.
So, I am writing :

#import <D:\Program Files\Microsoft Office\Office\EXCEL8.OLB>
.....
boolean MyClass::menuExportTableToExcel()
{

    using namespace Excel;
   _ApplicationPtr pXL;
    try
      {
            pXL.CreateInstance(L"Excel.Application.8");
            pXL->Visible = VARIANT_TRUE;
   
            WorkbooksPtr pBooks = pXL->Workbooks;
               _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);

            _WorksheetPtr pSheet = pXL->ActiveSheet;
            pSheet->Name = "Selling Sheet";

            for (int col_i = firstCol; col_i < listt->count(); col_i++)
            {
             
(*)              pSheet->Cells(1,col_i-firstCol+1)->Value = //something;

                 }
            ..........................
}

Compiler issues 4 errors on line (*).
      - term does not evaluate to a function
      - left of '->Value' must point to class/struct/union

I saw in *.tli, tlh files but unfortunatly it did not help
me.

In 2 words, I need in loop put data in EXCEL through _WorkbookPtr. I need something
like that in VB :

For rwIndex = 1 to 4
    For colIndex = 1 to 10
        With Worksheets("Sheet1").Cells(rwIndex, colIndex)
             .Value = "shalom"
        End With
    Next colIndex
Next rwIndex

 

0
Comment
Question by:arikka
  • 3
  • 2
6 Comments
 

Expert Comment

by:Cyberman
ID: 2589557
Without getting into too much detail, it's nothing to do with COM.

pSheet->Cells(1,col_i-firstCol+1)->Value = //something

It seems to think that you're calling
a function Cells()

Cells(1, col_i-firstCol+1)

you probably want square brackets (I'm assuming that Cells is actually a multidimensional array)

Cells[1, col_1-firstCol+1]

RE: - left of '->Value' must point to class/struct/union

This is pointing to the fact that you aren't getting what you want before ->Value

If the idea is to get something back from a function (e.g. Cells() actually returns a cell) you'll have to do

Foo* pCell = Cells(1,col_1-firstCol+1);

and then pSheet->pCell->Value

or whatever.




0
 
LVL 2

Author Comment

by:arikka
ID: 2590989
Adjusted points to 100
0
 
LVL 2

Author Comment

by:arikka
ID: 2590990
I've tried it before, it did not help...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:arikka
ID: 2591304
Please do not ask me, I already found the soultion !!!


    Excel::RangePtr pRange;
      CString csRange;
      for (int i = 0; i < nLines; i++) // send each line to Excel
        { // one value at at time (till speed becomes an issue
        for (int j = 0; j < sizeof(cBreaks) / sizeof(cBreaks[0]);j++)
          {
          csRange.Format("%c%d", 'A'+j, i+1); // "A1, A2... B1, B2," etc
          pRange = pSheet->Range[(LPCTSTR)csRange];
          int idxChar = i*nLineLength+cBreaks[j]+1; // idx 1 past prev. NUL
          if (idxChar > nLength) // avoid overruns
            break;
          _variant_t tVar(&pC[idxChar]); // point to NUL-terminated string
          pRange->PutValue(&tVar);
0
 
LVL 5

Expert Comment

by:ianB
ID: 2676280
Community Support has reduced points from 100 to 0
0
 
LVL 5

Accepted Solution

by:
ianB earned 0 total points
ID: 2676281
Closing question and keeping answer.

Ian
Community Support @ Experts Exchange
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

761 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