rust 我如何使用炉甘石来查找一个单元格的坐标?

jk9hmnmh  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(130)

我正在Rust中重建一个Python项目来帮助我使用Excel。我可以从一个xlsx文件中获得所有值。

use calamine::Reader;
use calamine::open_workbook;
use calamine::Xlsx;
use calamine::DataType;

fn main() {

    let path = "example.xlsx";
    let mut workbook: Xlsx<_> = open_workbook(path).expect("Open Failed");
    let mut rows: usize = 0;
    let mut cols: usize = 0;

    match workbook.worksheet_range("Sheet1") {
        Some(Ok(range)) => {
            rows = range.get_size().0;
            cols = range.get_size().1;
            for row in range.rows() {
                for ele in row {
                    match ele {
                        DataType::Float(value) => print!("{}", value),
                        DataType::String(value) => print!("{}", value),
                        DataType::Error(value) => print!("{}", value),
                        DataType::Bool(value) => print!("{}", value),
                        DataType::DateTime(value) => print!("{}", value),
                        DataType::Empty => print!("/"),
                        DataType::Int(value) => print!("{}", value),
                    }
                    print!(" | ");
                }
                println!("");
            }
        },
        _ => (),
    }

    println!("rows: {}, cols: {}", rows, cols);

}

如何获得坐标为(1,3)的单个单元格的值?
我试着像这样使用get_value(),但是不起作用:

use calamine::{Range, DataType};

let mut range = Range::new((0, 0), (5, 2));
assert_eq!(range.get_value((2, 1)), Some(&DataType::Empty));
range.set_value((2, 1), DataType::Float(1.0));
assert_eq!(range.get_value((2, 1)), Some(&DataType::Float(1.0)));
voj3qocg

voj3qocg1#

我找到了一个办法。

use calamine::{open_workbook, Reader, Xlsx, DataType};

fn main() {
    let file_path = "example.xlsx";
    let mut workbook: Xlsx<_> = open_workbook(file_path).unwrap();

    if let Some(Ok(range)) = workbook.worksheet_range("Sheet1") {
        let row = 1;
        let col = 1;
        let cell = range[row][col].to_owned();

        match cell {
            DataType::Float(value) => println!("{}", value),
            DataType::String(value) => println!("{}", value),
            DataType::Error(value) => println!("{}", value),
            DataType::Bool(value) => println!("{}", value),
            DataType::DateTime(value) => println!("{}", value),
            DataType::Empty => println!("/"),
            DataType::Int(value) => println!("{}", value),
        }
    }
}

相关问题