//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 Translate Demo"; sheet.Columns[0].ColumnWidth = 40; sheet.Columns[1].ColumnWidth = 20; sheet.Columns[2].ColumnWidth = 45; // ============ Example 1: Single Text Translation ============ sheet.Range["A1:C1"].Merge(); sheet.Range["A1"].Value = "Example 1: Single Text Translation"; sheet.Range["A1"].Font.Bold = true; sheet.Range["A1"].Font.Size = 14; 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 = 30; 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:C3"].Merge(); sheet.Range["B3"].Value = "=AI.TRANSLATE(A6, B6)"; sheet.Range["B3"].Font.Italic = true; sheet.Range["B3"].Font.Color = Color.FromArgb(68, 114, 196); sheet.Range["A5:C5"].Value = new object[,] { { "Source Text (English)", "Target Lang", "AI Translation" } }; sheet.Range["A5:C5"].Font.Bold = true; sheet.Range["A5:C5"].Interior.Color = Color.FromArgb(155, 194, 230); sheet.Range["A5:C5"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["A6"].Value = "Hello, how are you today?"; sheet.Range["B6"].Value = "zh-CN"; sheet.Range["B6"].Font.Bold = true; sheet.Range["B6"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["C6"].Formula2 = "=AI.TRANSLATE(A6, B6)"; sheet.Range["C6"].Font.Italic = true; sheet.Range["C6"].Font.Size = 11; sheet.Range["A6:C6"].Borders.LineStyle = BorderLineStyle.Thin; sheet.Range["A6:C6"].Borders.Color = Color.FromArgb(200, 200, 200); // ============ Example 2: Batch Translation ============ sheet.Range["A9:C9"].Merge(); sheet.Range["A9"].Value = "Example 2: Batch Translation (English → Japanese)"; sheet.Range["A9"].Font.Bold = true; sheet.Range["A9"].Font.Size = 14; sheet.Range["A9"].Font.Color = Color.White; sheet.Range["A9"].Interior.Color = Color.FromArgb(90, 126, 158); sheet.Range["A9"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["A9"].VerticalAlignment = VerticalAlignment.Center; sheet.Range["A9"].RowHeight = 30; sheet.Range["A11"].Value = "Formula:"; sheet.Range["A11"].Font.Bold = true; sheet.Range["A11"].Font.Size = 11; sheet.Range["A11"].Interior.Color = Color.FromArgb(217, 225, 242); sheet.Range["B11:C11"].Merge(); sheet.Range["B11"].Value = "=AI.TRANSLATE(A14:A18, B14)"; sheet.Range["B11"].Font.Italic = true; sheet.Range["B11"].Font.Color = Color.FromArgb(68, 114, 196); sheet.Range["A13:C13"].Value = new object[,] { { "Source Text (English)", "Target Lang", "AI Translation (Japanese)" } }; sheet.Range["A13:C13"].Font.Bold = true; sheet.Range["A13:C13"].Interior.Color = Color.FromArgb(155, 194, 230); sheet.Range["A13:C13"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["A14:A18"].Value = new object[,] { { "Good morning!" }, { "Thank you very much." }, { "How much does this cost?" }, { "Where is the nearest restaurant?" }, { "Have a nice day!" } }; sheet.Range["A14:C18"].Borders.LineStyle = BorderLineStyle.Thin; sheet.Range["A14:C18"].Borders.Color = Color.FromArgb(200, 200, 200); sheet.Range["B14"].Value = "ja-JP"; sheet.Range["B14:B18"].Merge(); sheet.Range["B14"].Font.Bold = true; sheet.Range["B14"].Font.Size = 12; sheet.Range["B14"].HorizontalAlignment = HorizontalAlignment.Center; sheet.Range["B14"].VerticalAlignment = VerticalAlignment.Center; sheet.Range["B14:B18"].Borders.LineStyle = BorderLineStyle.Thin; sheet.Range["B14:B18"].Borders.Color = Color.FromArgb(200, 200, 200); sheet.Range["C14"].Formula2 = "=AI.TRANSLATE(A14:A18, B14)"; sheet.Range["C14:C18"].Font.Italic = true; // Add section separators sheet.Range["A1:C7"].Borders[BordersIndex.EdgeBottom].LineStyle = BorderLineStyle.Double; sheet.Range["A1:C7"].Borders[BordersIndex.EdgeBottom].Color = Color.FromArgb(142, 172, 219); 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("Translate.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 Translate Demo" sheet.Columns(0).ColumnWidth = 40 sheet.Columns(1).ColumnWidth = 20 sheet.Columns(2).ColumnWidth = 45 ' ============ Example 1: Single Text Translation ============ sheet.Range("A1:C1").Merge() sheet.Range("A1").Value = "Example 1: Single Text Translation" sheet.Range("A1").Font.Bold = True sheet.Range("A1").Font.Size = 14 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 = 30 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:C3").Merge() sheet.Range("B3").Value = "=AI.TRANSLATE(A6, B6)" sheet.Range("B3").Font.Italic = True sheet.Range("B3").Font.Color = Color.FromArgb(68, 114, 196) sheet.Range("A5:C5").Value = New Object(,) { {"Source Text (English)", "Target Lang", "AI Translation"} } sheet.Range("A5:C5").Font.Bold = True sheet.Range("A5:C5").Interior.Color = Color.FromArgb(155, 194, 230) sheet.Range("A5:C5").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("A6").Value = "Hello, how are you today?" sheet.Range("B6").Value = "zh-CN" sheet.Range("B6").Font.Bold = True sheet.Range("B6").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("C6").Formula2 = "=AI.TRANSLATE(A6, B6)" sheet.Range("C6").Font.Italic = True sheet.Range("C6").Font.Size = 11 sheet.Range("A6:C6").Borders.LineStyle = BorderLineStyle.Thin sheet.Range("A6:C6").Borders.Color = Color.FromArgb(200, 200, 200) ' ============ Example 2: Batch Translation ============ sheet.Range("A9:C9").Merge() sheet.Range("A9").Value = "Example 2: Batch Translation (English → Japanese)" sheet.Range("A9").Font.Bold = True sheet.Range("A9").Font.Size = 14 sheet.Range("A9").Font.Color = Color.White sheet.Range("A9").Interior.Color = Color.FromArgb(90, 126, 158) sheet.Range("A9").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("A9").VerticalAlignment = VerticalAlignment.Center sheet.Range("A9").RowHeight = 30 sheet.Range("A11").Value = "Formula:" sheet.Range("A11").Font.Bold = True sheet.Range("A11").Font.Size = 11 sheet.Range("A11").Interior.Color = Color.FromArgb(217, 225, 242) sheet.Range("B11:C11").Merge() sheet.Range("B11").Value = "=AI.TRANSLATE(A14:A18, B14)" sheet.Range("B11").Font.Italic = True sheet.Range("B11").Font.Color = Color.FromArgb(68, 114, 196) sheet.Range("A13:C13").Value = New Object(,) { {"Source Text (English)", "Target Lang", "AI Translation (Japanese)"} } sheet.Range("A13:C13").Font.Bold = True sheet.Range("A13:C13").Interior.Color = Color.FromArgb(155, 194, 230) sheet.Range("A13:C13").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("A14:A18").Value = New Object(,) { {"Good morning!"}, {"Thank you very much."}, {"How much does this cost?"}, {"Where is the nearest restaurant?"}, {"Have a nice day!"} } sheet.Range("A14:C18").Borders.LineStyle = BorderLineStyle.Thin sheet.Range("A14:C18").Borders.Color = Color.FromArgb(200, 200, 200) sheet.Range("B14").Value = "ja-JP" sheet.Range("B14:B18").Merge() sheet.Range("B14").Font.Bold = True sheet.Range("B14").Font.Size = 12 sheet.Range("B14").HorizontalAlignment = HorizontalAlignment.Center sheet.Range("B14").VerticalAlignment = VerticalAlignment.Center sheet.Range("B14:B18").Borders.LineStyle = BorderLineStyle.Thin sheet.Range("B14:B18").Borders.Color = Color.FromArgb(200, 200, 200) sheet.Range("C14").Formula2 = "=AI.TRANSLATE(A14:A18, B14)" sheet.Range("C14:C18").Font.Italic = True ' Add section separators sheet.Range("A1:C7").Borders(BordersIndex.EdgeBottom).LineStyle = BorderLineStyle.Double sheet.Range("A1:C7").Borders(BordersIndex.EdgeBottom).Color = Color.FromArgb(142, 172, 219) 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("Translate.pdf")