Skip to content

Commit a172ec1

Browse files
authored
Merge pull request #335 from SyncfusionExamples/1022016-ExcelSummary
Documentation(1022016): How to build an AI Driven Excel Summarizer using the Syncfusion .NET Excel Library
2 parents acd7565 + 9c21939 commit a172ec1

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.37314.3 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelSummaryWithAzureOpenAI", "ExcelSummaryWithAzureOpenAI\ExcelSummaryWithAzureOpenAI.csproj", "{78E16B8D-D61E-4D40-A077-95AC9746C62E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {56ED7709-D106-4B96-9092-FB28E77F3FCE}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="33.2.7" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Azure;
2+
using Azure.AI.OpenAI;
3+
using OpenAI.Chat;
4+
using Syncfusion.XlsIO;
5+
using System.Text;
6+
7+
namespace ExcelSummaryWithAzureOpenAI
8+
{
9+
internal class Program
10+
{
11+
static async Task Main(string[] args)
12+
{
13+
string excelFilePath = "../../../Data/Sales Data.xlsx"; // Replace with the path to your Excel file
14+
15+
try
16+
{
17+
// Read Excel data and convert to text
18+
string excelText = ExtractDataAsText(excelFilePath);
19+
Console.WriteLine("Excel data read successfully.");
20+
21+
// Send data to Azure OpenAI for summarization
22+
string summary = await SummarizeData(excelText);
23+
Console.WriteLine("Summary from OpenAI:");
24+
Console.WriteLine(summary);
25+
}
26+
catch (Exception ex)
27+
{
28+
Console.WriteLine($"An error occurred: {ex.Message}");
29+
}
30+
}
31+
32+
// This method sends the extracted Excel text to Azure OpenAI
33+
public static Task<string> SummarizeData(string inputText)
34+
{
35+
// Initialize Azure OpenAI client with endpoint and API key
36+
AzureOpenAIClient azureOpenAIClient = new(
37+
new Uri("YOUR_ENDPOINT"),
38+
new AzureKeyCredential("YOUR_API_KEY"));
39+
40+
// Create Chat client using the Azure OpenAI
41+
ChatClient chatClient = azureOpenAIClient.GetChatClient("YOUR_MODEL_NAME");
42+
43+
// Create a chat completion request to summarize the Excel data
44+
ChatCompletion completion = chatClient.CompleteChat([
45+
new SystemChatMessage("You are a helpful assistant that summarizes Excel data."),
46+
new UserChatMessage($"Summarize the following Excel data:\n{inputText}")
47+
]);
48+
49+
// Return the summarized text result
50+
return Task.FromResult(completion.Content[0].Text?.Trim() ?? string.Empty);
51+
}
52+
53+
// Extracts data from an Excel file and returns it as a formatted text string
54+
public static string ExtractDataAsText(string filePath)
55+
{
56+
//Initialize the Excel engine
57+
using (ExcelEngine excelEngine = new ExcelEngine())
58+
{
59+
//Get the Excel application instance
60+
IApplication application = excelEngine.Excel;
61+
62+
// Set the default version to Xlsx
63+
application.DefaultVersion = ExcelVersion.Xlsx;
64+
65+
// Open the Excel workbook
66+
IWorkbook workbook = application.Workbooks.Open(filePath);
67+
68+
// Create a StringBuilder to hold the extracted data
69+
StringBuilder stringBuilder = new StringBuilder();
70+
71+
// Iterate through each worksheet and extract data
72+
foreach (IWorksheet worksheet in workbook.Worksheets)
73+
{
74+
// Append the worksheet name
75+
stringBuilder.AppendLine($"Worksheet: {worksheet.Name}");
76+
77+
// Get the used range of the worksheet
78+
IRange usedRange = worksheet.UsedRange;
79+
80+
// Loop through rows
81+
for (int row = usedRange.Row; row <= usedRange.LastRow; row++)
82+
{
83+
// Loop through columns
84+
for (int col = usedRange.Column; col <= usedRange.LastColumn; col++)
85+
{
86+
//Get the cell value and append it to the StringBuilder
87+
stringBuilder.Append(worksheet[row, col].DisplayText + "\t");
88+
89+
}
90+
91+
stringBuilder.AppendLine();
92+
93+
}
94+
95+
stringBuilder.AppendLine();
96+
}
97+
98+
return stringBuilder.ToString();
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)