Formatting Output File

May 20, 2009 at 2:09am
I created a project that will collect a lot of data, save it, and ultimately want to print out the sheets of data as a hard-copy. Although I've been using the simple output to a text file -- the user still has to find the text file, open it, switch the layout to landscape mode, then print it.

I was considering dumping this text file into excel (without creating a CSV file that the user will have to open with excel). I've used a program that would detect excel being open already, and dump all data into the cells automatically. Then it would just be simply clicking print from that point.

Or should I try to format this output another way? Anyone have any luck in getting a nice hard copy with minimal effort on the USER end? ( I don't want to create a file that the user has to open through excel ) Perhaps at the end of the program interface with a -USB- printer and print automatically from the program?
May 20, 2009 at 3:07am
Well, if it's not going to be TOO MUCH data, you could hold it in memory until the very end, then copy the formatted text to the clipboard. Additionally, you could play a sound to let the user know that the data is ready to be pasted.

Excel and similar spreadsheet programs will automatically put in different columns strings separated by '\t'. For example, for the string "one\ttwo\tthree", each word would be going in a different column.

EDIT: Oh, you could also write the file anyway, and then tell Excel to open it through the command line. Just don't use system(), for god's sake. Use CreateProcess().
Last edited on May 20, 2009 at 3:11am
May 20, 2009 at 3:33am
What's wrong with using system() for that purpose?
May 20, 2009 at 4:11am
How would I go about trying system()?

I only know the pause command and clear command. lol
May 20, 2009 at 11:49am
system() is blocking, which means that instead of exiting as soon as possible, his program will be bound to Excel's fate unless his user knows about ctrl+c or ctrl+break.
May 20, 2009 at 1:40pm
Good point.

It is possible to start non-blocking processes using system, though...
1 system( "start excel myfile.csv" );

But hey, I'm not advocating it over a better solution. :-)
May 20, 2009 at 4:24pm
Am I able to use system to

system( "start excel template.xls" );

And then dump the data cell by cell instead? To allow for formatting options -- size of text, etc? Would I need to use more system commands? -- Trying to find some resources on how to use system();
May 20, 2009 at 4:26pm
closed account (S6k9GNh0)
I'ts suggested you not use system for many reasons and you should probably find an alternative.

Reasons why: http://cplusplus.com/forum/articles/11153/

I personally would try and go with HelioS idea first. System should probably only be used for last resort or be used temporarily until a better solution is found.
Last edited on May 20, 2009 at 4:27pm
May 20, 2009 at 4:47pm
Typical lazy programmer... Choosing the fastest solution rather than the best. Mend your ways before it's too late!
May 20, 2009 at 4:55pm
I'm trying to find the best solution for the end-user that is the simpliest on their part. Not having to copy and paste anything or format the page. I've looked through the few C++ books and resources I could find any nothing is there on loading outside programs or formatting an output other than a simple text file. What is the best solution?
May 20, 2009 at 5:01pm
I explained it rather thoroughly in my first post, including how to format plain text so that Excel-like programs load different data into different columns, and what Windows API function to call to start Excel and pass it command line arguments. Which part don't you understand?
Last edited on May 20, 2009 at 5:01pm
May 20, 2009 at 6:44pm
The best solution is to use Excel as an OLE component in your application. That is essentially the same as embedding an Excel spreadsheet into your application, and you can control it.

The next best is helios's approach -- create the tab-delimited text file and use CreateProcess() to start Excel with the text file name as the command-line argument.

The last, but simplest, approach is to use system().
Topic archived. No new replies allowed.