Reading xls files

This is a discussion on Reading xls files within the C Programming forums, part of the General Programming Boards category; Hello everyone! I ran into another problem. So I have this excel spreadsheet that has like many many names and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Reading xls files

    Hello everyone! I ran into another problem. So I have this excel spreadsheet that has like many many names and many scores on projects (school stuff). My OBJECTIVE: Find the highest score on each project and print the name of the person. PROBLEMS: Some names also have two strings in them, like "Mc Donald" or "Adaji Ruvvali", etc.
    Code:
    FILE *fp;
    fp=fopen(projectscores.xls, r);
    So after I read the xls, how do I make it read the columns, there are total of 8 columns, One for the names, and 7 for the 7 projects. I was thinking of just going through the excel file and just saying finding the highest score on each project, but I was wondering if I can write code for this, so here we are ...

    I read the tutorial on the cprogramming website, but it doesn't talk about reading xls columns and rows. If you guys have the time to give me your advice and help , it will be greatly appreciated.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It'd be easer to save the file as ".csv" (Comma seperated values), otherwise you're going to have to read and process the Excel spreadsheet which is not easy.

    The former idea is far easier :-)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    hmm... really? Hmm... is it that much harder reading it from a xls spreadsheet? can't we something like

    Code:
    FILE *fp;
    fp=fopen(projectscores.xls, r);
    
    int i;
    int x;
    for(i=0; i != EOF; i++)
    {
           i = fgetc(fp);
           printf("Highest score is %d", i);
    }
    or something similar to that? Or am I really off.. lol

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No.

    Here is a 250 page document explaining the format: http://sc.openoffice.org/excelfileformat.pdf. Of course there may be libraries that do this for you, but it's still going to be more difficult than using the csv approach.

    BTW, Why do this in C? You can do it in Excel pretty easily...

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Hmm... 250 pages.... haha.

    Well I just like coding, its fun and it takes other stress off of me. Just trying things with coding and stuff, see how far I can go and stuff, but it seems like everything I try, i always hit a wall. lol.

    hey would this work? like so... ahh here's what I got so far, I was just wondering if it would work

    Code:
    typedef struct
    {
       char names[50];
       int scores[10];
    }PEOPLE;
    Then after this struct, I just make a few declaration functions, like one of them would get make sure the file name is correct, the other would maybe read the file into an array or something that can hold it and malloc some memory, then like finally make a function that will get the highest score? It might a lot of work, but its going to be fun

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are making a very large assumption -- that if the spreadsheet has "McDonald" in it, the file also has McDonald in it somewhere. If that was true, you wouldn't need a 250 page description of how to read the data.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by zacs7 View Post
    No.

    Here is a 250 page document explaining the format: http://sc.openoffice.org/excelfileformat.pdf. Of course there may be libraries that do this for you, but it's still going to be more difficult than using the csv approach.

    BTW, Why do this in C? You can do it in Excel pretty easily...
    Nice! Do you happen to have one on Power Point Format? You would be my personal hero if you did.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    OpenOffice is open source, get it yourself

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I know... I had to steal code from OpenOffice when I was doing a DB that compiled stuff out to PP slides. But as any OpenOffice user can tell you, their isn't 100% compatibility between programs.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    Quote Originally Posted by master5001 View Post
    Nice! Do you happen to have one on Power Point Format? You would be my personal hero if you did.
    Microsoft does post the details for their file formats, check here:
    http://www.microsoft.com/interop/doc...ryformats.mspx

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Sorry I was gone for a bit. Hmm so, is there no way to read my xls file or something?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dlwlsdn View Post
    Sorry I was gone for a bit. Hmm so, is there no way to read my xls file or something?
    Yes, there is, but just like you CAN build your own car from a few lumps of aluminium and some plate steel (and many other bits and pieces, most likely), it's much easier to achieve the goal in a different way: Save the .xls as .csv in Excel, then read that file - which is a relatively easy to read text-file, rather than a complex binary file.

    The document from MS: http://download.microsoft.com/downlo...cification.pdf
    is 349 pages long, and whilst MANY of those pages aren't necessary for what you want to do [you probably don't care about formatting options, comments on fields, revision records, etc, etc], I bet that unless someone who is REALLY experienced in this sort of thing is working on it, it will be months before you complete your project.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Alrighty, I saved it as a .csv. Now I'm confused on how to get the integers, how i have a clue on how to get the chars, but yeah. I'm going to read some tutorials and stuff. Thanks for the help ^^

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can translate a string (char array) to integer by using strtol() or sscanf("%d") for example. Reading tutorials is ALWAYS a good idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Here is my code at the moment, I was just thinking of a way for a while to do this... its quite long, but basically my comments are the things where it explains everything. I'll still be working on this , its fun. Cprogramming tutorials are awesome, but there are still questions that i have, especially about the fscan thingy...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    typedef struct {
        char names[50];
        int scores[7];
    }PEOPLE;
    
    // function declarations
    void the_file_name(char *filename);
    int how_many_lines(char *filename);
    void read_the_file_to_array(char *filename, PEOPLE peoples[], int *size);
    void get_the_stats(PEOPLE peoples[], int size);
    
    int main(void) {
        char filename[30];
        PEOPLE *peoples;
        int size;
    
        the_file_name(filename); // Finished the function
    
        peoples = malloc( (how_many_lines(filename)+1) * sizeof(PEOPLE)); // finished this function
    
        read_the_file_to_array(filename, peoples, &size); // NOT FINISHED......I can use this function to put things into arrays
    
        get_the_stats(peoples, size); // NOT FINISHED....Getting the scores..
    
        return EXIT_SUCCESS;
    }
    
    /* Function goes through student arrays and prints out the MAX score on each project.
     * so in the end, the function will display the person name and print the score and name
     * for each assignment, for assignment 1, it will go through that and find the max score and the name
    void getStats(PEOPLE peoples[], int size){
    
    // NOT FINISHED
    }
    
    
    
    /* My xls, cva file looks something like...
     * Hammy, Rogers (persons_id) | (what_student_number_they_are_in_class) assignment1 assignment2..  assignment7
     * I was thinking something using fscan and returning something as long as its != EOF.
     * perhaps putting it into the struct or something.
     * maybe something like, where the chars names[50] will hold the names of the students
     * and the scores[7], will hold the 7 assignments/projects they did.
     * so, each element space will hold a assignment, can i do something like this?
     * then pulling the highest number, by using a for loop?
     * also, i don't want the persons id nor the number number in class, just need assignments
     * hehe, sorry for asking so much
     */
    void readFileIntoArray(char *filename, PEOPLE peoples[], int *size){
    
    // NOT FINISHED
    }
    
    
    void the_file_name(char *filename){
        int length;
    
        do {
          printf("Enter a filename with .xls at the end please. ");
          fflush(stdout);
          scanf("&#37;s", filename);
          length = strlen(filename);
        }
        while(filename[len-1] != 's' || filename[length-2] != 'l' ||
            filename[len-3] != 'x' || filename[length-4] != '.');
    //FIINISHED
    }
    
    
    int how_many_lines(char *filename){
        FILE *fp;
        char a;
        int count=0, new_line=0;
        fp = fopen(filename, "r");
        while(fscanf(fp, "%a", &a) != EOF){
            if(a=='\n' && !new_line){
                count++;
                new_line = 1;
            }
            else if (a != '\n')
                new_line = 0;
    
        }
        fclose(fp);
        return count;
    // FINISHED
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  3. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 09:43 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21