Lesen von Excel-Datei / wchar_t und char



  • Hallo liebe C++'ler,

    ich habe ein Problem und habe auch nach einiger Recherche dazu keine Lösung gefunden, daher schildere ich mein Problem:

    -Ich versuche eine Excel-Datei in einem Programm einzulesen
    -Hierfür habe ich die Bibliothek LibXL verwendet
    -Ich habe alle Abhängigkeiten und Linker wie in der ReadMe beschrieben gesetzt
    -Zeichensatz unter Projekteinstellungen ist auf Multibyte-Zeichensatz gestellt
    -Verwendete Umgebung: Visual C++ 2010 Express

    Hier der Sourcecode:

    #include <iostream>
    #include "libxl.h"
    #include <Windows.h>
    
    using namespace libxl;
    
    int main() 
    {
       Book* book = xlCreateBook();
    
      const TCHAR szClassName[] = TEXT("input.xls");
    
       if(book->load(szClassName));
       {
          Sheet* sheet = book->getSheet(0);
          if(sheet)
          {
             for(int row = sheet->firstRow(); row < sheet->lastRow(); ++row)
             {
                for(int col = sheet->firstCol(); col < sheet->lastCol(); ++col)
                {
                   CellType cellType = sheet->cellType(row, col);
                   std::wcout << "(" << row << ", " << col << ") = ";
                   if(sheet->isFormula(row, col))
                   {
                      const wchar_t* s = sheet->readFormula(row, col);
                      std::wcout << (s ? s : "null") << " [formula]";
                   }
                   else
                   {
                      switch(cellType)
                      {
                         case CELLTYPE_EMPTY: std::wcout << "[empty]"; break;
                         case CELLTYPE_NUMBER:
                         {
                            double d = sheet->readNum(row, col);
                            std::wcout << d << " [number]";
                            break;
                         }
                         case CELLTYPE_STRING:
                         {
                            const wchar_t* s = sheet->readStr(row, col);
                            std::wcout << (s ? s : "null") << " [string]";
                            break;        
                         }
                         case CELLTYPE_BOOLEAN:
                         {
                            bool b = sheet->readBool(row, col);
                            std::wcout << (b ? "true" : "false") << " [boolean]";
                            break;
                         }
                         case CELLTYPE_BLANK: std::wcout << "[blank]"; break;
                         case CELLTYPE_ERROR: std::wcout << "[error]"; break;
                      }
                   }
                   std::wcout << std::endl;
                }
             }
          }
       }
    
       book->release();
       return 0;
    }
    

    An der Stelle

    const wchar_t* s = sheet->readFormula(row, col);
    

    kommt folgende Fehlermeldung:

    "error : ein Wert vom Typ "const char*" kann nicht zum Initialisieren einer Entität vom Typ "const wchar_t*" verwendet werden"

    Bisher habe ich nur die Lösung gefunden, den Zeichensatz von Unicode auf Multibyte umzusetzen, allerdings war bei mir von vornherein der Zeichensatz auf Multibyte gesetzt, daher weiß ich nicht, wie ich das Problem beheben kann.
    Ich hoffe jemand kann mir helfen. Danke!



  • Nimm TCHAR, das machst du ja weiter oben auch:

    const TCHAR* s = sheet->readFormula(row, col);
    

    Vermutlich musst du dann den Code noch an ein paar anderen Stellen anpassen...der Compiler sagt dir wo.



  • Danke erstmal für die hilfreiche Antwort, allerdings entsteht ein neues Problem, er kann die Datei nicht einlesen, das heißt:

    book->load(szClassName)
    

    kann nicht erfolgreich durchgeführt werden.
    Ich weiß nicht, warum er die Datei nicht findet, ich habe auch schon den gesamten Dateipfad+Datei eingegeben, dennoch kann er die Datei nicht einlesen.



  • Was sagt denn book->errorMessage() ?



  • Du escapest auch schön die escape-chars

    Oder wie auch immer der Mist heißt ...

    "C:\blabla.txt" zu "C:\\blabla.txt"



  • Okay, die Möglichkeit eines Fehlers über falsch gesetzte Slashs ist ausgeschlossen, da ich jetzt einfach "input.xls" geschrieben habe :

    book->load("D:\\input2.xls")
    

    ErrorMessage sagt mir:

    readHeader: file is corrupt (size <512 bytes)

    was an sich nicht sein kann, da die Datei mindestens 2kb besitzt laut Windows.

    Bisheriger Quellcode:

    #include <iostream>
    #include "libxl.h"
    
    using namespace libxl;
    
    int main() 
    {
       Book* book = xlCreateBook();
    
       if(book->load("D:\\input2.xls"))
       {
          Sheet* sheet = book->getSheet(0);
          if(sheet)
          {
             for(int row = sheet->firstRow(); row < sheet->lastRow(); ++row)
             {
                for(int col = sheet->firstCol(); col < sheet->lastCol(); ++col)
                {
                   CellType cellType = sheet->cellType(row, col);
                   std::wcout << "(" << row << ", " << col << ") = ";
                   if(sheet->isFormula(row, col))
                   {
                      const char* s = sheet->readFormula(row, col);	
                      std::wcout << (s ? s : "null") << " [formula]";
                   }
                   else
                   {
                      switch(cellType)
                      {
                         case CELLTYPE_EMPTY: std::wcout << "[empty]"; break;
                         case CELLTYPE_NUMBER:
                         {
                            double d = sheet->readNum(row, col);
                            std::wcout << d << " [number]";
                            break;
                         }
                         case CELLTYPE_STRING:
                         {
                            const char* s = sheet->readStr(row, col);
                            std::wcout << (s ? s : "null") << " [string]";
                            break;        
                         }
                         case CELLTYPE_BOOLEAN:
                         {
                            bool b = sheet->readBool(row, col);
                            std::wcout << (b ? "true" : "false") << " [boolean]";
                            break;
                         }
                         case CELLTYPE_BLANK: std::wcout << "[blank]"; break;
                         case CELLTYPE_ERROR: std::wcout << "[error]"; break;
                      }
                   }
                   std::wcout << std::endl;
                }
             }
          }
       }
    
       else if(!book->load("D:\\input2.xls")){
    
              std::cout << book->errorMessage() << std::endl;
    
       }
    
       book->release();
       return 0;
    }
    


  • .a



  • eventuell geht's auch nicht, weil die Bibliothek nicht frei ist. Ich werd's wohl mit einer anderes library versuchen jetzt.

    http://libxl.com/purchase.html


Anmelden zum Antworten