Created
July 6, 2025 04:21
-
-
Save kuc-arc-f/2c35422c90f60cb9e7224055b495c259 to your computer and use it in GitHub Desktop.
Rust Excel edit , example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "excel-test" | |
version = "0.1.0" | |
authors = ["naka"] | |
edition = "2021" | |
[dependencies] | |
umya-spreadsheet = "2.3.1" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use umya_spreadsheet::*; // クレート本体の読み込み | |
use std::path::Path; | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
// 1. 既存ファイルの読み込み | |
let path = Path::new("input.xlsx"); | |
let mut book = reader::xlsx::read(path)?; // 普通に読み込み | |
// let mut book = reader::xlsx::lazy_read(path)?; // 大きいファイルの場合(遅延読み込み) | |
// 2. シート選択とセル編集 | |
{ | |
let sheet = book | |
.get_sheet_by_name_mut("Sheet1") | |
.ok_or("Sheet1 が見つかりません")?; | |
// セル A1 に文字列 | |
sheet.get_cell_mut("A2").set_value("こんにちは、Rust!"); | |
// セル B2 に数値 | |
let mut num = 1011; | |
let mut s = num.to_string(); | |
sheet.get_cell_mut("B2").set_value(&s); | |
num = 1012; | |
s = num.to_string(); | |
sheet.get_cell_mut("B3").set_value(&s); | |
} | |
// 5. 編集した内容で新ファイルに保存 | |
writer::xlsx::write(&book, Path::new("output.xlsx"))?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment