diff --git a/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.sln b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.sln new file mode 100644 index 00000000..e92af518 --- /dev/null +++ b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.sln @@ -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 diff --git a/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Data/Sales Data.xlsx b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Data/Sales Data.xlsx new file mode 100644 index 00000000..41f93f05 Binary files /dev/null and b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Data/Sales Data.xlsx differ diff --git a/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.csproj b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.csproj new file mode 100644 index 00000000..7b6562ba --- /dev/null +++ b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI.csproj @@ -0,0 +1,15 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + diff --git a/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Program.cs b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Program.cs new file mode 100644 index 00000000..682796e7 --- /dev/null +++ b/Videos/ExcelSummaryWithAzureOpenAI/ExcelSummaryWithAzureOpenAI/Program.cs @@ -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 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(); + } + } + } +}