在智能标记字段中使用公式参数
Contents
[
Hide
]
可能的使用场景
有时,您想在智能标记字段中嵌入公式。本文介绍了如何使用公式在智能标记字段中嵌入公式的参数。
在智能标记字段中使用公式参数
以下示例代码将公式嵌入名为TestFormula的智能标记字段中,其数据源名称为MyDataSource,因此带有公式参数的完整字段类似于&=MyDataSource.TestFormula(formula),代码执行后,最终输出Excel文件将在 A1 到 A5 的单元格中包含公式。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Create a datatable and add column named TestFormula | |
DataTable dt = new DataTable(); | |
dt.Columns.Add("TestFormula"); | |
//Create first row with formula (which basically concatenates three strings) | |
DataRow dr = dt.NewRow(); | |
dr["TestFormula"] = "=\"01-This \" & \"is \" & \"concatenation\""; | |
dt.Rows.Add(dr); | |
//Create second row like above | |
dr = dt.NewRow(); | |
dr["TestFormula"] = "=\"02-This \" & \"is \" & \"concatenation\""; | |
dt.Rows.Add(dr); | |
//Create third row like above | |
dr = dt.NewRow(); | |
dr["TestFormula"] = "=\"03-This \" & \"is \" & \"concatenation\""; | |
dt.Rows.Add(dr); | |
//Create fourth row like above | |
dr = dt.NewRow(); | |
dr["TestFormula"] = "=\"04-This \" & \"is \" & \"concatenation\""; | |
dt.Rows.Add(dr); | |
//Create fifth row like above | |
dr = dt.NewRow(); | |
dr["TestFormula"] = "=\"05-This \" & \"is \" & \"concatenation\""; | |
dt.Rows.Add(dr); | |
//Set the name of the data table | |
dt.TableName = "MyDataSource"; | |
//Create a workbook | |
Workbook wb = new Workbook(); | |
//Access first worksheet | |
Worksheet ws = wb.Worksheets[0]; | |
//Put the smart marker field with formula parameter in cell A1 | |
ws.Cells["A1"].PutValue("&=MyDataSource.TestFormula(Formula)"); | |
//Create workbook designer, set data source and process it | |
WorkbookDesigner wd = new WorkbookDesigner(wb); | |
wd.SetDataSource(dt); | |
wd.Process(); | |
//Save the workbook in xlsx format | |
wb.Save(outputDir + "outputUsingFormulaParameterInSmartMarkerField.xlsx"); |