use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use calamine::{open_workbook_auto, Data, Range, Reader};
fn main() {
let excel_file = env::args()
.nth(1)
.expect("Please provide an excel file to convert");
let sheet_name = env::args()
.nth(2)
.expect("Expecting a sheet name as second argument");
let excel_path = PathBuf::from(excel_file);
match excel_path.extension().and_then(|s| s.to_str()) {
Some("xlsx") | Some("xlsm") | Some("xlsb") | Some("xls") => (),
_ => panic!("Expecting an excel file"),
}
let csv_path = excel_path.with_extension("csv");
let mut csv_file = BufWriter::new(File::create(csv_path).unwrap());
let mut workbook = open_workbook_auto(&excel_path).unwrap();
let range = workbook.worksheet_range(&sheet_name).unwrap();
write_to_csv(&mut csv_file, &range).unwrap();
}
fn write_to_csv<W: Write>(output_file: &mut W, range: &Range<Data>) -> std::io::Result<()> {
let max_column = range.get_size().1 - 1;
for rows in range.rows() {
for (col_number, cell_data) in rows.iter().enumerate() {
match cell_data {
Data::Empty => Ok(()),
Data::Int(i) => write!(output_file, "{i}"),
Data::Bool(b) => write!(output_file, "{b}"),
Data::Error(e) => write!(output_file, "{e:?}"),
Data::Float(f) => write!(output_file, "{f}"),
Data::DateTime(d) => write!(output_file, "{}", d.as_f64()),
Data::String(s) | Data::DateTimeIso(s) | Data::DurationIso(s) => {
write!(output_file, "{s}")
}
}?;
if col_number != max_column {
write!(output_file, ";")?;
}
}
write!(output_file, "\r\n")?;
}
Ok(())
}