fix(export): preserve datetime precision across CSV and XLSX exports
This commit is contained in:
1 parent
ffe1e58fbb
commit
ab6a7645f6
14 files changed
+438
-43
No files matched your search
@@ -907,10 +907,11 @@ async fn export_query_result_core_inner(
|
||||
result.rows.truncate(this_page);
|
||||
}
|
||||
let row_count = result.rows.len();
|
||||
let formatted_rows = crate::temporal_format::format_temporal_export_rows_with_string_types_cow(
|
||||
let formatted_rows = crate::temporal_format::format_temporal_export_rows_with_string_types_for_csv_cow(
|
||||
&result.rows,
|
||||
&column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
format == "csv",
|
||||
);
|
||||
|
||||
if format == "csv" || format == "txt" {
|
||||
@@ -1129,10 +1130,11 @@ async fn try_export_postgres_query_result_stream(
|
||||
}
|
||||
}
|
||||
crate::db::postgres::PostgresQueryStreamItem::Row(row) => {
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_cow(
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_for_csv_cow(
|
||||
&row,
|
||||
&temporal_column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
format == "csv",
|
||||
);
|
||||
if let Some(writer) = sql_writer.as_mut() {
|
||||
writer.write_row(formatted.into_owned(), None)?;
|
||||
@@ -1370,10 +1372,11 @@ async fn try_export_mysql_query_result_stream(
|
||||
}
|
||||
}
|
||||
crate::db::mysql::MySqlQueryStreamItem::Row(row) => {
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_cow(
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_for_csv_cow(
|
||||
&row,
|
||||
&temporal_column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
format == "csv",
|
||||
);
|
||||
if let Some(writer) = sql_writer.as_mut() {
|
||||
writer.write_row(formatted.into_owned(), None)?;
|
||||
@@ -1590,10 +1593,11 @@ async fn try_export_clickhouse_query_result_stream(
|
||||
}
|
||||
}
|
||||
crate::db::clickhouse_driver::ClickHouseQueryStreamItem::Row(row) => {
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_cow(
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_for_csv_cow(
|
||||
&row,
|
||||
&temporal_column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
format == "csv",
|
||||
);
|
||||
if let Some(writer) = sql_writer.as_mut() {
|
||||
writer.write_row(formatted.into_owned(), None)?;
|
||||
@@ -1775,10 +1779,11 @@ async fn try_export_sqlserver_query_result_stream(
|
||||
}
|
||||
}
|
||||
crate::db::sqlserver::SqlServerStreamItem::Row(row) => {
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_cow(
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_with_string_types_for_csv_cow(
|
||||
row,
|
||||
&temporal_column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
format == "csv",
|
||||
);
|
||||
if let Some(writer) = sql_writer.as_mut() {
|
||||
writer.write_row(formatted.into_owned(), None)?;
|
||||
|
||||
@@ -1010,10 +1010,11 @@ async fn try_export_native_table_stream(
|
||||
&cancelled,
|
||||
cancel_token.clone(),
|
||||
|row| {
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_cow(
|
||||
let formatted = crate::temporal_format::format_temporal_export_row_for_csv_cow(
|
||||
row,
|
||||
column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
request.format.eq_ignore_ascii_case("csv"),
|
||||
);
|
||||
write_table_text_row(&mut file, true, formatted.as_ref(), &mut row_buffer, request.csv_quote_mode)?;
|
||||
rows_exported += 1;
|
||||
@@ -1616,10 +1617,11 @@ async fn export_table_data_core_inner(
|
||||
if row_count == 0 {
|
||||
break;
|
||||
}
|
||||
let formatted_rows = crate::temporal_format::format_temporal_export_rows_cow(
|
||||
let formatted_rows = crate::temporal_format::format_temporal_export_rows_for_csv_cow(
|
||||
&result.rows,
|
||||
&column_types,
|
||||
request.date_time_format.as_deref(),
|
||||
request.format.eq_ignore_ascii_case("csv"),
|
||||
);
|
||||
|
||||
if is_first_batch {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
|
||||
use chrono::{DateTime, Datelike, FixedOffset, Local, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
|
||||
use serde_json::Value;
|
||||
use std::borrow::Cow;
|
||||
use std::fmt::Write as _;
|
||||
@@ -122,6 +122,31 @@ fn parse_known_temporal(value: &str) -> Option<ParsedTemporal> {
|
||||
None
|
||||
}
|
||||
|
||||
/// A driver may hand back a bare epoch integer for a column it still reports as
|
||||
/// a temporal type — IoTDB's `TIMESTAMP(ms)` does exactly that. The data grid's
|
||||
/// export formatter already renders those (`columnFormatter.ts`,
|
||||
/// `unit: "auto"`), which is why "export current page" shows a real date while
|
||||
/// the Rust streaming exporters used to write the raw epoch. Mirror that
|
||||
/// formatter's rule exactly so both paths agree: accept only 10 digits
|
||||
/// (seconds) or 13 digits (milliseconds), require the result to land in
|
||||
/// 1970..=2100, and render in the local zone the same way `dayjs(ms).format()`
|
||||
/// does. Widening this (microseconds, nanoseconds, other digit counts) would
|
||||
/// re-introduce the mismatch in the opposite direction.
|
||||
fn parse_epoch_temporal(value: &str) -> Option<NaiveDateTime> {
|
||||
let digits = value.strip_prefix('-').unwrap_or(value);
|
||||
if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
let parsed = value.parse::<i64>().ok()?;
|
||||
let milliseconds = match digits.len() {
|
||||
10 => parsed.checked_mul(1000)?,
|
||||
13 => parsed,
|
||||
_ => return None,
|
||||
};
|
||||
let local = DateTime::from_timestamp_millis(milliseconds)?.with_timezone(&Local).naive_local();
|
||||
(1970..=2100).contains(&local.year()).then_some(local)
|
||||
}
|
||||
|
||||
fn parse_temporal(value: &str, preferred_pattern: Option<&str>) -> Option<ParsedTemporal> {
|
||||
preferred_pattern
|
||||
.filter(|pattern| !pattern.trim().is_empty())
|
||||
@@ -141,7 +166,8 @@ pub(crate) fn excel_temporal_serial(
|
||||
// timezone-bearing and time-only values as text.
|
||||
TemporalKind::DateTimeWithTimeZone | TemporalKind::Time => return None,
|
||||
};
|
||||
let parsed = parse_temporal(value.trim(), preferred_pattern)?;
|
||||
let parsed = parse_temporal(value.trim(), preferred_pattern)
|
||||
.or_else(|| parse_epoch_temporal(value.trim()).map(ParsedTemporal::DateTime))?;
|
||||
let (date, time) = match (kind, parsed) {
|
||||
(ExcelTemporalKind::Date, ParsedTemporal::Date(date)) => (date, NaiveTime::default()),
|
||||
(ExcelTemporalKind::Date, ParsedTemporal::DateTime(value)) => (value.date(), value.time()),
|
||||
@@ -188,6 +214,7 @@ pub fn format_temporal_export_value(value: &Value, data_type: Option<&str>, patt
|
||||
return value.clone();
|
||||
};
|
||||
parse_known_temporal(raw)
|
||||
.or_else(|| parse_epoch_temporal(raw.trim()).map(ParsedTemporal::DateTime))
|
||||
.and_then(|parsed| format_parsed(parsed, pattern))
|
||||
.map(Value::String)
|
||||
.unwrap_or_else(|| value.clone())
|
||||
@@ -301,6 +328,125 @@ pub fn format_temporal_export_rows_with_string_types(
|
||||
format_temporal_export_rows_with_string_types_cow(rows, column_types, pattern).into_owned()
|
||||
}
|
||||
|
||||
/// CSV is untyped text: spreadsheet apps (WPS/Excel/OnlyOffice) re-guess a
|
||||
/// quoted date-looking cell's type on open regardless of RFC4180 quoting,
|
||||
/// silently truncating/reformatting it (dropping the date part or the
|
||||
/// milliseconds). Wrapping the value as an `="..."` formula is the standard
|
||||
/// cross-app way to force text interpretation; the existing CSV
|
||||
/// quoting/escaping (which doubles embedded `"`) turns it into valid CSV for
|
||||
/// free. Applied whenever the column is a temporal type, independent of
|
||||
/// whether `pattern` re-formats the value or the raw driver string passes
|
||||
/// through unchanged (`保持数据库原始值`) — both shapes are equally prone to
|
||||
/// spreadsheet mis-parsing.
|
||||
fn wrap_csv_force_text(value: Value) -> Value {
|
||||
match value {
|
||||
Value::String(text) => Value::String(format!("=\"{text}\"")),
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_temporal_export_row_for_csv_cow<'a>(
|
||||
row: &'a [Value],
|
||||
column_types: &[Option<String>],
|
||||
pattern: Option<&str>,
|
||||
force_csv_text: bool,
|
||||
) -> Cow<'a, [Value]> {
|
||||
if !force_csv_text {
|
||||
return format_temporal_export_row_cow(row, column_types, pattern);
|
||||
}
|
||||
if !column_types.iter().any(|data_type| temporal_kind(data_type.as_deref()).is_some()) {
|
||||
return Cow::Borrowed(row);
|
||||
}
|
||||
let active_pattern = active_temporal_pattern(pattern);
|
||||
Cow::Owned(
|
||||
row.iter()
|
||||
.enumerate()
|
||||
.map(|(index, value)| {
|
||||
let data_type = column_types.get(index).and_then(|data_type| data_type.as_deref());
|
||||
if temporal_kind(data_type).is_none() {
|
||||
return value.clone();
|
||||
}
|
||||
let value = match active_pattern {
|
||||
Some(pattern) => format_temporal_export_value(value, data_type, Some(pattern)),
|
||||
None => value.clone(),
|
||||
};
|
||||
wrap_csv_force_text(value)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_temporal_export_rows_for_csv_cow<'a>(
|
||||
rows: &'a [Vec<Value>],
|
||||
column_types: &[Option<String>],
|
||||
pattern: Option<&str>,
|
||||
force_csv_text: bool,
|
||||
) -> Cow<'a, [Vec<Value>]> {
|
||||
if !force_csv_text {
|
||||
return format_temporal_export_rows_cow(rows, column_types, pattern);
|
||||
}
|
||||
if !column_types.iter().any(|data_type| temporal_kind(data_type.as_deref()).is_some()) {
|
||||
return Cow::Borrowed(rows);
|
||||
}
|
||||
Cow::Owned(
|
||||
rows.iter()
|
||||
.map(|row| format_temporal_export_row_for_csv_cow(row, column_types, pattern, true).into_owned())
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_temporal_export_row_with_string_types_for_csv_cow<'a>(
|
||||
row: &'a [Value],
|
||||
column_types: &[String],
|
||||
pattern: Option<&str>,
|
||||
force_csv_text: bool,
|
||||
) -> Cow<'a, [Value]> {
|
||||
if !force_csv_text {
|
||||
return format_temporal_export_row_with_string_types_cow(row, column_types, pattern);
|
||||
}
|
||||
if !column_types.iter().any(|data_type| temporal_kind(Some(data_type)).is_some()) {
|
||||
return Cow::Borrowed(row);
|
||||
}
|
||||
let active_pattern = active_temporal_pattern(pattern);
|
||||
Cow::Owned(
|
||||
row.iter()
|
||||
.enumerate()
|
||||
.map(|(index, value)| {
|
||||
let data_type = column_types.get(index).map(String::as_str);
|
||||
if temporal_kind(data_type).is_none() {
|
||||
return value.clone();
|
||||
}
|
||||
let value = match active_pattern {
|
||||
Some(pattern) => format_temporal_export_value(value, data_type, Some(pattern)),
|
||||
None => value.clone(),
|
||||
};
|
||||
wrap_csv_force_text(value)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_temporal_export_rows_with_string_types_for_csv_cow<'a>(
|
||||
rows: &'a [Vec<Value>],
|
||||
column_types: &[String],
|
||||
pattern: Option<&str>,
|
||||
force_csv_text: bool,
|
||||
) -> Cow<'a, [Vec<Value>]> {
|
||||
if !force_csv_text {
|
||||
return format_temporal_export_rows_with_string_types_cow(rows, column_types, pattern);
|
||||
}
|
||||
if !column_types.iter().any(|data_type| temporal_kind(Some(data_type)).is_some()) {
|
||||
return Cow::Borrowed(rows);
|
||||
}
|
||||
Cow::Owned(
|
||||
rows.iter()
|
||||
.map(|row| {
|
||||
format_temporal_export_row_with_string_types_for_csv_cow(row, column_types, pattern, true).into_owned()
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_temporal_import_value_cow<'a>(
|
||||
value: &'a Value,
|
||||
data_type: Option<&str>,
|
||||
@@ -362,6 +508,49 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn formats_driver_epoch_timestamps_like_the_current_page_export() {
|
||||
// IoTDB hands back `strconv.FormatInt(epoch, 10)` for a TIMESTAMP(ms)
|
||||
// column, so the streaming exporters used to write the raw epoch while
|
||||
// "export current page" (formatted in the frontend) showed a real date.
|
||||
let pattern = Some("YYYY-MM-DD HH:mm:ss.SSS");
|
||||
let formatted = format_temporal_export_value(&json!("1785042135456"), Some("TIMESTAMP(ms)"), pattern);
|
||||
let rendered = formatted.as_str().expect("formatted value stays a string");
|
||||
assert_ne!(rendered, "1785042135456", "epoch must not pass through unformatted");
|
||||
assert!(rendered.ends_with(":15.456"), "milliseconds must survive: {rendered}");
|
||||
|
||||
// Ten digits are seconds, matching columnFormatter.ts `unit: "auto"`.
|
||||
let seconds = format_temporal_export_value(&json!("1785042135"), Some("TIMESTAMP(ms)"), pattern);
|
||||
assert_eq!(seconds.as_str().expect("string").get(..4), rendered.get(..4));
|
||||
|
||||
// The CSV force-text wrapper now wraps a real date instead of an epoch.
|
||||
let rows = vec![vec![json!("1785042135456"), json!("device-a")]];
|
||||
let types = vec!["TIMESTAMP(ms)".to_string(), "TEXT".to_string()];
|
||||
let wrapped = format_temporal_export_rows_with_string_types_for_csv_cow(&rows, &types, pattern, true);
|
||||
let cell = wrapped[0][0].as_str().expect("string");
|
||||
assert!(cell.starts_with("=\"") && cell.ends_with(":15.456\""), "cell={cell}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_non_auto_epoch_shapes_untouched() {
|
||||
let pattern = Some("YYYY-MM-DD HH:mm:ss.SSS");
|
||||
// Microsecond/nanosecond epochs are outside the shapes the data grid
|
||||
// formatter accepts; formatting them here would make the streaming
|
||||
// export disagree with "export current page" in the other direction.
|
||||
for raw in ["1785042135456789", "1785042135456789012", "17850421", "not-a-number"] {
|
||||
assert_eq!(
|
||||
format_temporal_export_value(&json!(raw), Some("TIMESTAMP(ms)"), pattern),
|
||||
json!(raw),
|
||||
"raw={raw}"
|
||||
);
|
||||
}
|
||||
// A plain integer column is not temporal, so it is never touched.
|
||||
assert_eq!(
|
||||
format_temporal_export_value(&json!("1785042135456"), Some("BIGINT"), pattern),
|
||||
json!("1785042135456")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_unpadded_slash_dates_for_import() {
|
||||
assert_eq!(
|
||||
@@ -391,6 +580,59 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_export_wraps_formatted_temporal_values_as_force_text_formulas() {
|
||||
let row = vec![json!(1), json!("2024-02-25 13:02:15"), json!("plain text")];
|
||||
let column_types = [Some("NUMBER".into()), Some("TIMESTAMP".into()), Some("VARCHAR2".into())];
|
||||
assert_eq!(
|
||||
format_temporal_export_row_for_csv_cow(&row, &column_types, Some("YYYY/M/D HH:mm:ss"), true).into_owned(),
|
||||
vec![json!(1), json!("=\"2024/2/25 13:02:15\""), json!("plain text")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_export_wraps_raw_temporal_values_even_without_a_configured_pattern() {
|
||||
// "保持数据库原始值": no export pattern configured, but the raw driver
|
||||
// string is still date-shaped and equally prone to spreadsheet
|
||||
// mis-parsing, so the force-text wrap must still apply.
|
||||
let row = vec![json!("2024-02-25 13:02:15.000")];
|
||||
let column_types = [Some("DATETIME".into())];
|
||||
assert_eq!(
|
||||
format_temporal_export_row_for_csv_cow(&row, &column_types, None, true).into_owned(),
|
||||
vec![json!("=\"2024-02-25 13:02:15.000\"")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_export_does_not_wrap_null_temporal_values() {
|
||||
let row = vec![json!(Value::Null)];
|
||||
let column_types = [Some("DATE".into())];
|
||||
assert_eq!(
|
||||
format_temporal_export_row_for_csv_cow(&row, &column_types, None, true).into_owned(),
|
||||
vec![json!(Value::Null)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_export_force_text_disabled_matches_plain_temporal_formatting() {
|
||||
let row = vec![json!("2024-02-25 13:02:15")];
|
||||
let column_types = [Some("TIMESTAMP".into())];
|
||||
assert_eq!(
|
||||
format_temporal_export_row_for_csv_cow(&row, &column_types, Some("YYYY/M/D HH:mm:ss"), false).into_owned(),
|
||||
format_temporal_export_row(&row, &column_types, Some("YYYY/M/D HH:mm:ss"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_export_with_string_types_wraps_temporal_values() {
|
||||
let row = vec![json!("2024-02-25"), json!(42)];
|
||||
let column_types = ["DATE".to_string(), "INT".to_string()];
|
||||
assert_eq!(
|
||||
format_temporal_export_row_with_string_types_for_csv_cow(&row, &column_types, None, true).into_owned(),
|
||||
vec![json!("=\"2024-02-25\""), json!(42)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_raw_export_values_when_pattern_requires_missing_fields() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -451,7 +451,7 @@ impl<W: Write + Seek> StreamingXlsxWriter<W> {
|
||||
rows: &sheet.rows,
|
||||
numeric_column_right_align: sheet.numeric_column_right_align,
|
||||
};
|
||||
write_worksheet_xml(&mut self.zip, &segment, self.auto_filter)?;
|
||||
write_worksheet_xml(&mut self.zip, &segment, self.auto_filter, self.date_time_format.as_deref())?;
|
||||
}
|
||||
|
||||
// 3. Write metadata files. These appear AFTER sheet data in the ZIP
|
||||
@@ -779,7 +779,12 @@ fn push_typed_cell_xml(
|
||||
push_cell_xml(output, value, row_index, col_index, style);
|
||||
}
|
||||
|
||||
fn write_worksheet_xml<W: Write>(writer: &mut W, segment: &WorksheetSegment, auto_filter: bool) -> Result<(), String> {
|
||||
fn write_worksheet_xml<W: Write>(
|
||||
writer: &mut W,
|
||||
segment: &WorksheetSegment,
|
||||
auto_filter: bool,
|
||||
date_time_format: Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
let total_rows = segment.rows.len() + 1;
|
||||
let range = sheet_range(segment.columns.len(), total_rows);
|
||||
let widths = estimate_column_widths(segment.columns, segment.column_comments, segment.rows);
|
||||
@@ -816,7 +821,7 @@ fn write_worksheet_xml<W: Write>(writer: &mut W, segment: &WorksheetSegment, aut
|
||||
segment.columns,
|
||||
segment.column_types,
|
||||
row,
|
||||
None,
|
||||
date_time_format,
|
||||
segment.numeric_column_right_align,
|
||||
);
|
||||
writer.write_all(row_buffer.as_bytes()).map_err(|err| err.to_string())?;
|
||||
@@ -1081,25 +1086,30 @@ fn split_sheets_for_max_rows<'a>(
|
||||
}
|
||||
|
||||
pub fn build_xlsx_workbook(data: &XlsxWorksheetData) -> Result<Vec<u8>, String> {
|
||||
build_xlsx_workbook_with_auto_filter(data, true)
|
||||
build_xlsx_workbook_with_auto_filter(data, true, None)
|
||||
}
|
||||
|
||||
pub fn build_xlsx_workbook_multi(sheets: &[XlsxWorksheetData]) -> Result<Vec<u8>, String> {
|
||||
build_xlsx_workbook_multi_with_auto_filter(sheets, true)
|
||||
build_xlsx_workbook_multi_with_auto_filter(sheets, true, None)
|
||||
}
|
||||
|
||||
pub fn build_xlsx_workbook_with_auto_filter(data: &XlsxWorksheetData, auto_filter: bool) -> Result<Vec<u8>, String> {
|
||||
build_xlsx_workbook_multi_with_auto_filter(std::slice::from_ref(data), auto_filter)
|
||||
pub fn build_xlsx_workbook_with_auto_filter(
|
||||
data: &XlsxWorksheetData,
|
||||
auto_filter: bool,
|
||||
date_time_format: Option<&str>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
build_xlsx_workbook_multi_with_auto_filter(std::slice::from_ref(data), auto_filter, date_time_format)
|
||||
}
|
||||
|
||||
pub fn build_xlsx_workbook_multi_with_auto_filter(
|
||||
sheets: &[XlsxWorksheetData],
|
||||
auto_filter: bool,
|
||||
date_time_format: Option<&str>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
if auto_filter {
|
||||
return build_xlsx_workbook_multi_with_max_rows(sheets, XLSX_MAX_DATA_ROWS);
|
||||
return build_xlsx_workbook_multi_with_max_rows(sheets, XLSX_MAX_DATA_ROWS, date_time_format);
|
||||
}
|
||||
build_xlsx_workbook_multi_with_max_rows_and_auto_filter(sheets, XLSX_MAX_DATA_ROWS, auto_filter)
|
||||
build_xlsx_workbook_multi_with_max_rows_and_auto_filter(sheets, XLSX_MAX_DATA_ROWS, auto_filter, date_time_format)
|
||||
}
|
||||
|
||||
/// Build an in-memory XLSX workbook with an explicit per-sheet data-row limit.
|
||||
@@ -1109,14 +1119,16 @@ pub fn build_xlsx_workbook_multi_with_auto_filter(
|
||||
pub(crate) fn build_xlsx_workbook_multi_with_max_rows(
|
||||
sheets: &[XlsxWorksheetData],
|
||||
max_data_rows_per_sheet: usize,
|
||||
date_time_format: Option<&str>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
build_xlsx_workbook_multi_with_max_rows_and_auto_filter(sheets, max_data_rows_per_sheet, true)
|
||||
build_xlsx_workbook_multi_with_max_rows_and_auto_filter(sheets, max_data_rows_per_sheet, true, date_time_format)
|
||||
}
|
||||
|
||||
fn build_xlsx_workbook_multi_with_max_rows_and_auto_filter(
|
||||
sheets: &[XlsxWorksheetData],
|
||||
max_data_rows_per_sheet: usize,
|
||||
auto_filter: bool,
|
||||
date_time_format: Option<&str>,
|
||||
) -> Result<Vec<u8>, String> {
|
||||
if sheets.is_empty() {
|
||||
return Err("At least one worksheet is required".to_string());
|
||||
@@ -1128,7 +1140,7 @@ fn build_xlsx_workbook_multi_with_max_rows_and_auto_filter(
|
||||
("_rels/.rels", root_rels_xml().to_string()),
|
||||
("xl/workbook.xml", workbook_xml_for_sheets(&sheet_names)),
|
||||
("xl/_rels/workbook.xml.rels", workbook_rels_xml_for_sheet_count(segments.len())),
|
||||
("xl/styles.xml", styles_xml(None)),
|
||||
("xl/styles.xml", styles_xml(date_time_format)),
|
||||
];
|
||||
|
||||
let cursor = Cursor::new(Vec::<u8>::new());
|
||||
@@ -1141,7 +1153,7 @@ fn build_xlsx_workbook_multi_with_max_rows_and_auto_filter(
|
||||
}
|
||||
for (index, segment) in segments.iter().enumerate() {
|
||||
zip.start_file(format!("xl/worksheets/sheet{}.xml", index + 1), options).map_err(|err| err.to_string())?;
|
||||
write_worksheet_xml(&mut zip, segment, auto_filter)?;
|
||||
write_worksheet_xml(&mut zip, segment, auto_filter, date_time_format)?;
|
||||
}
|
||||
|
||||
let output = zip.finish().map_err(|err| err.to_string())?;
|
||||
@@ -1152,9 +1164,10 @@ fn build_xlsx_workbook_multi_with_max_rows_and_auto_filter(
|
||||
mod tests {
|
||||
use super::{
|
||||
build_xlsx_workbook, build_xlsx_workbook_multi, build_xlsx_workbook_multi_with_auto_filter,
|
||||
build_xlsx_workbook_multi_with_max_rows, is_numeric_column_type, start_streaming_xlsx_workbook,
|
||||
start_streaming_xlsx_workbook_with_max_rows, start_streaming_xlsx_workbook_with_options,
|
||||
start_streaming_xlsx_workbook_with_trailing_sheets, write_worksheet_xml, WorksheetSegment, XlsxWorksheetData,
|
||||
build_xlsx_workbook_multi_with_max_rows, build_xlsx_workbook_with_auto_filter, is_numeric_column_type,
|
||||
start_streaming_xlsx_workbook, start_streaming_xlsx_workbook_with_max_rows,
|
||||
start_streaming_xlsx_workbook_with_options, start_streaming_xlsx_workbook_with_trailing_sheets,
|
||||
write_worksheet_xml, WorksheetSegment, XlsxWorksheetData,
|
||||
};
|
||||
use calamine::{open_workbook_auto, Reader};
|
||||
use serde_json::{json, Value};
|
||||
@@ -1241,7 +1254,7 @@ mod tests {
|
||||
rows: vec![vec![json!(1)]],
|
||||
numeric_column_right_align: false,
|
||||
};
|
||||
let workbook = build_xlsx_workbook_multi_with_auto_filter(&[worksheet], false).expect("build workbook");
|
||||
let workbook = build_xlsx_workbook_multi_with_auto_filter(&[worksheet], false, None).expect("build workbook");
|
||||
|
||||
assert!(!read_zip_entry(&workbook, "xl/worksheets/sheet1.xml").contains("<autoFilter"));
|
||||
}
|
||||
@@ -1344,6 +1357,34 @@ mod tests {
|
||||
assert!(styles.contains("numFmtId=\"165\" formatCode=\"yyyy-mm-dd hh:mm:ss\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_memory_temporal_cells_keep_the_configured_excel_display_format() {
|
||||
// The current-page export builds the whole workbook in memory instead of
|
||||
// streaming it. It used to hardcode `styles_xml(None)`, so a millisecond
|
||||
// pattern produced serials that carried the fraction but a numFmt that
|
||||
// displayed only whole seconds.
|
||||
let data = XlsxWorksheetData {
|
||||
sheet_name: Some("Temporal".to_string()),
|
||||
columns: vec!["ts".to_string()],
|
||||
column_types: vec!["datetime(3)".to_string()],
|
||||
column_comments: vec![],
|
||||
rows: vec![vec![json!("2026-07-25 13:02:15.456")]],
|
||||
numeric_column_right_align: false,
|
||||
};
|
||||
|
||||
let workbook =
|
||||
build_xlsx_workbook_with_auto_filter(&data, true, Some("YYYY-MM-DD HH:mm:ss.SSS")).expect("build workbook");
|
||||
let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml");
|
||||
let styles = read_zip_entry(&workbook, "xl/styles.xml");
|
||||
assert!(sheet.contains("<c r=\"A2\" s=\"3\"><v>46228.54323444444</v></c>"), "sheet={sheet}");
|
||||
assert!(styles.contains("numFmtId=\"165\" formatCode=\"yyyy-mm-dd hh:mm:ss.000\""), "styles={styles}");
|
||||
|
||||
// No configured pattern keeps the historical default.
|
||||
let workbook = build_xlsx_workbook_with_auto_filter(&data, true, None).expect("build workbook");
|
||||
let styles = read_zip_entry(&workbook, "xl/styles.xml");
|
||||
assert!(styles.contains("numFmtId=\"165\" formatCode=\"yyyy-mm-dd hh:mm:ss\""), "styles={styles}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_mysql_57_numeric_strings_as_numeric_cells() {
|
||||
let workbook = build_xlsx_workbook(&XlsxWorksheetData {
|
||||
@@ -1920,6 +1961,7 @@ mod tests {
|
||||
numeric_column_right_align: false,
|
||||
}],
|
||||
2,
|
||||
None,
|
||||
)
|
||||
.expect("build workbook");
|
||||
|
||||
@@ -1991,7 +2033,7 @@ mod tests {
|
||||
};
|
||||
let mut stats = WriteStats::default();
|
||||
|
||||
write_worksheet_xml(&mut stats, &segment, true).expect("write large worksheet");
|
||||
write_worksheet_xml(&mut stats, &segment, true, None).expect("write large worksheet");
|
||||
|
||||
assert!(stats.bytes_written > 10_000_000, "expected realistic worksheet size, got {}", stats.bytes_written);
|
||||
assert!(
|
||||
@@ -2022,7 +2064,7 @@ mod tests {
|
||||
rows: (100..102).map(|i| vec![json!(i)]).collect(),
|
||||
numeric_column_right_align: false,
|
||||
};
|
||||
let data = build_xlsx_workbook_multi_with_max_rows(&[sheet_a, sheet_b], 3).expect("build workbook");
|
||||
let data = build_xlsx_workbook_multi_with_max_rows(&[sheet_a, sheet_b], 3, None).expect("build workbook");
|
||||
|
||||
let workbook_xml = read_zip_entry(&data, "xl/workbook.xml");
|
||||
assert!(workbook_xml.contains("name=\"A\""), "workbook: {workbook_xml}");
|
||||
@@ -2056,6 +2098,7 @@ mod tests {
|
||||
numeric_column_right_align: false,
|
||||
}],
|
||||
100,
|
||||
None,
|
||||
)
|
||||
.expect("build workbook");
|
||||
|
||||
|
||||
Reference in new issue
Block a user