| { | ||
| "git": { | ||
| "sha1": "84ee4326f2abdad2fa2d1e48288c521e078d67a7" | ||
| "sha1": "3a1717a61ef92e83ac031f02e37cd20bc7a18320" | ||
| }, | ||
| "path_in_vcs": "" | ||
| } |
+2
-1
@@ -15,3 +15,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "doe" | ||
| version = "1.1.69" | ||
| version = "1.1.71" | ||
| authors = ["Andrew <dnrops@outlook.com>"] | ||
@@ -203,2 +203,3 @@ build = false | ||
| "anyhow", | ||
| "chrono", | ||
| ] | ||
@@ -205,0 +206,0 @@ |
+115
-1
@@ -52,2 +52,3 @@ /// xlsx model | ||
| let mut xlsx_book = xlsx::new_file(); | ||
| let _ = xlsx_book.remove_sheet_by_name("Sheet1"); | ||
| let mut st = Worksheet::default(); | ||
@@ -542,2 +543,109 @@ st.set_name(sheet_name.to_string()); | ||
| } | ||
| pub fn convert_excel_date(excel_date_str: &str) -> String { | ||
| use chrono::{NaiveDate, NaiveDateTime, Timelike, Datelike}; | ||
| // 去除可能的空白字符 | ||
| let excel_date_str = excel_date_str.trim(); | ||
| // 如果已经是标准日期时间格式,直接返回 | ||
| if excel_date_str.contains("-") && excel_date_str.len() >= 10 { | ||
| return excel_date_str.to_string(); | ||
| } | ||
| // 尝试解析为f64(Excel数字格式) | ||
| if let std::result::Result::Ok(excel_date) = excel_date_str.parse::<f64>() { | ||
| // Excel日期系统说明: | ||
| // Excel 1 = 1900-01-01 | ||
| // Excel 2 = 1900-01-02 | ||
| // ... | ||
| // Excel 60 = 1900-02-29 (这个日期实际上不存在,Excel错误地认为1900年是闰年) | ||
| // Excel 61 = 1900-03-01 | ||
| // 所以我们需要处理这个1900闰年bug | ||
| let whole_days = excel_date.floor() as i64; | ||
| let fraction = excel_date.fract(); | ||
| // 基准日期:1899-12-30,这样Excel 1 = 1900-01-01 | ||
| let base_date = NaiveDate::from_ymd_opt(1899, 12, 30).unwrap(); | ||
| // 计算天数 | ||
| let adjusted_days = whole_days; | ||
| // 注意:Excel的1900年闰年bug处理 | ||
| // Excel错误地认为1900年是闰年,所以Excel 60 = 1900-02-29(不存在的日期) | ||
| // Excel 61 = 1900-03-01 | ||
| // 但是我们使用1899-12-30作为基准日期,这个基准已经考虑了这个bug | ||
| // 所以不需要额外调整天数 | ||
| // 计算最终日期 | ||
| let date = match base_date.checked_add_signed(chrono::Duration::days(adjusted_days)) { | ||
| Some(d) => d, | ||
| None => return excel_date_str.to_string(), | ||
| }; | ||
| // 处理时间部分(Excel的小数部分是时间的比例) | ||
| let total_seconds = (fraction * 86400.0).round() as u32; | ||
| let hours = total_seconds / 3600; | ||
| let minutes = (total_seconds % 3600) / 60; | ||
| let seconds = total_seconds % 60; | ||
| // 创建日期时间 | ||
| let datetime = date.and_hms_opt(hours, minutes, seconds); | ||
| match datetime { | ||
| Some(dt) => { | ||
| // 格式化输出 | ||
| format!( | ||
| "{:04}-{:02}-{:02} {:02}:{:02}:{:02}", | ||
| dt.year(), | ||
| dt.month(), | ||
| dt.day(), | ||
| dt.hour(), | ||
| dt.minute(), | ||
| dt.second() | ||
| ) | ||
| } | ||
| None => excel_date_str.to_string(), | ||
| } | ||
| } else { | ||
| // 尝试处理"2025/1/12"这样的格式 | ||
| excel_date_str.to_string() | ||
| } | ||
| } | ||
| // 辅助函数:将"YYYY/MM/DD"格式转换为"YYYY-MM-DD HH:MM:SS" | ||
| pub fn convert_date_format(date_str: &str) -> String { | ||
| let parts: Vec<&str> = date_str.split('/').collect(); | ||
| if parts.len() == 3 { | ||
| let year = parts[0]; | ||
| let month = parts[1]; | ||
| let day = parts[2]; | ||
| // 如果包含时间,处理时间部分 | ||
| // In function `convert_date_format`, around line 105 | ||
| let (day_part, time_part) = if day.contains(' ') { | ||
| let day_parts: Vec<&str> = day.split(' ').collect(); | ||
| (day_parts[0], day_parts.get(1).copied().unwrap_or("")) | ||
| } else { | ||
| (day, "") | ||
| }; | ||
| let mut result = format!("{}-{:02}-{:02}", | ||
| year, | ||
| month.parse::<u32>().unwrap_or(0), | ||
| day_part.parse::<u32>().unwrap_or(0)); | ||
| if !time_part.is_empty() { | ||
| result.push_str(&format!(" {}", time_part)); | ||
| } else { | ||
| result.push_str(" 00:00:00"); | ||
| } | ||
| result | ||
| } else { | ||
| date_str.to_string() | ||
| } | ||
| } | ||
| pub fn xlsx_to_csv( | ||
@@ -562,3 +670,9 @@ xlsx_path: impl ToString, | ||
| let mut cell = sheet.get_cell_value_mut((c as u32, r as u32)); | ||
| row_vec.push(cell.get_value_lazy().to_string()); | ||
| if cell.get_data_type()=="n" { | ||
| // row_vec.push(convert_excel_date(&cell.get_value_lazy().to_string())); | ||
| row_vec.push(cell.get_value_lazy().to_string()); | ||
| }else{ | ||
| row_vec.push(cell.get_value_lazy().to_string()); | ||
| } | ||
| } | ||
@@ -565,0 +679,0 @@ csv_data.push(row_vec); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet