using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExcelHelper.Utils;
public class Excel2Prompt
{
///
/// 将excel数据转化为Ai可读的Prompt格式
///
///
///
///
public static string ConverterToPrompt(IEnumerable excelData, IEnumerable columns)
{
/*
示例:
- Column '系统单号' (Excel Column A)
- Type: object
- Unique values: 30462
- Sample values: ['103526033584143963', '103526050570323185', '103525904231101189']
- Missing values: 8278
- Column '参考号' (Excel Column B)
- Type: object
- Unique values: 11911
- Sample values: ['200012590592083', '200012844542760', '200012745140484']
- Missing values: 26899
- Column '状态' (Excel Column C)
- Type: object
- Unique values: 8
- Sample values: ['待审核发货', '待审核发货', '待审核发货']
- Missing values: 7007
*/
var prompts = new List();
prompts.Add("This Excel file contains the following columns:" );
var dataList = excelData.ToList();
foreach (var column in columns)
{
var columnData = dataList.Select(row =>
{
var rowDict = (IDictionary)row;
return rowDict.ContainsKey(column) ? rowDict[column] : null;
}).ToList();
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 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";
prompts.Add(prompt);
}
return string.Join(Environment.NewLine + Environment.NewLine, prompts);
}
private static string GetColumnDataType(List