|
| 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