Aspose.Cells可以預先定義Excel模板,然後填充數據(官方文檔:http://www.aspose.com/docs/display/cellsjava/Smart+Markers)。
設置的模板是一個標準的Excel文件,包含了可視化的格式,公式,以及標記(smart markers),並且可以引用多個數據源。
支持的標記(Smart Marker Options)
&=DataSource.FieldName 數據元是ICellsDataTable或者是JavaBean
&=[Data Source].[Field Name] 數據源是 ResultSet
&=$VariableName 一個變量
&=$VariableArray 數組變量
&==DynamicFormula 暫時不清楚
&=&=RepeatDynamicFormula 動態計算
下面介紹這兩種標記的使用方式
1、&=$VariableName 一個變量
2、&=$VariableArray 數組變量
Java代碼
String path = "SmartMarkerDesigner.xls"; InputStream inputStream = ClassLoader.getSystemResourceAsStream(path); Workbook wb = new Workbook(inputStream); WorkbookDesigner designer = new WorkbookDesigner(); designer.setWorkbook(wb); designer.setDataSource("Variable", "Single Variable"); designer.setDataSource("MultiVariable", new String[] { "Variable 1", "Variable 2", "Variable 3" }); designer.setDataSource("MultiVariable2", new String[] { "Skip 1", "Skip 2", "Skip 3" }); designer.setDataSource("Array1Dim", new String[] { "A1", "A2", "A3" }); designer.setDataSource("Array2Dim", new String[][] { { "A11", "A12", "A13" }, { "A21", "A22", "A23", "A24" }, { "A31", "A32", }, });
下面介紹JavaBean的使用方式
&=DataSource.FieldName 數據元是ICellsDataTable或者是JavaBean
&=Person.name(bean) 注意這個地方需要加上bean
其中&=&=C{r}*D{r} 是動態計算的標記
&=&= 構面跟一個表達式 r代表當前行,C{r}*D{r} 這樣在循環的時候沒一行都會計算。
上面的例子用id這列進行分組Grouping Data
normal - The group by field(s) value is not be repeated for the corresponding records in the column; instead they are printed once per data group.
分組的列不會被循環輸出,只會輸出一次。
merge - The same behavior as for the normal parameter, except that it merges the cells in the group by field(s) for each group set.
和normal一樣,但是他會合並單元格
repeat - The group by field(s) value is repeated for the corresponding records.
分組的列會循環輸出
&=subtotal9:Person.id 放在那一列下面是按照id進行分組統計。
或者&=Employee.money(subtotal9:Employee.id) 這麼寫也行,按照id進行分組統計
subtotalN (N的值可以是1-11 平均數 總行數 最大值 最小值 總和 等等。)
Performs a summary operation for a specified field data related to a group by field. The N represents numbers between 1 and 11 which specify the function used when calculating subtotals within a list of data. (1=AVERAGE, 2=COUNT, 3=COUNTA, 4=MAX, 5=MIN,...9=SUM etc.) Refer to the Subtotal reference in Microsoft Excel's help for further details.
The format actually states as:
subtotalN:Ref where Ref refers to the group by column.
&=Products.Units(subtotal9:Products.ProductID) specifies summary function upon Units field with respect to the ProductID field in the Products table.
&=Tabx.Col3(subtotal9:Tabx.Col1) specifies summary function upon the Col3 field group by Col1 in the table Tabx.
&=Table1.ColumnD(subtotal9:Table1.ColumnA&Table1.ColumnB) specifies summary function upon ColumnD field group by ColumnA and ColumnB in the table Table1.