Chat With Excel using AI

As data complexity continues to rise, the need for efficient and intelligent interaction with Excel becomes increasingly pressing. To meet this challenge, we have integrated AI technology that allows users to engage in profound interactions with Excel through natural language conversations. This transformative technology is set to dramatically enhance work efficiency, lower the learning curve, and redefine the way people interact with data. In this article, we will introduce how to chat with Excel using Spire.XLS AI.

Install Spire.XLS for .NET

The Excel AI integrated into Spire.XLS for .NET package, hence to begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Request a License Key of AI Product

A license key is required to run Spire.XLS AI, please contact our sales department (sales@e-iceblue.com) to request one.

Use AI to Chat With Excel

Spire.XLS AI provides the ExcelAI class, enabling users to engage in dynamic conversations with Excel document data. Spire.XLS AI extends its reach to accommodate diverse file formats including txt, csv, pdf, and md, thus facilitating seamless cross-format intelligence extraction and interpretation. The following are the key methods necessary to achieve this functionality:

  • UploadWorkbook(Workbook wb): This method is used to upload a Workbook object processed by Spire.XLS to the AI server, facilitating the integration of Excel content with the AI system's data.
  • UploadFile(string fileName, Stream stream): This method is used to upload txt files or files in other formats to the AI server.
  • DocumentSearch(string question, string file_server_path, bool enableHistory = false): This method allows posing specific questions to the AI system against a designated Excel document, generating intelligent responses based on its contents. The optional parameter enableHistory is set to false by default, if set to true, it enables the search history feature, allowing subsequent operations to track or leverage previous query results.
  • Ask(string question, bool enableHistory = false): This method allows interacting with the AI system by asking a specific question, generating intelligent responses.
  • ResetChatHistory(string sessionid): This method resets or clears the chat history associated with the sessionid. By invoking this method, all prior conversations and context associated with the specified session will be erased, ensuring a fresh start for the next interaction.

The following code demonstrates how to chat with Excel document using Spire.XLS AI:

  • C#
using Spire.Xls;
using Spire.Xls.AI;
using System.IO;
using System.Text;

// Define the file path of the Excel document 
string inputfile = "Input.xlsx";

// Create a new instance of the Workbook
Workbook wb = new Workbook();

// Load the Excel file
wb.LoadFromFile(inputfile);

// Create a new instance of the ExcelAI
ExcelAI excelAI = new ExcelAI();

// Upload the workbook and obtain the file path where it's stored in the AI system
string fpath = excelAI.UploadWorkbook(wb);

// Set the question1 to be asked to the AI system
string question1 = "The document discusses what topic? And please generate 3 topics for the upcoming conversation";

// Execute a smart search task based on the provided question for the Excel file
string answer1 =excelAI.DocumentSearch(question1, fpath, true);

// Set the question2 to be asked to the AI system
string question2 = "Please expand the first generated topic";

// Execute an ask task to ask question for AI system
string answer2 = excelAI.Ask(question2, true);

// Set the question3 to be asked to the AI system
string question3 = "How to use IF function in Excel to achieve multi condition judgment?";

// Reset the chat history for the current session
excelAI.ResetChatHistory(excelAI.SessionID);

// Execute an ask task to ask question for AI system
string answer3 = excelAI.Ask(question3, true);

// Create a StringBuilder object to append the answers
StringBuilder builder = new StringBuilder();
builder.AppendLine("Answer1:  "+answer1);
builder.AppendLine("-------------------------------------------------------------------");
builder.AppendLine("Answer2:  " + answer2);
builder.AppendLine("-------------------------------------------------------------------");
builder.AppendLine("Answer3:  " + answer3);

// Write the answer to the txt file
File.WriteAllText("ChatWithExcel.txt", builder.ToString());

Input Excel Content:

Chat With Excel using AI

Generated Txt Content:

Chat With Excel using AI