我们如何使用 POI API 读取受保护的密码 excel 文件 (.xls)

发布于 2024-07-30 02:36:52 字数 172 浏览 7 评论 0原文

我刚刚学习了 POI,发现 HSSF 的读取和创建 excel 文件(.xls)非常简单。 但是,当我想读取受密码保护的Excel时,我发现了一些问题。 我花了一个小时在互联网上找到这个解决方案。

请你帮我解决这个问题。 如果您能给我一个代码片段,我非常高兴。

谢谢。

I've just learned POI and find the HSSF is very simple to read and create excel file (.xls).
However, I found some problem when want to read excel protected with password.
It took me an hour to find this solution on internet.

Please could you help me to solve this problem.
I'm very glad if you could give me a code snippet.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

琉璃梦幻 2024-08-06 02:36:53

请参阅 http://poi.apache.org/encryption.html - 如果您使用的是足够新的 Apache 副本POI(例如3.8)然后加密的.xls文件(HSSF)和.xlsx文件(XSSF)可以解密(证明你有密码!)

但目前你无法写出加密的excel文件,只能写出未加密的文件

See http://poi.apache.org/encryption.html - if you're using a recent enough copy of Apache POI (eg 3.8) then encrypted .xls files (HSSF) and .xlsx files (XSSF) can be decrypted (proving you have the password!)

At the moment you can't write out encrypted excel files though, only un-encrypted ones

戈亓 2024-08-06 02:36:53

在您写下问题时,使用 Apache POI 并不容易。 从那时起,支持已经取得了长足进步

。如今,如果您想打开受密码保护的 Excel 文件,无论是 .xls 还是 .xlsx,您都知道该文件的密码,您需要做的就是使用 WorkbookFactory.create(File,Password),例如

File input = new File("password-protected.xlsx");
String password = "nice and secure";
Workbook wb = WorkbookFactory.create(input, password);

这将识别文件的类型,使用给定的密码对其进行解密,然后为您打开它。 然后您就可以正常阅读内容了

At the time you wrote your question, it wasn't easy to do with Apache POI. Since then, support has come on a long way

These days, if you want to open a password protected Excel file, whether .xls or .xlsx, for which you know the password, all you need to do is use WorkbookFactory.create(File,Password), eg

File input = new File("password-protected.xlsx");
String password = "nice and secure";
Workbook wb = WorkbookFactory.create(input, password);

That'll identify the type of the file, decrypt it with the given password, and open it for you. You can then read the contents as normal

爺獨霸怡葒院 2024-08-06 02:36:53

