在智能标记字段中使用Formula参数
Contents
[
Hide
]
可能的使用场景
有时,您想要在智能标记字段中嵌入公式。本文介绍了如何使用公式参数在智能标记字段中嵌入公式。
在智能标记变量名为Test及其数据源名称也为Test中嵌入公式的下面样本代码是这样子的 &=$Test(formula),在执行代码后,最终的输出Excel文件将在A1到A5单元格中具有公式。
以下示例代码将公式嵌入名为TestFormula的智能标记字段,其数据源名称为MyDataSource,因此带公式参数的完整字段如 &=MyDataSource.TestFormula(公式),执行代码后,最终输出的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"); |