【Rust】パスワード付きExcelファイルの読み取り

office-crypto パスワード付きの MS Office ファイルを復号化できる。 復号化したExcelファイルは calamine で読み取り操作できる。

[dependencies]  
office-crypto = "0.1.0"  
calamine = "0.24.0"

以下のようにして、calamine で読み取り操作できる。

use std::io::Cursor;
use office_crypto::decrypt_from_file;
use calamine::{open_workbook_from_rs, Reader, Xlsx};
  
fn main() -> Result<(), Box<dyn std::error::Error>> {  
  
    let path = "protected.xlsx";
    let decrypted: Vec<u8> = decrypt_from_file(path, "password")?;
  
    if let Ok(wb) = open_workbook_from_rs::<Xlsx<_>, _>(Cursor::new(decrypted)) {  
        let sheet_names = wb.sheet_names();  
        println!("{:?}", sheet_names);  
    };  

    Ok(())  
}

上記はシート名を出力する例。