brechtsanders / xlsxio

XLSX I/O - C library for reading and writing .xlsx files
MIT License
397 stars 113 forks source link

xlsxioread_sheet_next_cell has bug when the xlsx file like this ,hopefully fix it(updated) #88

Open abcdef123ghi opened 3 years ago

abcdef123ghi commented 3 years ago

I tried to read a xlsx file which has only one sheet and has no any else sheet in it ,it just has 11 rows and 1 column ,beside the first row was empty ,there's about 300 rows datas in it, like this: https://drive.google.com/file/d/1gbfNNBHZZ2hmOaYMpezpkfAQsnYLRpHr/view?usp=sharing

my case was I followed your examples and tried to parse the xlsx file but it doesn't work properly ,I just found out why,I changed the extension name which is xlsx to zip and extracted the xml files to a folder as well,tried to figure out what the hell, I just found the data in sheet1 were stored in sharedStrings.xml not in sheet1.xml which was in worksheets folder,that's why it didn't work properly,but my case was unusual cuz I tried to built another xlsx file,it's opposite which datas were store in sheet1.xml as usual

I tried to read it with the follwing code

int readelmlist(const char* path, int& retrecordscount, char** prodno)
{

   retrecordscount=0;

 xlsxioreader xlsxioread;
    if ((xlsxioread = xlsxioread_open(path)) == NULL) {
     fprintf(stderr, "Error opening .xlsx file\n");
     return 1;
   }

   //read values from first sheet
    char* value;
   xlsxioreadersheet sheet;
   const char* sheetname =NULL;
    printf("Contents of first sheet:\n");
    if ((sheet = xlsxioread_sheet_open(xlsxioread, sheetname, XLSXIOREAD_SKIP_EMPTY_ROWS)) != NULL) {
    //read all rows
    int rowcount=0;
     while (xlsxioread_sheet_next_row(sheet)) {
     //read all columns
       if (rowcount>0)
       {

             value = xlsxioread_sheet_next_cell(sheet);  

             printf("%s\t", value);
              const char * myvalue=value;
              size_t mysize=strlen(myvalue);
              strcpy(prodno[rowcount-1],myvalue);
              free(value);

        }
         rowcount++;

         printf("\n");
    }
       retrecordscount=rowcount;
       xlsxioread_sheet_close(sheet);
     }
       return 0;
  }

int main()
{
std::cout << "Hello World!\n";
    const char* path = "list1.xlsx";
    int retrecordscount = 0;

    char ** prodno = new char* [20000];

    for (int i = 0; i < 20000; i++)
    {
        prodno[i] = new char[10];
    }

    int result = readelmlist(path, retrecordscount, prodno);
    for (int i = 0; i < retrecordscount; i++)
    {
        std::cout << prodno[i] << std::endl;
    }
}

the problem was xlsxioread_sheet_next_cell not working properly which it just get the first digit of the value of the cell,not a whole number of the cell,any idea? I was using xlsxio which downloaded from the windows binary file which was xlsxio-0.2.29-binary-win32.zip used it on machine with MingW and g++ 10.2.0 due to your library was not working on Visual C++ I tried to build this program with g++ -g readelmxlsxtest5.cpp -o readelmxlsxtest5.exe -L/mingw32/lib -llibxlnt -llibiconv -I/mingw32/include -I/home/redmond/xlsxio-0.2.29/include -L/home/redmond/xlsxio-0.2.29/lib -llibxlsxio_readw