使用WorkbookFactory无法打开带西里尔文密码的加密XLSX文件
Let's break down exactly why your Cyrillic password "абв" isn't working, while ASCII passwords like "abc" do:
The Root Cause
Excel's file encryption uses ISO-8859-1 (Latin-1) encoding to process passwords, not UTF-8 (Java's default string encoding). When you pass the string "абв" directly to WorkbookFactory.create(), Java encodes those characters as UTF-8 bytes—but Excel stored the password using Latin-1 bytes. These two byte sequences don't match, hence the "Password incorrect" error.
ASCII characters like "abc" work because their byte representation is identical in both UTF-8 and Latin-1, so there's no mismatch.
How to Fix It
You need to explicitly convert your Unicode password to match the Latin-1 encoding Excel expects. Here's how to adjust your code:
// Convert the Unicode password to Latin-1 encoded bytes, then back to a string String latin1Password = new String("абв".getBytes("ISO-8859-1"), "ISO-8859-1"); // Use this converted password to open the workbook Workbook workbook = WorkbookFactory.create(new FileInputStream("src//main//resources//Unicode.xlsx"), latin1Password);
Why Your Print Verification Didn't Catch This
When you printed the password string to a file, you likely used UTF-8 encoding (the default for most file writers). This makes the characters look correct visually, but it doesn't reflect the byte sequence Excel actually uses for password validation. The key is matching the encoding Excel used when setting the password, not just visual correctness.
If you want to verify the byte sequences, you can print the byte arrays for both the original string and the Latin-1 converted one—you'll see they're different.
内容的提问来源于stack exchange,提问作者Dariy Pozinenko

