Directly read Excel file data

Source: Internet
Author: User
Author/wegf

Download source code

Preface
Because of the various needs to directly read the Excel file data, but the search on the Internet has not found a good method for multiple times,
It is generally read through ODBC or OLE, but both methods have limitations... (I believe everyone knows ).
What should we do? No way, you have to select the most difficult way-to analyze the Excel file format.
Introduction
MS Excel is well known as spreadsheet processing software. The Excel file format is a specific Biff (Binary Interchange File Format). Biff stores many records. The first record includes the record header and record body. The record header is 4 bytes, the first two specify the record type code (opcode), and the last two specify the record length. The record body stores the actual data of the record.
For example:

 
Bof record | record header | record body | byte | 0 1 2 3 | 0 1 2 3 | ------------------------------------------- contents | 09 | 00 | 04 | 00 | 02 | 00 | 10 | 00 | --------------------------------------- | opcode | length | version | file | Number | type | record header: opcode: 09 h is BOF; Length: 04 H record body is 4 bytes long; record body: Version Number: 02 h is version number (2 for the initial version of Excel) File Type: 10 H is a worksheet file. For details, refer to MS Excel file format.

Description
The following is a simple description of the program in this article.

Step 1: open the file cfile F; cfileexception E; // open the file if (! F. open ("d :\\ book1.xls", cfile: moderead, & E) {tchar szerror [1024]; E. geterrormessage (szerror, 1024); afxmessagebox (szerror); return;} Step 2: Read version number // read version while (dwpos <dwlen) {nread = f. read (void *) & recno, 2); If (recno = xl_bof) {word ver, type; F. read (void *) & reclen, 2); F. read (void *) & Ver, 2); F. read (void *) & type, 2); F. seek (reclen, cfile: Current); int ver = 0; Switch (VER) {Case biff7: ver = 7; break; Case biff8: ver = 8; afxmessagebox ("biff8"); break;} int type = 0; Switch (type) {Case workbook: TYPE = 5; afxmessagebox ("workbook"); break; Case Worksheet: type = 16; afxmessagebox ("worksheet"); break; Case chart: TYPE = 32; afxmessagebox ("chart"); break;} dwpos = f. getposition ();} Step 3: Read Other data. seektobegin (); dwpos = f. getposition (); // read table data while (dwpos <dwlen) {nread = f. read (void *) & recno, 2); Switch (recno) {Case xl_bof: {f. read (void *) & reclen, 2); afxmessagebox ("Bof");} break; Case xl_boundsheet: {dwordtemp; bytevisi; bytetype; tcharname; F. read (void *) & reclen, 2); F. read (void *) & temp, 4); F. read (void *) & Visi, 1); F. read (void *) & type, 1); F. read (void *) & strlen, 2); F. read (void *) & name, strlen); char Buf [128]; memset (BUF, 0x0,128); strncpy (BUF, & name, strlen ); afxmessagebox (BUF);} break; Case xl_dimension: f. read (void *) & reclen, 2); F. seek (reclen, cfile: Current); afxmessagebox ("dimension"); break; Case 0xe2: // interfacedf. read (void *) & reclen, 2); afxmessagebox ("E2"); break; Case xl_sst: f. read (void *) & reclen, 2); F. seek (reclen, cfile: Current); afxmessagebox ("SST"); break; Case xl_number: f. read (void *) & reclen, 2); afxmessagebox ("Number"); break; Case xl_string: f. read (void *) & reclen, 2); afxmessagebox ("string"); break; Case xl_rk: f. read (void *) & reclen, 2); afxmessagebox ("rk"); break; Case xl_label: {f. read (void *) & reclen, 2); afxmessagebox ("label");} break; Case 0xd6: f. read (void *) & reclen, 2); afxmessagebox ("rstring"); break; Case xl_eof: dwpos = dwlen; afxmessagebox ("EOF"); break; default: nread = f. read (void *) & reclen, 2); If (nread = 0) dwpos = dwlen; break ;}} Step 4: Close the file F. close ();

End
This document describes how to analyze the Excel file format and read its data, independent of the MS office system. The above program only reads the most basic information. If you need more complete analysis (I am still in progress...) for the application, of course, there are many ways to read the Excel file. Here I only show you how to use it,
I hope to have more conversations with friends!

References
1. Microsoft Excel File Format
2. msdn Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.