Can someone please tell me how to send data from my program to excel. I have a loop which is iteratively printing values to the console:

printf("16 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    printf("%.4x\n",rand16());
  }

but i need to send them to excel for analysis!! Please help

Recommended Answers

All 5 Replies

change printf to fprintf and write to a .csv file

FILE * fp = fopen("mydata.csv", "w");  // opens file to write ("w"), using the FILE pointer "fp"

//...

printf("16 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    fprintf(fp, "%.4x\n", rand16());   // prints to the file, using the file pointer "fp"

//...

fclose(fp);  // close the file after you're done

}

commented: straight to the point once again, very helpful poster +1

Thank you very much Jephthah..That works a charm!! :)

glad it helped.

side note, you can write any kind of file you want, the question is will application "X" understand it.

.CSV file just means "comma separated values" and this extension is typically opened by Excel on default. ally comma-separated values (strings or numbers) will appear across the columns, newlines will move down to the next row.

A true Excel file is an .XLS file and you can not very easily write a valid .XLS file without going through a lot of contortions with the Windows API and OLE Automation.

you could have also easily written a .TXT file and just opened it in any basic text editor such as WordPad or TextPad.


.

glad it helped.

side note, you can write any kind of file you want, the question is will application "X" understand it.

.CSV file just means "comma separated values" and this extension is typically opened by Excel on default. ally comma-separated values (strings or numbers) will appear across the columns, newlines will move down to the next row.

A true Excel file is an .XLS file and you can not very easily write a valid .XLS file without going through a lot of contortions with the Windows API and OLE Automation.

you could have also easily written a .TXT file and just opened it in any basic text editor such as WordPad or TextPad.


.

Thanks for the additional info..Will take note of that!

#include <stdio.h>
#include <stdlib.h>

int main()

FILE *fptr;
fptr = fopen ("data1.csv", "w");
int x, power2;
if (fptr = NULL)

    printf("File cannot be opened\n");
    exit(1);

for(x = 1; x <= 100; x++)

    power2 = x*x;
    fprintf(fptr, "%d\n", power2);

fclose(fptr);

Hi everyone. Im trying to write into excel through c programming. is my codes right?and also do i have to create an excel on the name data1 first before running this code on C? thanks

commented: Remember that posting under a 10 year old discussion is just that. Well hidden! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.