How to Diagnose and Repair a Corrupted XLS File
Separate storage failure, structural damage and unsupported XLS features before attempting repair.
Failure Modes Specific to XLS
Classify the corruption before attempting any repair. The BIFF8 binary format used by Excel 97–2003 fails in four distinct ways. Applying the wrong repair method can make data loss permanent.
Storage failure – The OLE2 container and BIFF8 streams are logically intact, but the underlying storage medium introduced bit flips, bad sectors, or truncation. Observable signs: file size exactly matching a power of two (512 B, 1024 B, 2048 B), a file that opens but crashes on a specific row, or a checksum mismatch between the OLE2 directory and the actual stream lengths.
Structural damage – BIFF8 record boundaries are broken. The OLE2 container may parse correctly, but the Workbook stream contains malformed record headers, truncated SST strings, or missing BOF/EOF pairs. This produces the “file is not a recognizable format” error or a hard crash during the workbook globals parse.
Unsupported feature – The file was authored in an application that writes XLS-compatible BIFF8 but includes records or values outside the format’s hard limits. Examples: cell styles referencing more than 4,094 XF records, data validation ranges exceeding 256 columns, or chart sheet subtypes undefined in the BIFF8 specification. The file is structurally valid; the reader rejects it by policy.
Encryption mismatch – XLS supports RC4 encryption with a 40-bit or 128-bit key, plus XOR obfuscation for worksheet and workbook protection. If the password is unknown, the file is cryptographically locked. No repair tool can recover the plaintext without the key. This is not corruption.
Verify the OLE2 Container
Every XLS file is an OLE2 compound document. The first diagnostic step confirms the container is parseable, independent of BIFF8 content. Use olevba from the oletools package:
olevba --info corrupted.xls
A healthy file produces:
- OLE2 container: valid
- Streams listed: Workbook, \x05SummaryInformation, \x05DocumentSummaryInformation
- No warning about truncated or orphaned streams
If olevba reports OLE2 container: broken or truncated stream, the damage is at the storage or container level. If it reports no OLE2 signature found, the file either lacks the D0 CF 11 E0 magic bytes (storage failure or wrong format) or is a renamed XLSX.
Additional container checks: - OLE2 sector size: Must be 512 bytes (version 3) or 4096 bytes (version 4). Any other value indicates bit flips in the header. - Directory entry count: A zero entry count with a non-zero file size means the directory sector chain is corrupt.
Check Hard Limits and File Size
XLS BIFF8 enforces fixed dimensions. Use these to identify impossible states before attempting a parse.
| Limit | Value |
|---|---|
| Maximum rows per sheet | 65,536 |
| Maximum columns per sheet | 256 |
| Maximum sheets | Limited by available memory; BIFF8 SUPBOOK records reference sheets by 0-based index stored in a 2-byte unsigned integer, so practical limit is 65,535 |
| Maximum SST entries | No hard cap in spec, but the SST continues record uses a 4-byte absolute file offset; files exceeding 2 GB are unaddressable |
| Maximum XF records (cell formats) | 4,094 (0x0FFE); ID 0x0FFF is reserved |
File size checks: - 0 bytes: No data. Unrecoverable. - Exactly 512, 1024, 2048, 4096 bytes: Strong indicator of truncation at a sector boundary. - Greater than 2 GB: Impossible for valid XLS. Indicates a renamed XLSX, a ZIP archive mislabeled as XLS, or a storage error that appended null bytes.
Controlled Open for Error Classification
Open the file in a reader that reports parse errors without attempting automatic repair. LibreOffice Calc in safe mode is suitable:
libreoffice --safe-mode --calc corrupted.xls
Map the error message to a failure mode:
| Error message | Failure mode |
|---|---|
General input/output error |
Storage failure (OS cannot read the byte stream reliably) |
Unexpected end of input or EOF not found |
Structural damage (truncated Workbook stream; missing EOF record at offset specified in BOF) |
Unsupported feature or unknown record type |
Unsupported feature (valid BIFF8 record ID outside the reader’s implemented set) |
Password required or encryption not supported |
Encryption mismatch |
If the file opens without error in safe mode but fails in Excel, the problem is likely an unsupported feature that Excel’s stricter parser rejects.
Repair for Storage Failure
When the OLE2 container is intact but bit flips exist, the only reliable repair is restoring from backup. If no backup exists, extract raw cell data by ignoring BIFF8 record boundaries:
xls2csv corrupted.xls > recovered.csv
This utility (from the catdoc package) scans for strings and numbers without validating record headers. Formatting, formulas, merged cells, and multi-sheet structure are lost. The output is a single CSV containing every recoverable value in file order. Validate the CSV by checking that the number of delimited fields does not exceed 256 per logical row.
Repair for Structural Damage
When the OLE2 container is valid but the Workbook stream has broken BIFF8 records, attempt a stream-level parse with a tolerant library. xlrd 1.2.0 is the last version with full XLS support:
import xlrd
try:
book = xlrd.open_workbook("corrupted.xls", formatting_info=False)
book.release_resources()
print("Streams intact, sheets parsed:", book.nsheets)
except xlrd.XLRDError as e:
print("XLRDError:", e)
If xlrd opens the file but Excel does not, the damage is minor—BIFF8 records that Excel’s parser strictly validates but xlrd tolerates (e.g., a CONTINUE record with an unexpected size). Rebuild the Workbook stream:
import xlrd, xlwt
book = xlrd.open_workbook("corrupted.xls")
wb = xlwt.Workbook()
for idx in range(book.nsheets):
sh = book.sheet_by_index(idx)
ws = wb.add_sheet(sh.name)
for r in range(sh.nrows):
for c in range(sh.ncols):
ws.write(r, c, sh.cell_value(r, c))
wb.save("repaired.xls")
This writes a clean BIFF8 Workbook stream from the parsed data. All formatting, formulas, charts, and VBA are discarded. The output file will open in any XLS reader.
Repair for Unsupported Feature
If the file opens in LibreOffice but not Excel, the BIFF8 stream contains records or values outside Excel’s supported subset. Convert to XLSX to drop or approximate the unsupported feature:
libreoffice --headless --convert-to xlsx:"MS Excel 2007 XML" corrupted.xls
The resulting XLSX preserves cell data. Features that cannot map to XLSX (e.g., BIFF8-specific chart types without an Office Art equivalent) are silently dropped.
Encryption Mismatch: No Repair
XLS encryption uses RC4 with a key derived from the password via MD5 hash (40-bit) or SHA-1 (128-bit). Worksheet and workbook protection use XOR obfuscation with a 16-bit password hash. Without the password, the plaintext is unrecoverable. Brute-force tools exist but succeed only against weak passwords. The file is not corrupted; it is locked.
Unrecoverable States
Some conditions leave no recoverable data:
- Zero-byte files – No content exists.
- Valid OLE2 container with zero-length Workbook stream – The directory entry exists but the stream’s starting sector points to an empty or unallocated sector chain. Occurs when sync tools write the OLE2 header and directory but fail to flush stream data.
- Truncation mid-BIFF-record – The parser cannot locate the next record’s type byte. Heuristic scanners may extract partial strings, but cell alignment is lost and numeric data is unreliable.
- Strong RC4 encryption without the key – Ciphertext is indistinguishable from random bytes.
In these cases, rebuild from the most recent known-good backup.
Post-Repair Validation
After any repair, verify the output with observable checks:
- Open the repaired file in two independent applications (LibreOffice Calc and Microsoft Excel). Both must report no errors.
- Compare row counts:
sh.nrowsfrom the original parse must match the repaired file’s row count. A mismatch indicates rows lost during rebuild. - Check for column shift: the last populated cell in column IV (256th column) must not contain data originally in column A. A shift indicates a BIFF8 record misalignment during raw extraction.
- Run
olevba --info repaired.xlsand confirmOLE2 container: validwith no stream warnings.
If any check fails, the damage was more severe than the initial diagnosis indicated. Only raw extraction (xls2csv) will recover partial data.
Reference for repair corrupted xls file diagnostic checklist
Microsoft’s BIFF8 specification defines the OLE2 container structure (section 2.1, Compound File), the Workbook stream record sequence (section 2.2, Workbook Stream), and individual record formats including BOF/EOF, SST, and CONTINUE. The diagnostic steps above depend on the distinction between OLE2-level errors and BIFF8 record-level errors as documented in MS-XLS.