Category / Section
How to add text box shape in Excel charts?
1 min read
Text box shape can be added into a chart using AddTextBox property in IChartShape of XlsIO as shown in the below code sample.
C#
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
//The first worksheet object in the worksheets collection is accessed.
IWorksheet worksheet = workbook.Worksheets[0];
IChartShape chart= workbook.Worksheets[0].Charts[0];
//Adding TextBox to chart.
chart.TextBoxes.AddTextBox(1,1, 100,200);
//Setting position for TextBox.
chart.TextBoxes[0].Top = 850;
chart.TextBoxes[0].Left = 765;
//Adding text to TextBox.
chart.TextBoxes[0].Text = "Yearly Profit”;
//Save the workbook.
workbook.SaveAs("Output.xlsx");
//Close the workbook and dispose the engine.
workbook.Close();
excelEngine.Dispose();
VB
Dim excelEngine As ExcelEngine = New ExcelEngine
Dim application As IApplication = excelEngine.Excel
Dim workbook As IWorkbook = application.Workbooks.Open(“sample.xlsx”)
'The first worksheet object in the worksheets collection is accessed.
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim chart As IChartShape = workbook.Worksheets(0).Charts(0)
'Adding TextBox to chart.
chart.TextBoxes.AddTextBox(1, 1, 100, 200)
'Setting position for TextBox.
chart.TextBoxes(0).Top = 850
chart.TextBoxes(0).Left = 765
'Adding text to TextBox.
chart.TextBoxes(0).Text = " Yearly Profit”
' Save the workbook.
workbook.SaveAs("Output.xlsx”)
' Close the workbook and dispose the engine.
workbook.Close()
excelEngine.Dispose()
Chart with TextBox
The sample which illustrates the above behavior can be downloaded here.