优化 Excel 数据处理和命令发送逻辑
- 修改 `Excel2Prompt.cs` 中 `ConverterToPrompt` 方法,支持列名映射。 - 提取 `AiMessageControll.xaml.cs` 中的发送命令逻辑到 `InvokeSendCommand` 方法。 - 在 `ImportViewModel.cs` 中新增 `_columnMappings` 字典,增强 Excel 数据处理逻辑。 - 整合并简化 `GenColumns` 方法的实现。 - 新增 `StringExtensions` 静态类,提供字符串替换扩展方法。 - 更新 `launch.json` 和 `tasks.json`,新增调试和构建配置。
This commit is contained in:
parent
cf3bd8a294
commit
1f9dd20c87
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": ".NET Core Launch (console)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
"program": "${workspaceFolder}/src/ExcelHelper/bin/Debug/net9.0-windows/ExcelHelper.dll",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}/src/ExcelHelper",
|
||||||
|
"console": "internalConsole",
|
||||||
|
"stopAtEntry": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ".NET Core Attach",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "attach"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/src/ExcelHelper/ExcelHelper.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "publish",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"publish",
|
||||||
|
"${workspaceFolder}/src/ExcelHelper/ExcelHelper.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary;ForceNoAlign"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"--project",
|
||||||
|
"${workspaceFolder}/src/ExcelHelper/ExcelHelper.csproj"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ public class Excel2Prompt
|
||||||
/// <param name="columns"></param>
|
/// <param name="columns"></param>
|
||||||
/// <param name="startCell">A</param>
|
/// <param name="startCell">A</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, IEnumerable<string> columns, string startCell)
|
public static string ConverterToPrompt(IEnumerable<dynamic> excelData, Dictionary<string,string> columns, string startCell)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
示例:
|
示例:
|
||||||
|
@ -46,13 +46,15 @@ public class Excel2Prompt
|
||||||
int columnIndex = startColumnIndex;
|
int columnIndex = startColumnIndex;
|
||||||
foreach (var column in columns)
|
foreach (var column in columns)
|
||||||
{
|
{
|
||||||
|
var colOriName = column.Key;
|
||||||
|
var safeColName = column.Value;
|
||||||
columnIndex++;
|
columnIndex++;
|
||||||
string columnLetter = GetExcelColumnName(columnIndex);
|
string columnLetter = GetExcelColumnName(columnIndex);
|
||||||
|
|
||||||
var columnData = dataList.Select(row =>
|
var columnData = dataList.Select(row =>
|
||||||
{
|
{
|
||||||
var rowDict = (IDictionary<string, object>)row;
|
var rowDict = (IDictionary<string, object>)row;
|
||||||
return rowDict.ContainsKey(column) ? rowDict[column] : null;
|
return rowDict.ContainsKey(safeColName) ? rowDict[safeColName] : null;
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
var nonNullData = columnData.Where(value => value != null).ToList();
|
var nonNullData = columnData.Where(value => value != null).ToList();
|
||||||
|
@ -63,7 +65,7 @@ public class Excel2Prompt
|
||||||
// 自动识别数据类型
|
// 自动识别数据类型
|
||||||
var dataType = GetColumnDataType(nonNullData);
|
var dataType = GetColumnDataType(nonNullData);
|
||||||
|
|
||||||
var prompt = $"- Column '{column.Replace("\n", " ")}' (Excel Column {columnLetter})" +
|
var prompt = $"- Column '{column.Key.Replace("\n", " ")}' (Excel Column {columnLetter})" +
|
||||||
$" - Type: {dataType}" +
|
$" - Type: {dataType}" +
|
||||||
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
|
$" - Sample values: ['{string.Join("', '", sampleValues)}']";
|
||||||
prompts.Add(prompt);
|
prompts.Add(prompt);
|
||||||
|
|
|
@ -80,24 +80,27 @@ public partial class AiMessageControll : UserControl
|
||||||
|
|
||||||
private void SendButton_Click(object sender, RoutedEventArgs e)
|
private void SendButton_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (SendCommand != null && SendCommand.CanExecute(CurrentMessage))
|
InvokeSendCommand();
|
||||||
{
|
|
||||||
SendCommand.Execute(CurrentMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MessageInput_KeyDown(object sender, KeyEventArgs e)
|
private void MessageInput_KeyDown(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.Key == Key.Enter && !string.IsNullOrEmpty(CurrentMessage) && !IsWaiting)
|
if (e.Key == Key.Enter && !string.IsNullOrEmpty(CurrentMessage) && !IsWaiting)
|
||||||
{
|
{
|
||||||
if (SendCommand != null && SendCommand.CanExecute(CurrentMessage))
|
InvokeSendCommand();
|
||||||
{
|
|
||||||
SendCommand.Execute(CurrentMessage);
|
|
||||||
}
|
|
||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void InvokeSendCommand()
|
||||||
|
{
|
||||||
|
if (SendCommand != null && SendCommand.CanExecute(CurrentMessage))
|
||||||
|
{
|
||||||
|
SendCommand.Execute(CurrentMessage);
|
||||||
|
CurrentMessage = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void ScrollToEnd()
|
public void ScrollToEnd()
|
||||||
{
|
{
|
||||||
if (Messages.Count > 0)
|
if (Messages.Count > 0)
|
||||||
|
|
|
@ -15,13 +15,75 @@ using ExcelHelper.Views.Components;
|
||||||
using MiniExcelLibs;
|
using MiniExcelLibs;
|
||||||
using MiniExcelLibs.OpenXml;
|
using MiniExcelLibs.OpenXml;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Dynamic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace ExcelHelper.Views.ViewModels;
|
namespace ExcelHelper.Views.ViewModels;
|
||||||
|
|
||||||
|
|
||||||
public partial class ImportViewModel : ObservableRecipient, IViewModel
|
public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
{
|
{
|
||||||
|
// 新增列名映射字典
|
||||||
|
private readonly Dictionary<string, string> _columnMappings = new();
|
||||||
|
|
||||||
|
private void GenColumns(IEnumerable<string> columns)
|
||||||
|
{
|
||||||
|
ExcelColumns.Clear();
|
||||||
|
TableColumns.Clear();
|
||||||
|
_columnMappings.Clear();
|
||||||
|
|
||||||
|
if (UseHeaderRow)
|
||||||
|
{
|
||||||
|
foreach (var columnName in columns)
|
||||||
|
{
|
||||||
|
// 修改正则表达式,保留中文字符(\u4e00-\u9fff)
|
||||||
|
var safeColumnName = Regex.Replace(columnName, @"[^a-zA-Z0-9\u4e00-\u9fff]", "_")
|
||||||
|
.Trim('_')
|
||||||
|
.ReplaceMultiple("__", "_");
|
||||||
|
_columnMappings[columnName] = safeColumnName; // 保存映射关系
|
||||||
|
|
||||||
|
var column = new DataGridTextColumn
|
||||||
|
{
|
||||||
|
Header = columnName,
|
||||||
|
Binding = new System.Windows.Data.Binding($"{safeColumnName}"), // 恢复方括号语法
|
||||||
|
Width = DataGridLength.Auto
|
||||||
|
};
|
||||||
|
ExcelColumns.Add(column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (var i = 'A'; i < 'Z'; i++)
|
||||||
|
{
|
||||||
|
var column = new DataGridTextColumn
|
||||||
|
{
|
||||||
|
Header = $"{i}",
|
||||||
|
Binding = new System.Windows.Data.Binding($"{i}"),
|
||||||
|
Width = DataGridLength.Auto
|
||||||
|
};
|
||||||
|
ExcelColumns.Add(column);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var columnsSeq = 0;
|
||||||
|
foreach (var item in ExcelColumns)
|
||||||
|
{
|
||||||
|
TableColumns.Add(new TableColumnModel
|
||||||
|
{
|
||||||
|
ExcelColumnName = item.Header.ToString(),
|
||||||
|
TableColumnName = $"Column{columnsSeq}",
|
||||||
|
});
|
||||||
|
columnsSeq++;
|
||||||
|
}
|
||||||
|
if (ExcelColumns.Count > 0)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send(new UpdateDataGridColumnsMessage([.. ExcelColumns]));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send(new ErrorDialogMessage("未读取到列信息!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
private readonly AiHelper AiHelper = new("deepseek-r1-250120", false);
|
private readonly AiHelper AiHelper = new("deepseek-r1-250120", false);
|
||||||
|
|
||||||
public Task FileDrop(string[] files)
|
public Task FileDrop(string[] files)
|
||||||
|
@ -67,15 +129,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
IsLoading = false;
|
IsLoading = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var excelData = await GetExcelData(path, config);
|
|
||||||
if (MaxRow != 0)
|
|
||||||
{
|
|
||||||
ExcelData = excelData.Take(Math.Min(MaxRow, 300));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ExcelData = excelData.Take(300);
|
|
||||||
}
|
|
||||||
var currentSheetName = SelectedSheetName;
|
var currentSheetName = SelectedSheetName;
|
||||||
var columns = MiniExcel.GetColumns(path, useHeaderRow: UseHeaderRow, startCell: StartCell, sheetName: currentSheetName, configuration: config);
|
var columns = MiniExcel.GetColumns(path, useHeaderRow: UseHeaderRow, startCell: StartCell, sheetName: currentSheetName, configuration: config);
|
||||||
|
|
||||||
|
@ -90,9 +144,42 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
{
|
{
|
||||||
SelectedSheetName = Sheets.FirstOrDefault(it => it.Equals(currentSheetName));
|
SelectedSheetName = Sheets.FirstOrDefault(it => it.Equals(currentSheetName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var excelData = await GetExcelData(path, config);
|
||||||
|
// 新增数据处理逻辑
|
||||||
|
if (UseHeaderRow)
|
||||||
|
{
|
||||||
|
var processedData = new List<ExpandoObject>();
|
||||||
|
foreach (var row in excelData)
|
||||||
|
{
|
||||||
|
var newRow = new ExpandoObject();
|
||||||
|
var newRowDict = (IDictionary<string, object>)newRow;
|
||||||
|
var originalRow = (IDictionary<string, object>)row;
|
||||||
|
foreach (var col in columns)
|
||||||
|
{
|
||||||
|
if (_columnMappings.TryGetValue(col, out var safeName)
|
||||||
|
&& originalRow.TryGetValue(col, out var value))
|
||||||
|
{
|
||||||
|
newRowDict[safeName] = value;
|
||||||
|
Debug.WriteLine($"Mapping {col} to {safeName}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
processedData.Add(newRow);
|
||||||
|
}
|
||||||
|
excelData = processedData;
|
||||||
|
}
|
||||||
|
if (MaxRow != 0)
|
||||||
|
{
|
||||||
|
ExcelData = excelData.Take(Math.Min(MaxRow, 300));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ExcelData = excelData.Take(300);
|
||||||
|
}
|
||||||
|
|
||||||
MaxRow = ExcelData.Count();
|
MaxRow = ExcelData.Count();
|
||||||
|
|
||||||
ExcelPromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns,StartCell);
|
ExcelPromptString = Excel2Prompt.ConverterToPrompt(ExcelData, _columnMappings, StartCell);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -108,57 +195,6 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
: MiniExcel.QueryRange(path, sheetName: SelectedSheetName, useHeaderRow: UseHeaderRow, startCell: StartCell, endCell: EndCell, configuration: config);
|
: MiniExcel.QueryRange(path, sheetName: SelectedSheetName, useHeaderRow: UseHeaderRow, startCell: StartCell, endCell: EndCell, configuration: config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void GenColumns(IEnumerable<string> columns)
|
|
||||||
{
|
|
||||||
ExcelColumns.Clear();
|
|
||||||
TableColumns.Clear();
|
|
||||||
if (UseHeaderRow)
|
|
||||||
{
|
|
||||||
foreach (var columnName in columns)
|
|
||||||
{
|
|
||||||
var column = new DataGridTextColumn
|
|
||||||
{
|
|
||||||
Header = columnName,
|
|
||||||
Binding = new System.Windows.Data.Binding($"{columnName}"),
|
|
||||||
Width = DataGridLength.Auto
|
|
||||||
};
|
|
||||||
ExcelColumns.Add(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (var i = 'A'; i < 'Z'; i++)
|
|
||||||
{
|
|
||||||
var column = new DataGridTextColumn
|
|
||||||
{
|
|
||||||
Header = $"{i}",
|
|
||||||
Binding = new System.Windows.Data.Binding($"{i}"),
|
|
||||||
Width = DataGridLength.Auto
|
|
||||||
};
|
|
||||||
ExcelColumns.Add(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var columnsSeq = 0;
|
|
||||||
foreach (var item in ExcelColumns)
|
|
||||||
{
|
|
||||||
TableColumns.Add(new TableColumnModel
|
|
||||||
{
|
|
||||||
ExcelColumnName = item.Header.ToString(),
|
|
||||||
TableColumnName = $"Column{columnsSeq}",
|
|
||||||
});
|
|
||||||
columnsSeq++;
|
|
||||||
}
|
|
||||||
if (ExcelColumns.Count > 0)
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new UpdateDataGridColumnsMessage([.. ExcelColumns]));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new ErrorDialogMessage("未读取到列信息!"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
private void OnReLoadExcel()
|
private void OnReLoadExcel()
|
||||||
{
|
{
|
||||||
|
@ -431,3 +467,14 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel
|
||||||
private string _excelPromptString;
|
private string _excelPromptString;
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 将扩展方法移到类外部
|
||||||
|
public static class StringExtensions
|
||||||
|
{
|
||||||
|
public static string ReplaceMultiple(this string input, string oldValue, string newValue)
|
||||||
|
{
|
||||||
|
while (input.Contains(oldValue))
|
||||||
|
input = input.Replace(oldValue, newValue);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue