diff --git a/Utils/Excel2Prompt.cs b/Utils/Excel2Prompt.cs index 3350b25..7bb4e4d 100644 --- a/Utils/Excel2Prompt.cs +++ b/Utils/Excel2Prompt.cs @@ -12,8 +12,9 @@ public class Excel2Prompt /// /// /// + /// A /// - public static string ConverterToPrompt(IEnumerable excelData, IEnumerable columns) + public static string ConverterToPrompt(IEnumerable excelData, IEnumerable columns, string startCell) { /* 示例: @@ -36,11 +37,18 @@ public class Excel2Prompt - Missing values: 7007 */ var prompts = new List(); - prompts.Add("This Excel file contains the following columns:" ); + prompts.Add("This Excel file contains the following columns:"); var dataList = excelData.ToList(); + // 解析 startCell,获取起始列的索引 + int startColumnIndex = GetColumnIndexFromCell(startCell); + + int columnIndex = startColumnIndex; foreach (var column in columns) { + columnIndex++; + string columnLetter = GetExcelColumnName(columnIndex); + var columnData = dataList.Select(row => { var rowDict = (IDictionary)row; @@ -50,23 +58,50 @@ public class Excel2Prompt var nonNullData = columnData.Where(value => value != null).ToList(); var missingValues = columnData.Count - nonNullData.Count; var uniqueValues = nonNullData.Distinct().Count(); - var sampleValues = nonNullData.Take(3).Select(value => value.ToString().Replace("\n"," ")).ToArray(); + var sampleValues = nonNullData.Take(3).Select(value => value.ToString().Replace("\n", " ")).ToArray(); // 自动识别数据类型 var dataType = GetColumnDataType(nonNullData); - var prompt = $"- Column '{column.Replace("\n", " ")}'\n" + - $" - Type: {dataType}\n" + - $" - Sample values: ['{string.Join("', '", sampleValues)}']"; - //$" - Unique values: {uniqueValues}\n" + - //$" - Missing values: {missingValues}\n"; - + var prompt = $"- Column '{column.Replace("\n", " ")}' (Excel Column {columnLetter})\n" + + $" - Type: {dataType}\n" + + $" - Sample values: ['{string.Join("', '", sampleValues)}']"; prompts.Add(prompt); } return string.Join(Environment.NewLine + Environment.NewLine, prompts); } + private static int GetColumnIndexFromCell(string cell) + { + // 提取列部分 + string columnPart = new string(cell.Where(char.IsLetter).ToArray()); + int columnIndex = 0; + int factor = 1; + + for (int i = columnPart.Length - 1; i >= 0; i--) + { + columnIndex += (columnPart[i] - 'A' + 1) * factor; + factor *= 26; + } + // 最终结果需要包含StartCell + return columnIndex - 1; + } + + private static string GetExcelColumnName(int columnNumber) + { + int dividend = columnNumber; + string columnName = string.Empty; + + while (dividend > 0) + { + int modulo = (dividend - 1) % 26; + columnName = Convert.ToChar(65 + modulo) + columnName; + dividend = (dividend - modulo) / 26; + } + + return columnName; + } private static string GetColumnDataType(List data) { diff --git a/Views/ViewModels/ImportViewModel.cs b/Views/ViewModels/ImportViewModel.cs index 41c9935..759717e 100644 --- a/Views/ViewModels/ImportViewModel.cs +++ b/Views/ViewModels/ImportViewModel.cs @@ -84,7 +84,7 @@ public partial class ImportViewModel : ObservableRecipient, IViewModel } MaxRow = ExcelData.Count(); - PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns); + PromptString = Excel2Prompt.ConverterToPrompt(ExcelData, columns,StartCell); } catch (Exception ex) {