Skip to main content

xls_rs/excel/
types.rs

1use anyhow::Result;
2
3/// Cell style configuration
4#[derive(Debug, Clone, Default)]
5pub struct CellStyle {
6    /// Bold text
7    pub bold: bool,
8    /// Italic text
9    pub italic: bool,
10    /// Background color (hex without #, e.g., "4472C4")
11    pub bg_color: Option<String>,
12    /// Font color (hex without #)
13    pub font_color: Option<String>,
14    /// Font size
15    pub font_size: Option<f64>,
16    /// Border style
17    pub border: bool,
18    /// Horizontal alignment
19    pub align: Option<String>,
20    /// Number format (e.g., "0.00", "#,##0", "yyyy-mm-dd")
21    pub number_format: Option<String>,
22}
23
24impl CellStyle {
25    pub fn header() -> Self {
26        Self {
27            bold: true,
28            bg_color: Some("4472C4".to_string()),
29            font_color: Some("FFFFFF".to_string()),
30            border: true,
31            align: Some("center".to_string()),
32            ..Default::default()
33        }
34    }
35
36    /// Parse hex color string to RGB color
37    pub fn parse_hex_color(hex: &str) -> Result<u32> {
38        let hex = hex.trim_start_matches('#');
39        if hex.len() != 6 {
40            anyhow::bail!("Invalid hex color: {}", hex);
41        }
42        let r = u8::from_str_radix(&hex[0..2], 16)?;
43        let g = u8::from_str_radix(&hex[2..4], 16)?;
44        let b = u8::from_str_radix(&hex[4..6], 16)?;
45        Ok(r as u32 * 0x10000 + g as u32 * 0x100 + b as u32)
46    }
47}
48
49/// Options for styled Excel writing
50#[derive(Debug, Clone)]
51pub struct WriteOptions {
52    /// Sheet name
53    pub sheet_name: Option<String>,
54    /// Apply header styling to first row
55    pub style_header: bool,
56    /// Header style
57    pub header_style: CellStyle,
58    /// Column-specific styles (by index)
59    pub column_styles: Option<std::collections::HashMap<usize, CellStyle>>,
60    /// Freeze first row
61    pub freeze_header: bool,
62    /// Enable auto-filter
63    pub auto_filter: bool,
64    /// Auto-fit column widths
65    pub auto_fit: bool,
66}
67
68impl Default for WriteOptions {
69    fn default() -> Self {
70        Self {
71            sheet_name: None,
72            style_header: true,
73            header_style: CellStyle::header(),
74            column_styles: None,
75            freeze_header: false,
76            auto_filter: false,
77            auto_fit: true,
78        }
79    }
80}