diff --git a/examples/excel_to_csv.rs b/examples/excel_to_csv.rs index 8e6d6e85..d6632826 100644 --- a/examples/excel_to_csv.rs +++ b/examples/excel_to_csv.rs @@ -24,9 +24,13 @@ fn main() { let dest = sce.with_extension("csv"); let mut dest = BufWriter::new(File::create(dest).unwrap()); let mut xl = open_workbook_auto(&sce).unwrap(); - let range = xl.worksheet_range(&sheet).unwrap(); - - write_range(&mut dest, &range).unwrap(); + let range = xl.worksheet_range(&sheet); + if range.is_err() { + /// Capture error however you want + std::process::exit(1); + } else { + write_range(&mut dest, &range.unwrap()).unwrap(); + } } fn write_range(dest: &mut W, range: &Range) -> std::io::Result<()> { @@ -36,7 +40,7 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( match *c { Data::Empty => Ok(()), Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => { - write!(dest, "{}", s) + write!(dest, "\"{}\"", s.replace("\"", "\"\"")) } Data::Float(ref f) => write!(dest, "{}", f), Data::DateTime(ref d) => write!(dest, "{}", d.as_f64()), @@ -45,7 +49,7 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( Data::Bool(ref b) => write!(dest, "{}", b), }?; if i != n { - write!(dest, ";")?; + write!(dest, ",")?; } } write!(dest, "\r\n")?; diff --git a/src/xlsx/mod.rs b/src/xlsx/mod.rs index 75863609..ea645963 100644 --- a/src/xlsx/mod.rs +++ b/src/xlsx/mod.rs @@ -808,6 +808,7 @@ impl Xlsx { ) -> Result>, XlsxError> { let mut cell_reader = self.worksheet_cells_reader(name)?; let len = cell_reader.dimensions().len(); + let usize_len: usize = len as usize; let mut cells = Vec::new(); if len < 100_000 { cells.reserve(len as usize); @@ -823,6 +824,13 @@ impl Xlsx { Err(e) => return Err(e), } } + /// Chcek for machine memory error + if cells.capacity() < usize_len { + match cells.try_reserve(len as usize - cells.capacity()) { + Ok(_) => (), + Err(e) => return Err(XlsxError::CellError(e.to_string())), + } + } Ok(Range::from_sparse(cells)) } }