Exporting from C to Excel.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HKKID
    New Member
    • Mar 2007
    • 3

    Exporting from C to Excel.

    I've got a program in C that creates a 2 dimensional array pN with dimensions of [nt] and [nx]. I need to export the array to excel, so I can do useful math on it.

    I can get the fwrite command to save a 1d array for me, but not a 2d array. Tried looking up programming guides online, and all the ones I could find are limited to 1d arrays as well.


    The best I could come up with was to try and break my array up into a bunch of 1 dimensional arrays, and export each of them individually. However, since my 2d array is something like 300 X 1000 (exact size depends on user input), this isn't a very practical solution, even when put inside a for loop.

    I've got all of 4 days programming experience, and don't speak "programmer " at all, so if you could limit your explanation to common english (as hard as I'm sure that is) I would greatly appriciate some help.




    *Note, for what I want to do to the data later, I do need to export specifically to Excel. If there's a shortcut for doing that versus exporting to a more general format (like a .txt file or a generic spreadsheet), I'd rather take the shortcut.

    Thanks in advance.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by HKKID
    I've got a program in C that creates a 2 dimensional array pN with dimensions of [nt] and [nx]. I need to export the array to excel, so I can do useful math on it.

    I can get the fwrite command to save a 1d array for me, but not a 2d array. Tried looking up programming guides online, and all the ones I could find are limited to 1d arrays as well.


    The best I could come up with was to try and break my array up into a bunch of 1 dimensional arrays, and export each of them individually. However, since my 2d array is something like 300 X 1000 (exact size depends on user input), this isn't a very practical solution, even when put inside a for loop.

    I've got all of 4 days programming experience, and don't speak "programmer " at all, so if you could limit your explanation to common english (as hard as I'm sure that is) I would greatly appriciate some help.




    *Note, for what I want to do to the data later, I do need to export specifically to Excel. If there's a shortcut for doing that versus exporting to a more general format (like a .txt file or a generic spreadsheet), I'd rather take the shortcut.

    Thanks in advance.
    The best idea I can think of is to print them to a file, comma-delimited, to import into Excel.

    When you say you can do it for a 1d array, but not a 2d, what are you trying, and what did that return? Can you just not print the 2d array (the values for the first show up, but not the second)?

    Comment

    • HKKID
      New Member
      • Mar 2007
      • 3

      #3
      Originally posted by sicarie
      The best idea I can think of is to print them to a file, comma-delimited, to import into Excel.

      When you say you can do it for a 1d array, but not a 2d, what are you trying, and what did that return? Can you just not print the 2d array (the values for the first show up, but not the second)?
      actually I can get it to Print the 2d array using the code below, I just can't figure out how to SAVE it.

      Code:
      for (i=0; i <= nx; i++)
      		{
      		for (m=1; m<=nt; m++)
      			{
      			printf("%f", pn[i][m]);
      			printf("    ");
      			}
      		printf("\n");
      		}
      As for what I'm trying... I figured I would need something like this:

      Code:
      // code to create my array//
      
      FILE *fp;
      
      fp=fopen("spreadsheetname.xls", wb);  /* I have no idea why the "wb" is there, but it shows up in all the examples I could find, so I'm assuming it matters */
      
      fwrite(pn, [[something to indicate the location of the data I want to save]], [[something to indicate location on the .xls file to put the data]], fp);
      
      fclose(fp);
      But, I've got no clue what the syntax for the part in the middle should be.

      For sake of reference, the relevant part of the programming guide I've been using is Here.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        fwrite writes binary data which is not what you want, it is not outputting the data in a format readable by Excel. For the format to be readable by Excel it will also need to be readable by you in Notepad.

        However all is not lost, there is a function fprintf, this is very similar to printf except that it outputs to a file rather than the screen.

        You will need to alter you printf loop to an fprintf loop and also make it output a comma instead of the spaces between each value.


        I will say this though, c is capable of doing any calculations that Excel is and if you plan envolves Exporting to Excel doing some calculations and then re-Imorting the data back to the program then I would say you are making a serious design error.

        Comment

        • HKKID
          New Member
          • Mar 2007
          • 3

          #5
          Originally posted by Banfa
          I will say this though, c is capable of doing any calculations that Excel is and if you plan envolves Exporting to Excel doing some calculations and then re-Imorting the data back to the program then I would say you are making a serious design error.
          Thanks for the help!

          And to answer your question, I may be new at C, but I'm not an idiot. I wasn't planning on importing my data back into the program. I have a pre-built mathematica program that does peak fitting, uncertainty analysis, and a few other useful things for me. All i need to make that program do the things I'm interested in, is my data saved as a .xls file :-p.

          Comment

          Working...