Skip to content

Commit

Permalink
Merge pull request #6 from sangshuduo/xlsx-tools
Browse files Browse the repository at this point in the history
feat(cli): Add cat_xlsx command with usage information
  • Loading branch information
sangshuduo authored Nov 30, 2024
2 parents a666f9f + b8e9e06 commit fbb14f6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# rust-cli-tools
My CLI tools written by Rust

## cat_xlsx Usage:

```shell
Usage: cat_xlsx <XLSX_FILE>

Arguments:
<XLSX_FILE> Path to the xlsx file

Options:
-h, --help Print help
-V, --version Print version
```
3 changes: 2 additions & 1 deletion cat_xlsx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
calamine = "0.21.0"
clap = { version = "4.1", features = ["derive"] }
calamine = "0.26.1"
49 changes: 35 additions & 14 deletions cat_xlsx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
use calamine::{open_workbook_auto, DataType, Reader};
use std::env;
use calamine::{open_workbook_auto, Data, Reader};
use clap::Parser;
use std::error::Error;
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to the xlsx file
xlsx_file: PathBuf,
}

/// Reads and displays the contents of an Excel (.xlsx) file.
/// Iterates through all worksheets and prints their contents in a tab-separated format.
/// Each worksheet is clearly delimited and labeled.
fn main() -> Result<(), Box<dyn Error>> {
// Get the path to the xlsx file from command-line arguments
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: {} <file.xlsx>", args[0]);
let args = Args::parse();

let path = args.xlsx_file;
// Check if the file exists
if !path.exists() {
eprintln!("Error: File not found");
std::process::exit(1);
}
// Validate file extension
if !path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("xlsx"))
{
eprintln!("Error: File must have .xlsx extension");
std::process::exit(1);
}
let path = &args[1];

// Open the workbook (auto-detects the format)
let mut workbook = open_workbook_auto(path)?;

// Iterate over the worksheets
let sheet_names = workbook.sheet_names().to_owned();
for sheet_name in sheet_names {
if let Some(Ok(range)) = workbook.worksheet_range(&sheet_name) {
if let Ok(range) = workbook.worksheet_range(&sheet_name) {
println!("Sheet: {}", sheet_name);
for row in range.rows() {
for cell in row {
match cell {
DataType::Empty => print!("(empty)\t"),
DataType::String(s) => print!("{}\t", s),
DataType::Float(f) => print!("{}\t", f),
DataType::Int(i) => print!("{}\t", i),
DataType::Bool(b) => print!("{}\t", b),
DataType::Error(e) => print!("Error({:?})\t", e),
DataType::DateTime(dt) => print!("DateTime({})\t", dt),
Data::Empty => print!("(empty)\t"),
Data::String(s) => print!("{}\t", s),
Data::Float(f) => print!("{}\t", f),
Data::Int(i) => print!("{}\t", i),
Data::Bool(b) => print!("{}\t", b),
Data::Error(e) => print!("Error({:?})\t", e),
Data::DateTime(dt) => print!("DateTime({})\t", dt),
_ => print!("(unknown)\t"),
}
}
Expand Down

0 comments on commit fbb14f6

Please sign in to comment.