格式化智能标记

复制样式属性

有时,在使用智能标记时,您想要复制包含智能标记标签的单元格的样式。为此,您可以使用智能标记标签的 CopyStyle 属性。

使用智能标记从 Cells 复制样式

此示例使用一个简单的模板 Microsoft Excel 文件,在 A2 和 B2 单元格中有两个标记。粘贴在单元格 B2 中的标记使用 CopyStyle 属性,而单元格 A2 中的标记则不使用。应用简单格式(例如,将字体颜色设置为红色的并将单元格填充颜色设置为黄色的).

执行代码时,Aspose.Cells将格式复制到B列中的所有记录,但不保留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);

添加自定义标签

介绍

在使用智能标记的分组数据功能时,有时您需要将自己的自定义标签添加到摘要行。您还希望将列的名称与该标签连接起来,例如“订单小计”。 Aspose.Cells 为您提供 Label 和 LabelPosition 属性,因此您可以将自定义标签放置在智能标记中,同时在分组数据中与小计行连接。

添加自定义标签以与智能标记中的小计行连接

这个例子使用了数据文件和一个模板文件细胞中有一些标记。执行代码时,Aspose.Cells 将一些自定义标签添加到分组数据的摘要行中。

// 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);