Read Password-Encrypted XLSX Files
Detect encrypted workbooks, provide passwords safely, and handle wrong-password failures.
Updated
Xlsx::isEncrypted()Xlsx::encryptionMode()ReaderOptions::withPassword()MnbExcel::decryptXlsx()What you will learn
- Detect encryption before starting a read.
- Open Standard or Agile encrypted XLSX files.
- Avoid leaking passwords into logs and manifests.
Detect encryption and mode#
use Mnb\PHPExcel\Format\Xlsx;
if (Xlsx::isEncrypted('secure-report.xlsx')) {
printf("Mode: %s\n", Xlsx::encryptionMode('secure-report.xlsx') ?? 'unknown');
}Read with an explicit password#
use Mnb\PHPExcel\Reader\Options\ReaderOptions;
$options = ReaderOptions::defaults()->withPassword($password);
foreach (MnbExcel::read('secure-report.xlsx', $options)
->sheet('Report')
->withHeaderRow(1)
->rows() as $row) {
// Process decrypted rows.
}Read a large encrypted workbook#
MnbExcel::largeRead('secure-orders.xlsx')
->password($password)
->sheet('Orders')
->withHeader()
->chunk(1000, $callback);Handle password secrets correctly#
- Read passwords from a secret store or protected configuration.
- Never include the password in queue payload logs or failed-row exports.
- Clear temporary decrypted files and restrict filesystem permissions.
- Return a generic “password invalid or file unsupported” message to untrusted clients.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue