Identificativo articolo: 178605 - Ultima modifica: giovedì 6 ottobre 2011 - Revisione: 3.0

How to Determinare la versione di una cartella di lavoro di Microsoft Excel

Suggerimento di sistemaIl presente articolo fa riferimento a un sistema operativo diverso da quello in uso. Il contenuto dell'articolo che potrebbe non essere relativo al sistema in uso è disabilitato.
Espandi tutto | Chiudi tutto

Sommario

In questo articolo verrà spiegato come determinare la versione di una cartella di lavoro di Microsoft Excel (xls).

Informazioni

In Microsoft Excel i dati vengono salvati tramite l'archiviazione strutturata. In particolare viene creato un flusso di dati denominato "Cartella di lavoro" (in precedenza semplicemente "Cartella") in cui viene salvato il contenuto a partire da un record BOF (Beginning of File, inizio del file). Oltre alla versione, questo record contiene attributi utili della cartella di lavoro. Il codice Microsoft Visual C++ riportato sotto spiega come aprire un file, leggerlo e restituire il numero di versione basato sul BOF:
  1. In Microsoft Developer Studio creare una nuova applicazione console Win32.
  2. Aggiungere un file di origine C++ (cpp) al progetto e inserirvi il seguente codice:
    #include <windows.h>
    #include <iostream.h>   // My additions
    
    // BOF record from Microsoft Excel
    typedef struct _xlbof
    {
       char bofMarker; // Should be 0x09
    
       char vers;  // Version indicator for biff2, biff3, and biff4
                   // = 0x00 -> Biff2
                   // = 0x02 -> Biff3
                   // = 0x04 -> Biff4
                   // = 0x08 -> Biff5/Biff7/Biff8
    
       char skip[2]; // Unspecified
    
       short int vers2;  // Version number
                         // 0x0500 -> Biff5/Biff7
                         // 0x0600 -> Biff8
    
       short int dt;     // Substream type (not used in this example)
    
       short int rupBuild;  // Internal build identifier
       short int rupYear;   // Internal Build year
    } XLBOF;
    
    
    
    //* XLVersionFromFile() ******************************************
    //* Returns
    //*        n for BiffN
    //*   i.e. 8 for Biff8 (Microsoft Excel 97, Excel 2000, Excel 2002)
    //*
    //*        Negative if an error occurs
    //****************************************************************
    
    int XLVersionFromFile(char *filename) {
       // Translate filename to Unicode
       WCHAR wcFilename[1024];
       int i = mbstowcs(wcFilename, filename, strlen(filename));
       wcFilename[i] = 0;
    
       IStorage *pStorage;
       HRESULT hr;
       XLBOF xlbof;
    
       // Open the document as an OLE compound document
       hr = ::StgOpenStorage(wcFilename, NULL,
                  STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);
    
       if(!FAILED(hr)) {
          // Open the data-stream where Microsoft Excel stores the data
          IStream *pStream;
          hr = pStorage->OpenStream(L"Workbook", NULL,
                  STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);
    
          // If "Workbook" does not exist, try "Book"
          if(FAILED(hr)) {
             hr = pStorage->OpenStream(L"Book", NULL,
                  STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);
          }
          if(!FAILED(hr)) {
             // Read the relevant BOF information
             DWORD dwCount; // bytes read
             pStream->Read(&xlbof, sizeof(XLBOF), &dwCount);
    
             // Let go of the IStream pointer
             pStream->Release();
          }
          else return -2;
    
          // Let go of the IStorage pointer
          pStorage->Release();
        }
        else return -1;
    
        // Determine which version to return
        if(xlbof.vers != 0x08) return (xlbof.vers + 4) / 2;
        else {
           switch(xlbof.vers2) {
             case 0x0500:  // Either Biff5 or Biff7
                // Biff7's rupYear is at least 1994
                if(xlbof.rupYear < 1994) return 5;
    
                // Check for specific builds of Microsoft Excel 5
                switch(xlbof.rupBuild) {
                   case 2412: // XL5a
                   case 3218: // XL5c
                   case 3321: // NT XL5
                      return 5;
                   default:
                      return 7;
                }
    
           case 0x0600:  return 8;
           }
        }
    
        // Version not recognized. Perhaps there is a newer version.
        return -3;
    }
    
    void main()
    {
       int iretVal = 0;
       iretVal = XLVersionFromFile("C:\\Test.xls");
                                   //Adapt the filename to your example
       cout << "The Excel Version is " << iretVal << "\n\n\r";
       return;
    }
    
  3. Nella funzione main() può essere necessario modificare il percorso e il nome di file della cartella di lavoro di Microsoft Excel nella seguente riga di codice:
     iretVal = XLVersionFromFile("C:\\Test.xls");
    

Riferimenti

Guida in linea di Microsoft Visual C++, versione 5.0; cercare "archiviazione strutturata"

Microsoft Developers Network (MSDN); cercare "archiviazione strutturata", "formato di file Microsoft Excel", "BOF", "beginning of file"

Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2); cercare "formato di file Microsoft Excel", "BOF", "beginning of file"

Le informazioni in questo articolo si applicano a:
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
Chiavi: 
kbhowto KB178605
LE INFORMAZIONI CONTENUTE NELLA MICROSOFT KNOWLEDGE BASE SONO FORNITE SENZA GARANZIA DI ALCUN TIPO, IMPLICITA OD ESPLICITA, COMPRESA QUELLA RIGUARDO ALLA COMMERCIALIZZAZIONE E/O COMPATIBILITA' IN IMPIEGHI PARTICOLARI. L'UTENTE SI ASSUME L'INTERA RESPONSABILITA' PER L'UTILIZZO DI QUESTE INFORMAZIONI. IN NESSUN CASO MICROSOFT CORPORATION E I SUOI FORNITORI SI RENDONO RESPONSABILI PER DANNI DIRETTI, INDIRETTI O ACCIDENTALI CHE POSSANO PROVOCARE PERDITA DI DENARO O DI DATI, ANCHE SE MICROSOFT O I SUOI FORNITORI FOSSERO STATI AVVISATI. IL DOCUMENTO PUO' ESSERE COPIATO E DISTRIBUITO ALLE SEGUENTI CONDIZIONI: 1) IL TESTO DEVE ESSERE COPIATO INTEGRALMENTE E TUTTE LE PAGINE DEVONO ESSERE INCLUSE. 2) I PROGRAMMI SE PRESENTI, DEVONO ESSERE COPIATI SENZA MODIFICHE, 3) IL DOCUMENTO DEVE ESSERE DISTRIBUITO INTERAMENTE IN OGNI SUA PARTE. 4) IL DOCUMENTO NON PUO' ESSERE DISTRIBUITO A SCOPO DI LUCRO.