Extract content from Excel (XLSB, XLSX, XLS,etc.) files
(file_path: &str)
| 753 | |
| 754 | /// Extract content from Excel (XLSB, XLSX, XLS,etc.) files |
| 755 | async fn extract_excel_content(file_path: &str) -> GraphBitResult<String> { |
| 756 | use calamine::{open_workbook_auto, Reader}; |
| 757 | |
| 758 | let mut workbook = open_workbook_auto(file_path).map_err(|e| { |
| 759 | GraphBitError::validation( |
| 760 | "document_loader", |
| 761 | format!("Failed to open Excel file {}: {}", file_path, e), |
| 762 | ) |
| 763 | })?; |
| 764 | |
| 765 | let mut result = String::new(); |
| 766 | result.push_str("Excel Document Content:\n\n"); |
| 767 | |
| 768 | let sheet_names = workbook.sheet_names().to_vec(); |
| 769 | for sheet_name in sheet_names { |
| 770 | if let Ok(range) = workbook.worksheet_range(&sheet_name) { |
| 771 | writeln!(result, "Sheet: {}", sheet_name).unwrap(); |
| 772 | result.push_str("-".repeat(sheet_name.len() + 7).as_str()); |
| 773 | result.push('\n'); |
| 774 | |
| 775 | for row in range.rows() { |
| 776 | let row_str = row |
| 777 | .iter() |
| 778 | .map(|cell| match cell { |
| 779 | Data::Empty => "".to_string(), |
| 780 | Data::String(s) => s.clone(), |
| 781 | Data::Float(f) => f.to_string(), |
| 782 | Data::Int(i) => i.to_string(), |
| 783 | Data::Bool(b) => b.to_string(), |
| 784 | Data::DateTime(d) => d.to_string(), |
| 785 | Data::Error(e) => format!("Error({:?})", e), |
| 786 | _ => "".to_string(), |
| 787 | }) |
| 788 | .collect::<Vec<_>>() |
| 789 | .join(" | "); |
| 790 | |
| 791 | if !row_str.trim().is_empty() { |
| 792 | writeln!(result, "{}", row_str).unwrap(); |
| 793 | } |
| 794 | } |
| 795 | result.push('\n'); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | if result.trim().is_empty() { |
| 800 | return Err(GraphBitError::validation( |
| 801 | "document_loader", |
| 802 | "No content could be extracted from the Excel file", |
| 803 | )); |
| 804 | } |
| 805 | |
| 806 | Ok(result.trim().to_string()) |
| 807 | } |
| 808 | |
| 809 | /// Get supported document types |
| 810 | pub fn supported_types() -> Vec<&'static str> { |