Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37314.3 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExcelSummaryWithAzureOpenAI", "ExcelSummaryWithAzureOpenAI\ExcelSummaryWithAzureOpenAI.csproj", "{78E16B8D-D61E-4D40-A077-95AC9746C62E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78E16B8D-D61E-4D40-A077-95AC9746C62E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {56ED7709-D106-4B96-9092-FB28E77F3FCE}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="33.2.7" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Azure;
using Azure.AI.OpenAI;
using OpenAI.Chat;
using Syncfusion.XlsIO;
using System.Text;

namespace ExcelSummaryWithAzureOpenAI
{
internal class Program
{
static async Task Main(string[] args)
{
string excelFilePath = "../../../Data/Sales Data.xlsx"; // Replace with the path to your Excel file

try
{
// Read Excel data and convert to text
string excelText = ExtractDataAsText(excelFilePath);
Console.WriteLine("Excel data read successfully.");

// Send data to Azure OpenAI for summarization
string summary = await SummarizeData(excelText);
Console.WriteLine("Summary from OpenAI:");
Console.WriteLine(summary);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

// This method sends the extracted Excel text to Azure OpenAI
public static Task<string> SummarizeData(string inputText)
{
// Initialize Azure OpenAI client with endpoint and API key
AzureOpenAIClient azureOpenAIClient = new(
new Uri("YOUR_ENDPOINT"),
new AzureKeyCredential("YOUR_API_KEY"));

// Create Chat client using the Azure OpenAI
ChatClient chatClient = azureOpenAIClient.GetChatClient("YOUR_MODEL_NAME");

// Create a chat completion request to summarize the Excel data
ChatCompletion completion = chatClient.CompleteChat([
new SystemChatMessage("You are a helpful assistant that summarizes Excel data."),
new UserChatMessage($"Summarize the following Excel data:\n{inputText}")
]);

// Return the summarized text result
return Task.FromResult(completion.Content[0].Text?.Trim() ?? string.Empty);
}

// Extracts data from an Excel file and returns it as a formatted text string
public static string ExtractDataAsText(string filePath)
{
//Initialize the Excel engine
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Get the Excel application instance
IApplication application = excelEngine.Excel;

// Set the default version to Xlsx
application.DefaultVersion = ExcelVersion.Xlsx;

// Open the Excel workbook
IWorkbook workbook = application.Workbooks.Open(filePath);

// Create a StringBuilder to hold the extracted data
StringBuilder stringBuilder = new StringBuilder();

// Iterate through each worksheet and extract data
foreach (IWorksheet worksheet in workbook.Worksheets)
{
// Append the worksheet name
stringBuilder.AppendLine($"Worksheet: {worksheet.Name}");

// Get the used range of the worksheet
IRange usedRange = worksheet.UsedRange;

// Loop through rows
for (int row = usedRange.Row; row <= usedRange.LastRow; row++)
{
// Loop through columns
for (int col = usedRange.Column; col <= usedRange.LastColumn; col++)
{
//Get the cell value and append it to the StringBuilder
stringBuilder.Append(worksheet[row, col].DisplayText + "\t");

}

stringBuilder.AppendLine();

}

stringBuilder.AppendLine();
}

return stringBuilder.ToString();
}
}
}
}
Loading