use std::collections::HashMap;
use std::io::BufReader;
use std::sync::Arc;
use chrono::NaiveTime;
use quick_xml::Reader;
use quick_xml::events::{BytesStart, Event};
use crate::column::ColumnDef;
use crate::error::{ExcelError, Result};
use crate::header::{HeaderAnalysis, HeaderMap, resolve_headers};
use crate::model::{RowData, RowImage};
use crate::read::ReadOptions;
use crate::read::images::{
AnchorImage, AnchorIndex, load_media, load_sheet_anchors, load_wps_index, parse_dispimg_id,
};
use crate::read::merges::MergeRange;
use crate::read::sheet_meta::{
Comment, ConditionalFormat, DataValidation, Hyperlink, MetaWants, load_comments,
load_sheet_meta,
};
use crate::read::shared_strings::{SharedStrings, decode_text};
use crate::read::source::{EntryReader, ZipSource};
use crate::read::styles::Styles;
use crate::value::{Cell, CellValue, parse_datetime_text, serial_to_datetime};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Capture {
None,
Value,
Formula,
Inline,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum CellType {
#[default]
Number,
SharedString,
InlineString,
String,
Bool,
Error,
Date,
}
#[derive(Debug, Clone)]
struct ActiveMerge {
range: MergeRange,
value: Option<CellValue>,
}
pub struct SheetStream {
xml: Reader<BufReader<EntryReader>>,
buf: Vec<u8>,
source: ZipSource,
sheet_name: String,
date_1904: bool,
shared: Arc<SharedStrings>,
styles: Arc<Styles>,
defs: Vec<ColumnDef>,
options: ReadOptions,
anchors_by_row: HashMap<u32, Vec<(u16, AnchorImage)>>,
wps_index: Option<HashMap<String, String>>,
pending_dispimg: Vec<(u16, String)>,
merges: Vec<MergeRange>,
merge_cursor: usize,
active_merges: Vec<ActiveMerge>,
current_merges: Vec<MergeRange>,
defaults_by_col: Vec<(u16, String)>,
hyperlinks: Vec<Hyperlink>,
comments: Vec<Comment>,
validations: Vec<DataValidation>,
conditional_formats: Vec<ConditionalFormat>,
current_row: u32,
last_row: u32,
cells: Vec<Cell>,
images: Vec<RowImage>,
cell_active: bool,
cell_col: u16,
cell_last_col: u16,
cell_type: CellType,
cell_style: u32,
value_buf: String,
formula_buf: String,
inline_buf: String,
capture: Capture,
in_inline_str: bool,
header: Option<HeaderMap>,
header_cells: Vec<Vec<String>>,
header_done: bool,
image_cols: Option<Vec<u16>>,
pub rows_scanned: u64,
pub rows_yielded: u64,
pub skipped_rows: u64,
limit_hit: bool,
finished: bool,
}
impl SheetStream {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
source: ZipSource,
sheet_path: &str,
sheet_name: &str,
date_1904: bool,
shared: Arc<SharedStrings>,
styles: Arc<Styles>,
defs: &[ColumnDef],
options: ReadOptions,
) -> Result<Self> {
let entry = source.entry_reader(sheet_path)?;
let xml = Reader::from_reader(BufReader::with_capacity(256 * 1024, entry));
let mut anchors_by_row: HashMap<u32, Vec<(u16, AnchorImage)>> = HashMap::new();
if options.read_images {
let anchors: AnchorIndex = load_sheet_anchors(&source, sheet_path)?;
for ((row, col), anchor) in anchors {
anchors_by_row.entry(row).or_default().push((col, anchor));
}
for list in anchors_by_row.values_mut() {
list.sort_by_key(|(c, _)| *c);
}
}
let meta = load_sheet_meta(
&source,
sheet_path,
MetaWants {
merges: options.read_merges,
hyperlinks: options.read_hyperlinks,
validations: options.read_validations,
conditional_formats: options.read_conditional_formats,
},
)?;
let merges = meta.merges;
let comments = if options.read_comments {
load_comments(&source, sheet_path)?
} else {
Vec::new()
};
Ok(Self {
xml,
buf: Vec::with_capacity(64 * 1024),
source,
sheet_name: sheet_name.to_string(),
date_1904,
shared,
styles,
defs: defs.to_vec(),
options,
anchors_by_row,
wps_index: None,
pending_dispimg: Vec::new(),
merges,
merge_cursor: 0,
active_merges: Vec::new(),
current_merges: Vec::new(),
defaults_by_col: Vec::new(),
hyperlinks: meta.hyperlinks,
comments,
validations: meta.validations,
conditional_formats: meta.conditional_formats,
current_row: 0,
last_row: 0,
cells: Vec::new(),
images: Vec::new(),
cell_active: false,
cell_col: 0,
cell_last_col: 0,
cell_type: CellType::default(),
cell_style: 0,
value_buf: String::new(),
formula_buf: String::new(),
inline_buf: String::new(),
capture: Capture::None,
in_inline_str: false,
header: None,
header_cells: Vec::new(),
header_done: false,
image_cols: None,
rows_scanned: 0,
rows_yielded: 0,
skipped_rows: 0,
limit_hit: false,
finished: false,
})
}
pub fn header(&self) -> Option<&HeaderMap> {
self.header.as_ref()
}
pub fn header_analysis(&self) -> Option<HeaderAnalysis> {
self.header.as_ref().map(|h| h.analysis())
}
pub fn sheet_name(&self) -> &str {
&self.sheet_name
}
pub fn is_finished(&self) -> bool {
self.finished
}
pub fn limit_hit(&self) -> bool {
self.limit_hit
}
pub fn merges(&self) -> &[MergeRange] {
&self.merges
}
pub fn hyperlinks(&self) -> &[Hyperlink] {
&self.hyperlinks
}
pub fn hyperlink_at(&self, row: u32, col: u16) -> Option<&Hyperlink> {
self.hyperlinks.iter().find(|h| h.contains(row, col))
}
pub fn comments(&self) -> &[Comment] {
&self.comments
}
pub fn validations(&self) -> &[DataValidation] {
&self.validations
}
pub fn validation_at(&self, row: u32, col: u16) -> Option<&DataValidation> {
self.validations.iter().find(|v| v.contains(row, col))
}
pub fn conditional_formats(&self) -> &[ConditionalFormat] {
&self.conditional_formats
}
pub fn conditional_format_at(&self, row: u32, col: u16) -> Option<&ConditionalFormat> {
self.conditional_formats.iter().find(|cf| cf.contains(row, col))
}
pub fn comment_at(&self, row: u32, col: u16) -> Option<&Comment> {
self.comments
.iter()
.find(|c| c.row == row && c.col == col)
}
pub fn next_row(&mut self) -> Result<Option<RowData<'_>>> {
if self.finished {
return Ok(None);
}
if let Some(max) = self.options.max_rows {
if self.rows_yielded >= max {
self.limit_hit = true;
self.finished = true;
return Ok(None);
}
}
loop {
if !self.read_next_row_element()? {
if !self.header_done {
self.build_header();
}
self.finished = true;
return Ok(None);
}
self.rows_scanned += 1;
let row_index = self.current_row;
self.apply_merges(row_index);
if !self.header_done {
let start = self.options.header.row_index + 1;
let end = start + self.options.header.row_span.max(1) - 1;
if row_index < start {
continue;
}
if row_index <= end {
self.collect_header_row(row_index - start);
continue;
}
self.build_header();
}
self.apply_defaults(row_index);
self.apply_computed(row_index);
self.collect_row_images(row_index)?;
let keep = match (self.options.row_filter.as_ref(), self.header.as_ref()) {
(Some(filter), Some(header)) => {
let row = RowData {
row_index,
sheet: &self.sheet_name,
header,
cells: &self.cells,
images: &self.images,
merges: &self.current_merges,
styles: self.styles.as_ref(),
hyperlinks: &self.hyperlinks,
comments: &self.comments,
validations: &self.validations,
conditional_formats: &self.conditional_formats,
};
filter.keep(&row)
}
_ => true,
};
if !keep {
self.skipped_rows += 1;
continue;
}
let empty = self.cells.iter().all(|c| c.value.is_empty());
if empty && self.images.is_empty() && self.options.skip_empty_rows {
self.skipped_rows += 1;
continue;
}
self.rows_yielded += 1;
let Some(header) = self.header.as_ref() else {
return Err(ExcelError::Other("工作表表头尚未构建".to_string()));
};
return Ok(Some(RowData {
row_index,
sheet: &self.sheet_name,
header,
cells: &self.cells,
images: &self.images,
merges: &self.current_merges,
styles: self.styles.as_ref(),
hyperlinks: &self.hyperlinks,
comments: &self.comments,
validations: &self.validations,
conditional_formats: &self.conditional_formats,
}));
}
}
fn apply_merges(&mut self, row_index: u32) {
if self.merges.is_empty() {
return; }
while self.merge_cursor < self.merges.len()
&& self.merges[self.merge_cursor].first_row <= row_index
{
let range = self.merges[self.merge_cursor];
self.active_merges.push(ActiveMerge { range, value: None });
self.merge_cursor += 1;
}
self.active_merges.retain(|m| m.range.last_row >= row_index);
if self.active_merges.is_empty() {
self.current_merges.clear();
return;
}
let fill = self.options.fill_merged;
let mut added = false;
let mut active = std::mem::take(&mut self.active_merges);
for m in active.iter_mut() {
if m.range.first_row == row_index && m.value.is_none() {
if let Some(c) = self.cells.iter().find(|c| c.col == m.range.first_col) {
if !c.value.is_empty() {
m.value = Some(c.value.clone());
}
}
}
if !fill {
continue;
}
let Some(value) = m.value.clone() else { continue };
if value.is_empty() {
continue;
}
let start = if m.range.first_row == row_index {
m.range.first_col.saturating_add(1)
} else {
m.range.first_col
};
for col in start..=m.range.last_col {
match self.cells.iter_mut().find(|c| c.col == col) {
Some(c) if c.value.is_empty() => {
c.value = value.clone();
added = true;
}
Some(_) => {}
None => {
self.cells.push(Cell {
row: row_index,
col,
value: value.clone(),
style: 0,
});
added = true;
}
}
}
}
self.active_merges = active;
if added {
self.cells.sort_by_key(|c| c.col);
}
self.current_merges.clear();
self.current_merges
.extend(self.active_merges.iter().map(|m| m.range));
}
fn apply_computed(&mut self, row_index: u32) {
if self.options.computed.is_empty() {
return;
}
let Some(header) = self.header.as_ref() else {
return;
};
let mut values: Vec<(u16, CellValue)> = Vec::with_capacity(self.options.computed.len());
{
let row = RowData {
row_index,
sheet: &self.sheet_name,
header,
cells: &self.cells,
images: &self.images,
merges: &self.current_merges,
styles: self.styles.as_ref(),
hyperlinks: &self.hyperlinks,
comments: &self.comments,
validations: &self.validations,
conditional_formats: &self.conditional_formats,
};
for cc in &self.options.computed {
let Some(col) = row
.header
.file_headers
.iter()
.position(|h| h.as_str() == cc.header())
else {
continue;
};
values.push((col as u16, cc.eval(&row)));
}
}
if values.is_empty() {
return;
}
self.inject_cells(row_index, values);
}
fn apply_defaults(&mut self, row_index: u32) {
if self.defaults_by_col.is_empty() {
return;
}
let mut values: Vec<(u16, CellValue)> = Vec::new();
for (col, text) in &self.defaults_by_col {
let empty = self
.cells
.iter()
.find(|c| c.col == *col)
.map(|c| c.value.is_empty())
.unwrap_or(true);
if empty {
values.push((*col, CellValue::Text(text.clone())));
}
}
if values.is_empty() {
return;
}
self.inject_cells(row_index, values);
}
fn inject_cells(&mut self, row_index: u32, values: Vec<(u16, CellValue)>) {
for (col, value) in values {
match self.cells.iter_mut().find(|c| c.col == col) {
Some(c) => c.value = value,
None => self.cells.push(Cell {
row: row_index,
col,
value,
style: 0,
}),
}
}
self.cells.sort_by_key(|c| c.col);
}
fn read_next_row_element(&mut self) -> Result<bool> {
self.cells.clear();
self.images.clear();
let mut row_seen = false;
loop {
self.buf.clear();
let event = match self.xml.read_event_into(&mut self.buf) {
Ok(Event::Eof) => return Ok(row_seen),
Ok(ev) => ev,
Err(e) => return Err(e.into()),
};
let mut begin: Option<(Option<u16>, CellType, u32)> = None;
let mut cell_end = false;
let mut row_end = false;
match event {
Event::Start(ref e) => match e.local_name().as_ref() {
b"row" => {
row_seen = true;
self.current_row = row_index_of(e).unwrap_or(self.last_row + 1);
self.last_row = self.current_row;
self.cell_last_col = 0;
self.cells.clear();
}
b"c" => begin = Some(parse_cell_attrs(e)),
b"v" => self.capture = Capture::Value,
b"f" => self.capture = Capture::Formula,
b"is" => self.in_inline_str = true,
b"t" if self.in_inline_str => self.capture = Capture::Inline,
_ => {}
},
Event::Empty(ref e) => match e.local_name().as_ref() {
b"row" => {
row_seen = true;
self.current_row = row_index_of(e).unwrap_or(self.last_row + 1);
self.last_row = self.current_row;
self.cell_last_col = 0;
row_end = true;
}
b"c" => {
begin = Some(parse_cell_attrs(e));
cell_end = true;
}
_ => {}
},
Event::End(ref e) => match e.local_name().as_ref() {
b"row" => row_end = true,
b"c" => cell_end = true,
b"v" | b"f" => self.capture = Capture::None,
b"is" => {
self.in_inline_str = false;
self.capture = Capture::None;
}
b"t" if self.in_inline_str => self.capture = Capture::None,
_ => {}
},
Event::Text(ref t) => match self.capture {
Capture::Value => self.value_buf.push_str(&decode_text(t)),
Capture::Formula => self.formula_buf.push_str(&decode_text(t)),
Capture::Inline => self.inline_buf.push_str(&decode_text(t)),
Capture::None => {}
},
Event::CData(ref t) => match self.capture {
Capture::Value => self
.value_buf
.push_str(&String::from_utf8_lossy(t.as_ref())),
Capture::Formula => self
.formula_buf
.push_str(&String::from_utf8_lossy(t.as_ref())),
Capture::Inline => self
.inline_buf
.push_str(&String::from_utf8_lossy(t.as_ref())),
Capture::None => {}
},
_ => {}
}
if let Some((col, cell_type, style)) = begin {
self.begin_cell(col, cell_type, style);
}
if cell_end {
self.finish_cell();
}
if row_end {
return Ok(true);
}
}
}
fn begin_cell(&mut self, col: Option<u16>, cell_type: CellType, style: u32) {
self.cell_active = true;
self.cell_type = cell_type;
self.cell_style = style;
self.cell_col = col.unwrap_or_else(|| {
if self.cells.is_empty() {
0
} else {
self.cell_last_col + 1
}
});
self.cell_last_col = self.cell_col;
self.value_buf.clear();
self.formula_buf.clear();
self.inline_buf.clear();
self.capture = Capture::None;
}
fn finish_cell(&mut self) {
if !self.cell_active {
return;
}
self.cell_active = false;
let cell_type = self.cell_type;
let style = self.cell_style;
let mut value = match cell_type {
CellType::SharedString => match self.value_buf.trim().parse::<usize>() {
Ok(idx) => CellValue::Text(self.shared.get(idx).unwrap_or_default().to_string()),
Err(_) => CellValue::Text(String::new()),
},
CellType::InlineString => CellValue::Text(std::mem::take(&mut self.inline_buf)),
CellType::String => CellValue::Text(std::mem::take(&mut self.value_buf)),
CellType::Bool => CellValue::Bool(matches!(self.value_buf.trim(), "1" | "true" | "TRUE")),
CellType::Error => CellValue::Error(std::mem::take(&mut self.value_buf)),
CellType::Date => match parse_datetime_text(self.value_buf.trim()) {
Some(dt) => CellValue::DateTime(dt),
None => CellValue::Text(std::mem::take(&mut self.value_buf)),
},
CellType::Number => {
if self.value_buf.trim().is_empty() {
if self.formula_buf.is_empty() {
CellValue::Empty
} else {
CellValue::Formula(std::mem::take(&mut self.formula_buf))
}
} else {
match self.value_buf.trim().parse::<f64>() {
Ok(n) => self.number_value(n, style),
Err(_) => CellValue::Text(std::mem::take(&mut self.value_buf)),
}
}
}
};
if !self.formula_buf.is_empty() {
if let Some(id) = parse_dispimg_id(&self.formula_buf) {
self.pending_dispimg.push((self.cell_col, id));
value = CellValue::Text(std::mem::take(&mut self.formula_buf));
}
}
if let CellValue::Text(s) = &mut value {
if self.options.trim_text {
let trimmed = s.trim();
if trimmed.len() != s.len() {
*s = trimmed.to_string();
}
}
}
self.cells.push(Cell {
row: self.current_row,
col: self.cell_col,
value,
style,
});
}
fn number_value(&self, n: f64, style: u32) -> CellValue {
if self.styles.is_date(style) {
if let Some(dt) = serial_to_datetime(n, self.date_1904) {
let midnight = NaiveTime::from_hms_opt(0, 0, 0).unwrap();
return if dt.time() == midnight {
CellValue::Date(dt.date())
} else {
CellValue::DateTime(dt)
};
}
}
CellValue::Number(n)
}
fn collect_header_row(&mut self, span_index: u32) {
let span = self.options.header.row_span.max(1) as usize;
let span_index = span_index as usize;
let mut row_cells: Vec<(u16, String)> = self
.cells
.iter()
.map(|c| (c.col, c.text()))
.filter(|(_, t)| !t.is_empty())
.collect();
row_cells.sort_by_key(|(c, _)| *c);
for (col, text) in row_cells {
let col = col as usize;
if self.header_cells.len() <= col {
self.header_cells.resize(col + 1, vec![String::new(); span]);
}
if let Some(slot) = self.header_cells[col].get_mut(span_index) {
*slot = text;
}
}
}
fn build_header(&mut self) {
let span = self.options.header.row_span.max(1) as usize;
let mut file_headers: Vec<String> = Vec::with_capacity(self.header_cells.len());
for col in &self.header_cells {
let parts: Vec<String> = (0..span)
.filter_map(|i| col.get(i))
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
file_headers.push(parts.join(" "));
}
while matches!(file_headers.last(), Some(h) if h.trim().is_empty()) {
file_headers.pop();
}
for cc in &self.options.computed {
if !file_headers.iter().any(|h| h.as_str() == cc.header()) {
file_headers.push(cc.header().to_string());
}
}
let map = if self.defs.is_empty() {
HeaderMap::dynamic(file_headers)
} else {
resolve_headers(&file_headers, &self.defs, &self.options.header)
};
self.image_cols = match &self.options.image_columns {
Some(cols) => Some(cols.clone()),
None => {
if self.defs.is_empty() {
None
} else if self.defs.iter().any(|d| d.image) {
Some(
self.defs
.iter()
.filter(|d| d.image)
.filter_map(|d| map.index_of(&d.header))
.collect(),
)
} else {
Some(Vec::new())
}
}
};
self.defaults_by_col = self
.defs
.iter()
.filter(|d| d.is_importable())
.filter_map(|d| d.default.as_ref().map(|v| (d, v)))
.filter_map(|(d, v)| map.index_of(&d.header).map(|col| (col, v.clone())))
.collect();
self.header = Some(map);
self.header_done = true;
}
fn ensure_wps_index(&mut self) -> Result<()> {
if self.wps_index.is_none() {
let index = load_wps_index(&self.source)?;
self.wps_index = Some(index);
}
Ok(())
}
fn collect_row_images(&mut self, row_index: u32) -> Result<()> {
self.images.clear();
if !self.options.read_images {
self.pending_dispimg.clear();
return Ok(());
}
let anchor_row = row_index.saturating_sub(1);
let mut found: Vec<(u16, String, Option<String>)> = Vec::new();
if let Some(list) = self.anchors_by_row.get(&anchor_row) {
for (col, anchor) in list {
let name = anchor
.name
.clone()
.or_else(|| anchor.media.rsplit('/').next().map(|s| s.to_string()));
found.push((*col, anchor.media.clone(), name));
}
}
if !self.pending_dispimg.is_empty() {
let pendings: Vec<(u16, String)> = self.pending_dispimg.drain(..).collect();
if !pendings.is_empty() {
self.ensure_wps_index()?;
}
if let Some(index) = self.wps_index.as_ref() {
for (col, id) in pendings {
if found.iter().any(|(c, _, _)| *c == col) {
continue;
}
if let Some(media) = index.get(&id) {
found.push((col, media.clone(), Some(format!("DISPIMG:{id}"))));
}
}
}
}
found.sort_by_key(|(c, _, _)| *c);
let filter = self.image_cols.clone();
for (col, media, name) in found {
if let Some(cols) = &filter {
if !cols.contains(&col) {
continue;
}
}
match load_media(&self.source, &media) {
Ok(img) => {
let header = self.header_name_of(col);
self.images.push(RowImage {
column: col,
header,
bytes: img.bytes,
ext: img.ext,
name,
});
}
Err(e) => log::warn!("读取内嵌图片失败({media}):{e}"),
}
}
Ok(())
}
fn header_name_of(&self, col: u16) -> String {
if let Some(header) = &self.header {
if let Some(c) = header.columns.iter().find(|c| c.file_index == Some(col)) {
return c.header.clone();
}
if let Some(h) = header.file_headers.get(col as usize) {
if !h.is_empty() {
return h.clone();
}
}
}
format!("第{}列", crate::header::excel_column_name(col))
}
}
fn parse_cell_attrs(e: &BytesStart<'_>) -> (Option<u16>, CellType, u32) {
let mut col: Option<u16> = None;
let mut cell_type = CellType::default();
let mut style: u32 = 0;
for a in e.attributes().flatten() {
match a.key.as_ref() {
b"r" => col = col_from_ref(&String::from_utf8_lossy(&a.value)),
b"t" => {
cell_type = match a.value.as_ref() {
b"s" => CellType::SharedString,
b"inlineStr" => CellType::InlineString,
b"str" => CellType::String,
b"b" => CellType::Bool,
b"e" => CellType::Error,
b"d" => CellType::Date,
_ => CellType::Number,
}
}
b"s" => {
style = String::from_utf8_lossy(&a.value)
.trim()
.parse()
.unwrap_or(0)
}
_ => {}
}
}
(col, cell_type, style)
}
fn row_index_of(e: &BytesStart<'_>) -> Option<u32> {
for a in e.attributes().flatten() {
if a.key.as_ref() == b"r" {
return String::from_utf8_lossy(&a.value).trim().parse().ok();
}
}
None
}
pub(crate) fn col_from_ref(r: &str) -> Option<u16> {
let mut n: u32 = 0;
let mut has = false;
for c in r.chars() {
if c.is_ascii_alphabetic() {
has = true;
n = n * 26 + (c.to_ascii_uppercase() as u32 - 'A' as u32 + 1);
} else {
break;
}
}
if has && n > 0 {
Some((n - 1) as u16)
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_col_from_ref() {
assert_eq!(col_from_ref("A1"), Some(0));
assert_eq!(col_from_ref("B12"), Some(1));
assert_eq!(col_from_ref("Z1"), Some(25));
assert_eq!(col_from_ref("AA1"), Some(26));
assert_eq!(col_from_ref("AB100"), Some(27));
assert_eq!(col_from_ref("1"), None);
}
}