翻譯|使用教程|編輯:龔雪|2024-09-02 11:22:03.883|閱讀 90 次
概述:本文將利用DevExpress WinForms Spreadsheet控件合并用戶自定義公式,并簡要說明如何利用AI進(jìn)行高級計算。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress WinForms擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
DevExpress技術(shù)交流群10:532598169 歡迎一起進(jìn)群討論
標(biāo)準(zhǔn)Excel公式處理各種業(yè)務(wù)需求,當(dāng)然有時您的企業(yè)可能也會需要額外的功能來處理特定的用例。
當(dāng)涉及到電子表格計算時,用戶自定義公式為那些希望在Excel文檔中引入定制的、特定于任務(wù)計算的最終用戶提供了各種選項。用戶自定義公式允許您自動執(zhí)行重復(fù)任務(wù),更有效地分析數(shù)據(jù),并執(zhí)行內(nèi)置函數(shù)無法執(zhí)行的高級計算。
通過集成AI服務(wù),DevExpress可以將Excel公式計算提升到一個全新的水平,能更有效地分析大型數(shù)據(jù)集、結(jié)合模式識別、做出預(yù)測等。
在本文中我們將使用DevExpress WinForms Spreadsheet控件合并一個用戶定義的公式,并說明如何利用AI進(jìn)行高級計算。
在下面的部分中,我將創(chuàng)建一個用戶定義的公式,該公式使用AI服務(wù)進(jìn)行電子表格計算。
注意:在下面的示例中使用OpenAI服務(wù),在您將此解決方案納入應(yīng)用程序之前,請務(wù)必閱讀并理解OpenAI的許可協(xié)議和使用條款。
首先,我需要將DevExpress WinForms Spreadsheet控件添加到項目中。
一旦將Spreadsheet控件添加到項目中后,就可以安裝Azure.AI.OpenAI NuGet包并創(chuàng)建一個類來注冊O(shè)penAIClient,該客戶端將在自定義公式類中使用(向AI服務(wù)發(fā)送請求)。
public static class AIFormulaClient { public static OpenAIClient Client; static AIFormulaClient() => Client = new OpenAIClient("your OpenAI key"); }
接下來,我將在示例WinForms應(yīng)用程序中創(chuàng)建一個用戶定義函數(shù)(UDF)。出于本例的目的,我將定義一個名為AISummary的函數(shù),此函數(shù)使用AI服務(wù)為選定的單元格范圍生成摘要。
AISummaryFunction類實現(xiàn)了自定義函數(shù)并提供了兩個參數(shù):
IFunction.Evaluate方法檢索參數(shù)值,并將單元格數(shù)據(jù)(CSV格式)和單詞計數(shù)發(fā)送給GetSummary方法,該方法使用OpenAIClient生成請求并返回響應(yīng),返回值(AI生成的摘要)用作公式計算的結(jié)果。
public class AISummaryFunction : ICustomFunction { const string functionName = "AISummary"; readonly ParameterInfo[] functionParameters; public AISummaryFunction() { this.functionParameters = new ParameterInfo[] { new ParameterInfo(ParameterType.Reference, ParameterAttributes.Required), new ParameterInfo(ParameterType.Value, ParameterAttributes.Optional) }; } public string Name { get { return functionName; } } ParameterInfo[] IFunction.Parameters { get { return functionParameters; } } ParameterType IFunction.ReturnType { get { return ParameterType.Value; } } bool IFunction.Volatile { get { return false; } } ParameterValue IFunction.Evaluate(IList<ParameterValue> parameters, EvaluationContext context) { int wordCount = 40; if (parameters.Count > 1) wordCount = (int)parameters[1].NumericValue; IWorkbook workbook = context.Sheet.Workbook; workbook.Options.Export.Csv.Range = parameters[0].RangeValue.GetReferenceA1(); byte[] cellDataBytes = workbook.SaveDocument(DocumentFormat.Csv); string value = GetSummary(Encoding.UTF8.GetString(cellDataBytes), wordCount); return value; } string GetSummary(string cellRangeData, int wordCount) { ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions() { DeploymentName = "gpt-4-vision-preview", Messages = { new ChatRequestSystemMessage("You are a helpful assistant."), new ChatRequestUserMessage( new ChatMessageTextContentItem(string.Format("Please create summary in {0} words for the following excel data", wordCount)), new ChatMessageTextContentItem(cellRangeData)) }, MaxTokens = 300 }; Response<ChatCompletions> chatResponse = AIFormulaClient.Client.GetChatCompletions(chatCompletionsOptions); ChatChoice choice = chatResponse.Value.Choices[0]; return choice.Message.Content; } string IFunction.GetName(CultureInfo culture) { return functionName; } }
在這一步中,將注冊自定義函數(shù)。
public Form1() { InitializeComponent(); AISummaryFunction aiSummaryFunction = new AISummaryFunction(); var globalFunctions = spreadsheetControl.Document.Functions.GlobalCustomFunctions; if (!globalFunctions.Contains(aiSummaryFunction.Name)) globalFunctions.Add(aiSummaryFunction); }
現(xiàn)在可以在電子表格中使用AISummary函數(shù)(就像任何其他公式一樣),將在WinForms Spreadsheet 控件中加載Excel文件,或者可以將匯總數(shù)據(jù)輸入到單元格范圍中,然后應(yīng)用AISummary函數(shù)來獲得AI生成的匯總。
Worksheet worksheet = spreadsheetControl.ActiveWorksheet; worksheet["E2"].Formula = "AISummary(A2:D10, 50)";
接下來將測試新用戶定義公式,并確保我的AI集成/自定義公式按預(yù)期工作。
更多產(chǎn)品需求,歡迎咨詢“”~
更多DevExpress線上公開課、中文教程資訊請上中文網(wǎng)獲取
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)