格式化 Smart Markers

复制样式属性

有时,使用 Smart Markers 时,您希望复制包含 Smart Marker 标记的单元格的样式。您可以使用 Smart Markers 标记的 CopyStyle 属性来实现此目的。

从包含 Smart Markers 的单元格复制样式

此示例使用一个简单的模板 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);

添加自定义标签

介绍

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

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

该示例使用包含一些标记的 数据文件和一个模板文件。执行代码时,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);