Formatting Smart Markers

Copy Style Attribute

Sometimes, when using smart markers, you want to copy the style of the cell that contains the smart marker tags. You can use the CopyStyle attribute of the smart marker’s tags for this purpose.

Copying Styles from Cells with Smart Markers

This example uses a simple template Microsoft Excel file with two markers in the A2 and B2 cells. The marker pasted in cell B2 uses the CopyStyle attribute, whereas the marker in cell A2 does not. Apply simple formatting (for example, set the font color to red and set the cell fill color to yellow).

When executing the code, Aspose.Cells copies the formatting to all the records in column B but does not keep the formatting in column A.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create Students DataTable
DataTable dtStudent = new DataTable("Student");
// Define a field in it
DataColumn dcName = new DataColumn("Name", typeof(string));
dtStudent.Columns.Add(dcName);
// Add three rows to it
DataRow drName1 = dtStudent.NewRow();
DataRow drName2 = dtStudent.NewRow();
DataRow drName3 = dtStudent.NewRow();
drName1["Name"] = "John";
drName2["Name"] = "Jack";
drName3["Name"] = "James";
dtStudent.Rows.Add(drName1);
dtStudent.Rows.Add(drName2);
dtStudent.Rows.Add(drName3);
string filePath = dataDir + "TestSmartMarkers.xlsx";
// Create a workbook from Smart Markers template file
Workbook workbook = new Workbook(filePath);
// Instantiate a new WorkbookDesigner
WorkbookDesigner designer = new WorkbookDesigner();
// Specify the Workbook
designer.Workbook = workbook;
// Set the Data Source
designer.SetDataSource(dtStudent);
// Process the smart markers
designer.Process();
// Save the Excel file
workbook.Save(dataDir+ "output.xlsx", SaveFormat.Xlsx);

Adding Custom Labels

Introduction

While working with Smart Markers' grouping data feature, sometimes you need to add your own custom labels to the summary row. You also want to concatenate the Column’s name with that Label, e.g “Sub Total of Orders”. Aspose.Cells provides you Label and LabelPosition attributes, so you may place your custom labels in the Smart Markers while concatenating with the Subtotal rows in grouping data.

Adding custom Labels to concatenate with the Subtotal rows in Smart Markers

This example uses a data file and a template file with a few markers in the cells. When executing the code, Aspose.Cells adds some custom labels to the summary rows for the grouped data.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate the workbook from a template file that contains Smart Markers
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
Workbook designer = new Workbook(dataDir + "SmartMarker_Designer.xlsx");
// Export data from the first worksheet to fill a data table
DataTable dt = workbook.Worksheets[0].Cells.ExportDataTable(0, 0, 11, 5, true);
// Set the table name
dt.TableName = "Report";
// Instantiate a new WorkbookDesigner
WorkbookDesigner d = new WorkbookDesigner();
// Specify the workbook to the designer book
d.Workbook = designer;
// Set the data source
d.SetDataSource(dt);
// Process the smart markers
d.Process();
// Save the Excel file
designer.Save(dataDir + "output.xlsx", SaveFormat.Xlsx);