You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WorkbookFactory无法打开带西里尔文密码的加密XLSX文件

Why Unicode Passwords Fail with Apache POI's WorkbookFactory

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:30:15