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..
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..
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What is your current code?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
insertCode:#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(); }
This is wrong:
It includes a pre-standard header; to include the standard header:Code:#include<iostream.h>
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 <iostream>
This is another pre-standard header:Code:#include<conio.h>
Use:Code:#include<fstream.h>
This is wrong:Code:#include <fstream>
It should be:Code:void 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.Code:int main()
These are terribly named:
Names should be descriptive. Furthermore, variables should be declared near first use. So, I might have written:Code:int l,ar;
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:std::cout << "Enter the length:"; int length; cin >> length; of << length; int area = length * length;
Next, let's look at how you are actually writing to the CSV file:Code:of.close();
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.Code:of<<l; // ... of<<ar;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.