save output file to excel format

This is a discussion on save output file to excel format within the C++ Programming forums, part of the General Programming Boards category; Hi im a new learner on c++ and i need help on how to save the output file in Excel ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    save output file to excel format

    Hi im a new learner on c++ and i need help on how to save the output file in Excel format? any examples can be shown?

    All helps are highly appreciated. thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Saving files in actual .xls format is very hard work. It is a proprietary binary format owned by Microsoft. You can download the specification of the file format, but it will none the less involve a lot of work.

    On the other hand, excel is automatically bound to .csv as well, which is exceedingly easy for programs to create. It's just a text file
    Code:
    ofstream of("myfile.csv");
    of << "1,2,3" << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    is there any examples u know? so i can look at it

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Search:

    Type: Forums, Posts, Groups, Visitor Messages; Keyword(s): excel, format
    http://cboard.cprogramming.com/searc...earchid=259219
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    First thing, can you get away with just outputting csv?
    If not, explain why not so that we can confirm your reasoning.

    Then if not, be prepared for a lot of hard work. It can be done through the Office COM components but you are not going to get very far quickly there.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,499
    Quote Originally Posted by iMalc View Post
    you are not going to get very far quickly there.
    and not cheaply either. it's likely that you'll owe microsoft royalties if you distribute your program.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    owh yea i have tried the .csv file and it works. thanks..... i have another problem which as shown below

    cout << "Please enter the name of the file to write to.\n";
    cout << "File name: ";
    cin >> FileName;
    cin.get();
    cout << "Please type any text to save to the file.\n";
    cout << "Text: ";
    cin.getline(UserInput, 512);


    ofstream bravo;
    bravo.open(FileName);
    bravo << UserInput;
    bravo.close();


    the problem is i got an error after compiling it says : no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >:pen(std::string&)'

    can anyone help me to point out where my mistake is?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Quote Originally Posted by Elkvis View Post
    and not cheaply either. it's likely that you'll owe microsoft royalties if you distribute your program.
    That is not true.

    Using Microsoft run-time components would not give Microsoft a claim to royalties from a program developer. However, if your program was to require certain files from the office distribution, there would probably be some license fee (paid by either you or the end user) if you distribute and install those files. It would be easier to simply stipulate that your program requires Office to be installed.

    Quote Originally Posted by imjustnew View Post
    can anyone help me to point out where my mistake is?
    The error message is telling you that ofstream::open() does not accept an argument of type std::string.

    Change the line "bravo.open(FileName)" to "bravo.open(FileName.c_str())"

    Next time, read the documentation. And disable smilies in your posts.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    owh yea sorry the smile just appear itself when i paste it...

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    i just realized that there are no numbers appear and only text appears when i open the file from the saved output in excel format. Why all the numbers missing in the excel?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Post your code to generate the .csv file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    below is the program.....the problem is when i open the .txt file the result didnt appear...only txt appear ....how im gonna solve this?


    [tag]
    Code:
    #include <iostream>
    #include <math.h> 
    #include <stdio.h>
    #include <iomanip>
    #include <stdlib.h>
    #include <conio.h>
    #include <sstream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main (int argc, char *argv[])
    
    
    {
        int V=50, R=70, freq, a, b = 180, minFreq, maxFreq, intvFreq;
        float L=10e-3, C=10e-6, PI=3.142, anglepol, polZ, Ipol, cart_a, cart_b;
        float xL, xC;
        char quit, restart, ans, again;
        float phs = 45, angVZ;
        
        
        
        
        
        
          do{
          system ("cls");
          
              cout << "\n This is a series RLC circuit program to calculate the current of the circuit\n" << endl;
              cout << "\t              in Polar and Cartesian form \n" << endl;
              cout << "\t------------R------------L-----------C--------------" << endl;
              cout << "\t|                                                   |" << endl;
              cout << "\t----------------------AC supply---------------------" << endl;
              cout << "\n";
              
              cout << endl; 
              
              cout << "\n Please," << endl;
              cout << "\n        Enter the minimum frequency: " ; 
              cin >> minFreq ;
              
              cout << "\n        Enter the maximum frequency: " ; 
              cin >> maxFreq ;
              
              cout << "\n        Enter the frequency interval: ";
              cin >> intvFreq ;
              
              cout << endl << "\n\tFrequency Hz\tCurren" << endl;
              cout <<"\t\t        Polar\n";
              cout <<"\t\t\t(current,deg)";
              cout <<"\n" << endl;
              
              ofstream myfile;
              myfile.open ("currentdata.txt");
              myfile << "\tcurrent of circuit\n";
              myfile << "\n\n\n\tFreq\t\tcurrent\n";
              
              for (freq=minFreq; freq<=maxFreq; freq=freq+intvFreq)
                       
                    {
                     xL = 2*PI*freq*L;
                     xC = 1/(2*PI*freq*C);
                     
                     if (xL > xC)
                       { 
                        anglepol = atan((xL - xC)/R)*(b/PI);
                        
                        }
                     else if (xL < xC)
                        {
                        anglepol = atan((xC-xL)/R)*(b/PI) ; 
                        
                        }
                    if (xL > xC)
                     polZ = sqrt((R*R)+((xL - xC)*(xL - xC)));
                    
                    else if (xL < xC)
                     polZ = sqrt((R*R)+((xC - xL)*(xC - xL)));
                     
                       angVZ = phs - anglepol;
                       Ipol = V/polZ;
                     
                       cart_a = Ipol * cos(angVZ*PI/b);
                       cart_b = Ipol * sin(angVZ*PI/b);
                       
                       cout.precision(2);
                     
                       printf ("\t%d\t\t%3.2f,%3.2f\n", freq, Ipol, anglepol);     
                      
                       
                    }
                     
                      
                    
                    cout << "\n\n \t\t  Press r and enter to restart the program" << endl;
                    cout << "\n \t\t\t\t    or      " << endl;
                    cout << "\n \t\t   Press q and enter to quit the program" << endl;
                    
                    
                   myfile.close();
                   cout << "\n\n\tType quit to close:  ";
                   cin >> ans;
       
                    
                    
                    }while (ans != 'q'); // quit program                 
                    
        
                   
                     while(toupper(again) == 'r'); //restart program
                   
                   return 0;       
             }
    [/tag]

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    how im gonna solve this?
    You may want to actually try to write some numbers into your file. Also you should not be mixing C stdio functions with C++ streams.

    Jim

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    so why the result didnt appear in txt file?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    You never try to write anything but the text to the file. Show where you think you are writing the result to the file.

    Jim

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-24-2010, 01:21 PM
  2. output format
    By Beowolf in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2007, 12:22 AM
  3. Save to an Excel file
    By SpookyDescendan in forum C++ Programming
    Replies: 1
    Last Post: 09-20-2006, 07:50 AM
  4. save the output of child process
    By gandalf_bar in forum Linux Programming
    Replies: 3
    Last Post: 05-14-2004, 01:59 AM
  5. how can i save the output of this function to use later?
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 07-18-2002, 07:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21