//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); // Please uncomment the following code and ensure you fill in the correct API Endpoint, API Key, and Model Name. // Workbook.AIModelRequestHandler = new OpenAIModelRequestHandler(apiEndpoint, apiKey, modelName); IWorksheet sheet = workbook.Worksheets[0]; sheet.Name = "AI Sentiment Analysis Demo"; sheet.Columns[0].ColumnWidth = 55; sheet.Columns[1].ColumnWidth = 55; // ============ Example: Customer Reviews ============ sheet.Range["A1:B1"].Merge(); sheet.Range["A1"].Value = "Example: Customer Product Reviews"; sheet.Range["A1"].Font.Bold = true; sheet.Range["A1"].Font.Size = 16; sheet.Range["A1"].Font.Color = Color.White; sheet.Range["A1"].Interior.Color = Color.FromArgb(90, 126, 158); sheet.Range["A1"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["A1"].VerticalAlignment = VerticalAlignment.Center; sheet.Range["A1"].RowHeight = 35; sheet.Range["A3"].Value = "Formula:"; sheet.Range["A3"].Font.Bold = true; sheet.Range["A3"].Font.Size = 11; sheet.Range["A3"].Interior.Color = Color.FromArgb(217, 225, 242); sheet.Range["B3"].Value = "=AI.TEXTSENTIMENT(A6:A13,\"Positive\",\"Negative\",\"Neutral\")"; sheet.Range["B3"].Font.Italic = true; sheet.Range["B3"].Font.Color = Color.FromArgb(68, 114, 196); sheet.Range["B3"].WrapText = true; sheet.Range["A5:B5"].Value = new object[,] { { "Review Text", "AI Sentiment" } }; sheet.Range["A5:B5"].Font.Bold = true; sheet.Range["A5:B5"].Interior.Color = Color.FromArgb(155, 194, 230); sheet.Range["A5:B5"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["A6:A13"].Value = new object[,] { { "I absolutely love this product! It exceeded all my expectations!" }, { "This is the worst purchase I've ever made. Total waste of money." }, { "The product is okay, nothing special but does the job." }, { "Outstanding quality and excellent customer service!" }, { "Disappointed with the quality. Not worth the price." }, { "It's average. Works fine but could be better." }, { "Amazing! Best product ever! Highly recommend to everyone!" }, { "Terrible experience. Would not recommend to anyone." } }; for (int i = 6; i <= 13; i++) { if ((i - 6) % 2 == 0) { sheet.Range["A" + i].Interior.Color = Color.FromArgb(242, 242, 242); } sheet.Range["A" + i].Borders.LineStyle = BorderLineStyle.Thin; sheet.Range["A" + i].Borders.Color = Color.FromArgb(200, 200, 200); sheet.Range["A" + i].WrapText = true; } sheet.Range["B6"].Formula2 = "=AI.TEXTSENTIMENT(A6:A13,\"Positive\",\"Negative\",\"Neutral\")"; for (int i = 6; i <= 13; i++) { sheet.Range["B" + i].Font.Bold = true; sheet.Range["B" + i].Font.Size = 11; sheet.Range["B" + i].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["B" + i].Borders.LineStyle = BorderLineStyle.Medium; sheet.Range["B" + i].Borders.Color = Color.FromArgb(200, 200, 200); } // ============ Add Conditional Formatting for Sentiment Results ============ IFormatCondition positiveCondition = (IFormatCondition)sheet.Range["B6:B13"].FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=\"Positive\"", null ); positiveCondition.Interior.Color = Color.FromArgb(226, 239, 218); positiveCondition.Font.Color = Color.FromArgb(0, 128, 0); IFormatCondition negativeCondition = (IFormatCondition)sheet.Range["B6:B13"].FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=\"Negative\"", null ); negativeCondition.Interior.Color = Color.FromArgb(255, 199, 206); negativeCondition.Font.Color = Color.FromArgb(192, 0, 0); IFormatCondition neutralCondition = (IFormatCondition)sheet.Range["B6:B13"].FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=\"Neutral\"", null ); neutralCondition.Interior.Color = Color.FromArgb(255, 242, 204); neutralCondition.Font.Color = Color.FromArgb(128, 100, 0); workbook.Calculate(); // As AI functions operate on an asynchronous computation function, // it is necessary to await the completion of their calculation processes. workbook.WaitForCalculationToFinish(); // Set print settings sheet.PageSetup.FitToPagesTall = 1; sheet.PageSetup.FitToPagesWide = 1; sheet.PageSetup.IsPercentScale = false; sheet.PageSetup.Orientation = PageOrientation.Landscape; // Save to a pdf file workbook.Save("Textsentiment.pdf");
' Create a new Workbook Dim workbook As New Workbook ' Please uncomment the following code And ensure you fill in the correct API Endpoint, API Key, And Model Name. ' Workbook.AIModelRequestHandler = New OpenAIModelRequestHandler(apiEndpoint, apiKey, modelName) Dim sheet As IWorksheet = workbook.Worksheets(0) sheet.Name = "AI Sentiment Analysis Demo" sheet.Columns(0).ColumnWidth = 55 sheet.Columns(1).ColumnWidth = 55 '============ Example Customer Reviews ============ sheet.Range("A1:B1").Merge() sheet.Range("A1").Value = "Example: Customer Product Reviews" sheet.Range("A1").Font.Bold = True sheet.Range("A1").Font.Size = 16 sheet.Range("A1").Font.Color = Color.White sheet.Range("A1").Interior.Color = Color.FromArgb(90, 126, 158) sheet.Range("A1").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("A1").VerticalAlignment = VerticalAlignment.Center sheet.Range("A1").RowHeight = 35 sheet.Range("A3").Value = "Formula:" sheet.Range("A3").Font.Bold = True sheet.Range("A3").Font.Size = 11 sheet.Range("A3").Interior.Color = Color.FromArgb(217, 225, 242) sheet.Range("B3").Value = "=AI.TEXTSENTIMENT(A6:A13,""Positive"",""Negative"",""Neutral"")" sheet.Range("B3").Font.Italic = True sheet.Range("B3").Font.Color = Color.FromArgb(68, 114, 196) sheet.Range("B3").WrapText = True sheet.Range("A5:B5").Value = New Object(,) { {"Review Text", "AI Sentiment"} } sheet.Range("A5:B5").Font.Bold = True sheet.Range("A5:B5").Interior.Color = Color.FromArgb(155, 194, 230) sheet.Range("A5:B5").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("A6:A13").Value = New Object(,) { {"I absolutely love this product! It exceeded all my expectations!"}, {"This is the worst purchase I've ever made. Total waste of money."}, {"The product is okay, nothing special but does the job."}, {"Outstanding quality and excellent customer service!"}, {"Disappointed with the quality. Not worth the price."}, {"It's average. Works fine but could be better."}, {"Amazing! Best product ever! Highly recommend to everyone!"}, {"Terrible experience. Would not recommend to anyone."} } For i As Integer = 6 To 13 If (i - 6) Mod 2 = 0 Then sheet.Range("A" + i.ToString()).Interior.Color = Color.FromArgb(242, 242, 242) End If sheet.Range("A" + i.ToString()).Borders.LineStyle = BorderLineStyle.Thin sheet.Range("A" + i.ToString()).Borders.Color = Color.FromArgb(200, 200, 200) sheet.Range("A" + i.ToString()).WrapText = True Next sheet.Range("B6").Formula2 = "=AI.TEXTSENTIMENT(A6:A13,""Positive"",""Negative"",""Neutral"")" For i As Integer = 6 To 13 sheet.Range("B" + i.ToString()).Font.Bold = True sheet.Range("B" + i.ToString()).Font.Size = 11 sheet.Range("B" + i.ToString()).HorizontalAlignment = HorizontalAlignment.Center sheet.Range("B" + i.ToString()).Borders.LineStyle = BorderLineStyle.Medium sheet.Range("B" + i.ToString()).Borders.Color = Color.FromArgb(200, 200, 200) Next '============ Add Conditional Formatting for Sentiment Results ============ Dim positiveCondition As IFormatCondition = CType(sheet.Range("B6:B13").FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=""Positive""", Nothing ), IFormatCondition) positiveCondition.Interior.Color = Color.FromArgb(226, 239, 218) positiveCondition.Font.Color = Color.FromArgb(0, 128, 0) Dim negativeCondition As IFormatCondition = CType(sheet.Range("B6:B13").FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=""Negative""", Nothing ), IFormatCondition) negativeCondition.Interior.Color = Color.FromArgb(255, 199, 206) negativeCondition.Font.Color = Color.FromArgb(192, 0, 0) Dim neutralCondition As IFormatCondition = CType(sheet.Range("B6:B13").FormatConditions.Add( FormatConditionType.CellValue, FormatConditionOperator.Equal, "=""Neutral""", Nothing ), IFormatCondition) neutralCondition.Interior.Color = Color.FromArgb(255, 242, 204) neutralCondition.Font.Color = Color.FromArgb(128, 100, 0) workbook.Calculate() ' As AI functions operate on an asynchronous computation function, ' it Is necessary to await the completion of their calculation processes. workbook.WaitForCalculationToFinish() ' Set print settings sheet.PageSetup.FitToPagesTall = 1 sheet.PageSetup.FitToPagesWide = 1 sheet.PageSetup.IsPercentScale = False sheet.PageSetup.Orientation = PageOrientation.Landscape ' save to a pdf file workbook.Save("Textsentiment.pdf")