Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

Memento

[Rust] 엑셀 파일 생성 및 암호 적용 본문

Study/Rust

[Rust] 엑셀 파일 생성 및 암호 적용

벚꽃손님 2024. 4. 1. 12:48

rust 라이브러리 생성

$ cargo new wasm-lib --lib

 

엑셀 파일 생성

rust_xlsxwriter 라이브러리를 이용하여 엑셀 파일을 생성한다.

$ cargo add rust_xlsxwriter

 

자동생성된 src/lib.rs 파일을 아래와 같이 작성한다.

// lib.rs

use rust_xlsxwriter::*;

pub fn save_to_excel() {
    // Create a new Excel file object.
    let mut workbook = Workbook::new();

    // Add a worksheet to the workbook.
    let worksheet = workbook.add_worksheet();

    // Set the column width for clarity.
    worksheet.set_column_width(0, 22);

    // Write a string without formatting.
    worksheet.write(0, 0, "Hello");

    // Save the file to disk.
    workbook.save("demo.xlsx");
}

 

비밀번호 적용

파일에 비밀번호를 적용하려면 rust_xlsxwriter 자체에서 제공하지 않고, https://github.com/herumi/msoffice 를 활용하여야 한다.

우선 필수 라이브러리를 설치한다.

$ sudo apt install libssl-dev

 

이후 msoffice 소스를 git clone하여 빌드한다.

    mkdir work
    git clone https://github.com/herumi/cybozulib
    git clone https://github.com/herumi/msoffice
    cd msoffice
    make -j RELEASE=1

 

빌드하여 생성된 msoffice-crypt.exe 파일을 이용하여 비밀번호를 적용한다.

Command::new("/msoffice-crypt.exe")
        .args(["-e", "-p", "test", "demo.xlsx", "enc.xlsx"])
        .output()
        .expect("failed to execute process");

 

결과