这是一个完整的示例代码,它读入受保护的 Excel 文件,使用密码解密并写出不受保护的 Excel 文件

    public static void readProtectedBinFile() {
    try {

        InputStream inp = new FileInputStream("c:\\tmp\\protectedFile.xls");
        org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword("abracadabra"); 

        Workbook wb;
        wb = WorkbookFactory.create(inp);

        // Write the output to a file
        FileOutputStream fileOut;
        fileOut = new FileOutputStream("c:\\tmp\\unprotectedworkbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Here is a complete example code that reads in a protected excel file, decrypts using a password and writes out unprotected excel file

    public static void readProtectedBinFile() {
    try {

        InputStream inp = new FileInputStream("c:\\tmp\\protectedFile.xls");
        org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword("abracadabra"); 

        Workbook wb;
        wb = WorkbookFactory.create(inp);

        // Write the output to a file
        FileOutputStream fileOut;
        fileOut = new FileOutputStream("c:\\tmp\\unprotectedworkbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
转角预定愛 2024-08-06 02:36:53

这是读取 Excel 文件的代码,检查 .xls 和 .xlsx(有密码保护或没有密码保护)作为完整的示例代码。

 private Workbook createWorkbookByCheckExtension() throws IOException, InvalidFormatException {
                Workbook workbook = null;
                String filePath = "C:\\temp\\TestProtectedFile.xls";
                String fileName = "TestProtectedFile.xls";
                String fileExtensionName = fileName.substring(fileName.indexOf("."));
                if (fileExtensionName.equals(".xls")) {                         
                    try {
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        workbook = new HSSFWorkbook(fileInputStream);                               
                    } catch (EncryptedDocumentException e) {
                        // Checking of .xls file with password protected.
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        Biff8EncryptionKey.setCurrentUserPassword("password");
                        workbook = new HSSFWorkbook(fileInputStream);
                    }                           
                } else if (fileExtensionName.equals(".xlsx")){
                    // Checking of .xlsx file with password protected.
                    String isWorkbookLock = "";
                    InputStream is = null;
                    is = new FileInputStream(new File(filePath));
                    if (!is.markSupported()) {
                        is = new PushbackInputStream(is, 8);
                    }

                    if (POIFSFileSystem.hasPOIFSHeader(is)) {
                        POIFSFileSystem fs = new POIFSFileSystem(is);
                        EncryptionInfo info = new EncryptionInfo(fs);
                        Decryptor d = Decryptor.getInstance(info);
                        try {
                            d.verifyPassword("password");
                            is = d.getDataStream(fs);
                            workbook = new XSSFWorkbook(OPCPackage.open(is));
                            isWorkbookLock = "true";
                        } catch (GeneralSecurityException e) {
                            e.printStackTrace();
                        }               
                    }           
                    if (isWorkbookLock != "true") {
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        workbook = new XSSFWorkbook(fileInputStream);
                    }
                }
                return workbook;
            }

That is the code for Read Excel file with checking of .xls and .xlsx (with password protected or without password protected) as complete example code.

 private Workbook createWorkbookByCheckExtension() throws IOException, InvalidFormatException {
                Workbook workbook = null;
                String filePath = "C:\\temp\\TestProtectedFile.xls";
                String fileName = "TestProtectedFile.xls";
                String fileExtensionName = fileName.substring(fileName.indexOf("."));
                if (fileExtensionName.equals(".xls")) {                         
                    try {
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        workbook = new HSSFWorkbook(fileInputStream);                               
                    } catch (EncryptedDocumentException e) {
                        // Checking of .xls file with password protected.
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        Biff8EncryptionKey.setCurrentUserPassword("password");
                        workbook = new HSSFWorkbook(fileInputStream);
                    }                           
                } else if (fileExtensionName.equals(".xlsx")){
                    // Checking of .xlsx file with password protected.
                    String isWorkbookLock = "";
                    InputStream is = null;
                    is = new FileInputStream(new File(filePath));
                    if (!is.markSupported()) {
                        is = new PushbackInputStream(is, 8);
                    }

                    if (POIFSFileSystem.hasPOIFSHeader(is)) {
                        POIFSFileSystem fs = new POIFSFileSystem(is);
                        EncryptionInfo info = new EncryptionInfo(fs);
                        Decryptor d = Decryptor.getInstance(info);
                        try {
                            d.verifyPassword("password");
                            is = d.getDataStream(fs);
                            workbook = new XSSFWorkbook(OPCPackage.open(is));
                            isWorkbookLock = "true";
                        } catch (GeneralSecurityException e) {
                            e.printStackTrace();
                        }               
                    }           
                    if (isWorkbookLock != "true") {
                        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
                        workbook = new XSSFWorkbook(fileInputStream);
                    }
                }
                return workbook;
            }
梦言归人 2024-08-06 02:36:53

POI 将无法读取加密的工作簿 - 这意味着如果您保护了整个工作簿(而不仅仅是一个工作表),那么它将无法阅读。 否则,它应该有效。

POI will not be able to read encrypted workbooks - that means that if you have protected the entire workbook (and not just a sheet), then it won't be able to read it. Otherwise, it should work.

万水千山粽是情ミ 2024-08-06 02:36:53

拉维是对的。 看来您可以读取受密码保护的文件,但不能读取 POI 加密的文件。 请参阅http://osdir.com/ml/user -poi.apache.org/2010-05/msg00118.html。 以下代码打印出文件的痕迹

POIFSLister lister = new POIFSLister();
lister.viewFile(spreadsheetPath, true);

如果您得到提及加密的输出,则无法使用 POI 打开该文件。

Ravi is right. It seems you can read password protected, but not encrypted files with POI. See http://osdir.com/ml/user-poi.apache.org/2010-05/msg00118.html. The following code prints out a trace of the file

POIFSLister lister = new POIFSLister();
lister.viewFile(spreadsheetPath, true);

If you get an output mentioning encryption then you cannot open the file with POI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文