添加XML回退解析机制支持复杂Excel文件

当pandas无法解析某些包含非标准元素的Excel文件时,
添加了XML直接解析功能来提取工作表名称和数据。

- 实现了`_extract_sheet_names_from_xml`方法从XML提取工作表名称
- 实现了`_read_excel_sheet_xml`方法直接解析Excel XML数据
- 添加多种命名空间支持以处理不同Excel格式
- 在pandas解析失败时自动回退到XML解析方式

fix(excel-storage-service): 修复XML解析中的命名空间问题

改进了XML解析逻辑,添加对多种命名空间的支持,
使用通配符查找元素以兼容不同Excel文件格式。

refactor(table-rag-service): 优化XML解析逻辑提高兼容性

统一了XML解析的命名空间处理方式,
改进了元素查找逻辑以更好地支持不同Excel格式。

feat(frontend): 添加RAG向量检索和索引重建功能

- 实现了RAG状态查看、搜索和索引重建接口
- 添加了前端RAG检索界面组件
- 增加了错误处理和加载状态提示
This commit is contained in:
2026-04-08 19:21:40 +08:00
parent 41e5eaaa2d
commit 3b82103e87
6 changed files with 523 additions and 145 deletions

View File

@@ -45,8 +45,25 @@ class ExcelStorageService:
return []
content = z.read('xl/workbook.xml')
root = ET.fromstring(content)
ns = {'main': 'http://purl.oclc.org/ooxml/spreadsheetml/main'}
sheets = root.findall('.//main:sheet', ns)
# 尝试多种命名空间
namespaces = [
'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
'http://purl.oclc.org/ooxml/spreadsheetml/main',
]
for ns_uri in namespaces:
ns = {'main': ns_uri}
sheets = root.findall('.//main:sheet', ns)
if sheets:
names = [s.get('name') for s in sheets if s.get('name')]
if names:
return names
# 尝试通配符
sheets = root.findall('.//{*}sheet')
if not sheets:
sheets = root.findall('.//sheet')
return [s.get('name') for s in sheets if s.get('name')]
except Exception:
return []
@@ -79,72 +96,77 @@ class ExcelStorageService:
if 'xl/sharedStrings.xml' in z.namelist():
ss_content = z.read('xl/sharedStrings.xml')
ss_root = ET.fromstring(ss_content)
ns = {'main': 'http://purl.oclc.org/ooxml/spreadsheetml/main'}
for si in ss_root.findall('.//main:si', ns):
t = si.find('.//main:t', ns)
shared_strings.append(t.text if t is not None else '')
for si in ss_root.iter():
if si.tag.endswith('}si') or si.tag == 'si':
t = si.find('.//{*}t')
shared_strings.append(t.text if t is not None and t.text else '')
sheet_file = f'xl/worksheets/sheet{sheet_index}.xml'
sheet_content = z.read(sheet_file)
root = ET.fromstring(sheet_content)
ns = {'main': 'http://purl.oclc.org/ooxml/spreadsheetml/main'}
rows_data = []
for row in root.findall('.//main:row', ns):
row_idx = int(row.get('r', 0))
if row_idx <= header_row + 1:
continue
headers = {}
row_cells = {}
for cell in row.findall('main:c', ns):
cell_ref = cell.get('r', '')
col_letters = ''.join(filter(str.isalpha, cell_ref))
cell_type = cell.get('t', 'n')
v = cell.find('main:v', ns)
for row in root.iter():
if row.tag.endswith('}row') or row.tag == 'row':
row_idx = int(row.get('r', 0))
if v is not None and v.text:
if cell_type == 's':
try:
val = shared_strings[int(v.text)]
except (ValueError, IndexError):
val = v.text
elif cell_type == 'b':
val = v.text == '1'
else:
val = v.text
else:
val = None
row_cells[col_letters] = val
# 收集表头行
if row_idx == header_row + 1:
for cell in row:
if cell.tag.endswith('}c') or cell.tag == 'c':
cell_ref = cell.get('r', '')
col_letters = ''.join(filter(str.isalpha, cell_ref))
cell_type = cell.get('t', 'n')
v = cell.find('{*}v')
if v is not None and v.text:
if cell_type == 's':
try:
headers[col_letters] = shared_strings[int(v.text)]
except (ValueError, IndexError):
headers[col_letters] = v.text
else:
headers[col_letters] = v.text
else:
headers[col_letters] = col_letters
continue
if row_cells:
rows_data.append(row_cells)
if row_idx <= header_row + 1:
continue
row_cells = {}
for cell in row:
if cell.tag.endswith('}c') or cell.tag == 'c':
cell_ref = cell.get('r', '')
col_letters = ''.join(filter(str.isalpha, cell_ref))
cell_type = cell.get('t', 'n')
v = cell.find('{*}v')
if v is not None and v.text:
if cell_type == 's':
try:
val = shared_strings[int(v.text)]
except (ValueError, IndexError):
val = v.text
elif cell_type == 'b':
val = v.text == '1'
else:
val = v.text
else:
val = None
row_cells[col_letters] = val
if row_cells:
rows_data.append(row_cells)
if not rows_data:
return pd.DataFrame()
df = pd.DataFrame(rows_data)
if header_row >= 0:
first_row_sheet = f'xl/worksheets/sheet{sheet_index}.xml'
sheet_content = z.read(first_row_sheet)
root = ET.fromstring(sheet_content)
first_row = root.find(f'.//main:row[@r="{header_row + 1}"]', ns)
if first_row is not None:
headers = {}
for cell in first_row.findall('main:c', ns):
cell_ref = cell.get('r', '')
col_letters = ''.join(filter(str.isalpha, cell_ref))
cell_type = cell.get('t', 'n')
v = cell.find('main:v', ns)
if v is not None and v.text:
if cell_type == 's':
try:
headers[col_letters] = shared_strings[int(v.text)]
except (ValueError, IndexError):
headers[col_letters] = v.text
else:
headers[col_letters] = v.text
df.columns = [headers.get(col, col) for col in df.columns]
if headers:
df.columns = [headers.get(col, col) for col in df.columns]
return df
except Exception as e: