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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! xlsx のシートを JSON で出す。ほかの言語から読むための出し方である
//!
//! シート名を渡すと、そのシートの行を 1 行ずつ JSON にして出す (JSON Lines)。渡さないと、シートの一覧を出す。
//! 形は src/json.rs に書いてある。旧形式の .xls も同じように読める。
//!
//! cargo run --release --example json -- data.xlsx
//! cargo run --release --example json -- data.xlsx Sheet1
use folio::Error;
use folio::book::Book;
use folio::json;
use std::io::{BufWriter, Write};
use std::path::Path;
fn main() {
if let Err(error) = run() {
eprintln!("{error}");
std::process::exit(1);
}
}
fn run() -> Result<(), Error> {
let mut argument = std::env::args().skip(1);
let Some(path) = argument.next() else {
return Err(Error::new("xlsx のパスを渡してください"));
};
let mut book = Book::open(Path::new(&path))?;
// 100 万行のシートでも 1 行ごとに書き込まないよう、まとめて流す
let mut output = BufWriter::new(std::io::stdout().lock());
let Some(want) = argument.next() else {
for one in book.sheet() {
writeln!(output, "{}", json::sheet(one))?;
}
output.flush()?;
return Ok(());
};
let epoch1904 = book.epoch1904();
let mut sheet = book.read(&want)?;
let style = sheet.style();
while let Some(row) = sheet.read()? {
writeln!(output, "{}", json::row(row, &style, epoch1904))?;
}
output.flush()?;
Ok(())
}