Thread: To save the output of C++ program in Excel format

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    To save the output of C++ program in Excel format

    I need to save the output of c++ Program in Excel format.
    I have already seen one post in this forum, but that wasn't sufficient for me.

    Can anyone kindly help me with this..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Which post did you see, and why was it not sufficient for you?

    Personally, I think your best bet is either to output to a text file in a CSV format that is then converted by Excel, or by using some library (but I have no recommendations to make).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Which post did you see, and why was it not sufficient for you?

    Personally, I think your best bet is either to output to a text file in a CSV format that is then converted by Excel, or by using some library (but I have no recommendations to make).
    The outputs are coming in one single column and they are not appending..thats my problem

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    3
    insert
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    void main()
    {
     clrscr();
     ofstream of("myfile.csv");
     int l,ar;
     cout<<"Enter the length:";
     cin>>l;
     of<<l;
     ar=l*l;
     cout<<"area="<<ar;
     of<<ar;
     of.close();
     getch();
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    #include<iostream.h>
    It includes a pre-standard header; to include the standard header:
    Code:
    #include <iostream>
    This is not wrong per se, but non-standard and rather unnecessary since your use of clrscr and getch aren't really necessary:
    Code:
    #include<conio.h>
    This is another pre-standard header:
    Code:
    #include<fstream.h>
    Use:
    Code:
    #include <fstream>
    This is wrong:
    Code:
    void main()
    It should be:
    Code:
    int main()
    With the standard headers, ofstream, cout, and cin should be written as std::ostream, std::cout, and std::cin respectively because they are declared in the std namespace. You can also use using declarations or a using directive, but you may find out the details about those later.

    These are terribly named:
    Code:
    int l,ar;
    Names should be descriptive. Furthermore, variables should be declared near first use. So, I might have written:
    Code:
    std::cout << "Enter the length:";
    int length;
    cin >> length;
    of << length;
    int area = length * length;
    This is unnecessary as the file stream will be closed when the of object is destroyed upon reaching the end of the scope of the main function:
    Code:
    of.close();
    Next, let's look at how you are actually writing to the CSV file:
    Code:
    of<<l;
    // ...
    of<<ar;
    That is not a CSV format. You should have a delimiter to separate the length and the area, and as the name implies, this delimiter is typically a comma. Then, you should end the record with a newline sequence.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    I could help you with writing the data directly to Excel using COM (Microsoft's Component Object Model), but it is a bit advanced. I've written library code for automating both Excel and Word. Just asking if you are interested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exporting C output to Excel sheet
    By Anuroop in forum C Programming
    Replies: 1
    Last Post: 05-30-2013, 12:17 PM
  2. C output saved in word or excel
    By Brian Joseph in forum C Programming
    Replies: 3
    Last Post: 04-19-2013, 01:01 PM
  3. save output file to excel format
    By imjustnew in forum C++ Programming
    Replies: 18
    Last Post: 12-31-2011, 11:09 AM
  4. Replies: 11
    Last Post: 09-24-2010, 01:21 PM
  5. Save to an Excel file
    By SpookyDescendan in forum C++ Programming
    Replies: 1
    Last Post: 09-20-2006, 07:50 AM

Tags for this Thread