スマート マーカーを使用してデータをスマートにインポートおよび配置する
序章
スマートマーカーAspose.Cells Excel デザイナー スプレッドシートに配置する情報を Aspose.Cells に知らせるために使用されます。スマート マーカーを使用すると、特定の情報と書式のみを含むテンプレートを作成できます。
デザイナー スプレッドシートとスマート マーカー
Designer スプレッドシートは、視覚的な書式設定、数式、およびスマート マーカーを含む標準の Excel ファイルです。プロジェクトからの情報や関連する連絡先の情報など、1 つ以上のデータ ソースを参照するスマート マーカーを含めることができます。スマート マーカーは、情報が必要なセルに書き込まれます。
すべてのスマート マーカーは &= で始まります。データ マーカーの例は &=Party.FullName です。データ マーカーの結果が複数の項目 (たとえば、完全な行) になる場合、次の行は自動的に下に移動され、新しい情報のためのスペースが確保されます。したがって、小計と合計をデータ マーカーの直後の行に配置して、挿入されたデータに基づいて計算を行うことができます。挿入された行を計算するには、次を使用します。動的数式.
スマート マーカーは、情報元とフィールド名ほとんどの情報の部品。特別な情報は、変数と変数配列で渡すこともできます。変数は常に 1 つのセルのみを埋めますが、変数配列は複数のセルを埋めます。セルごとに 1 つのデータ マーカーのみを使用します。未使用のスマート マーカーは削除されます。
スマート マーカーには、パラメーターを含めることもできます。パラメータを使用すると、情報のレイアウト方法を変更できます。これらは、カンマ区切りのリストとして括弧内のスマート マーカーの末尾に追加されます。
スマート マーカーのオプション
&=データソース.フィールド名 &=[データソース].[フィールド名]&=$変数名 &=$変数配列 &==動的式 &=&=RepeatDynamicFormula
パラメーター
次のパラメータが許可されています。
- noadd - データに合わせて余分な行を追加しないでください。
- スキップ:n - データの各行に対して n 行をスキップします。
- 昇順:nまた降順:n - スマート マーカーでデータを並べ替えます。 n が 1 の場合、列はソーターの最初のキーです。データ ソースの処理後に、データが並べ替えられます。例: &=Table1.Field3(昇順:1)。
- 水平 上から下ではなく、左から右にデータを書き込みます。
- 数値 可能であれば、テキストを数値に変換します。
- シフト 下または右にシフトし、データに合わせて余分な行または列を作成します。 shift パラメータは、Microsoft Excel と同じように機能します。たとえば、Microsoft Excel では、セルの範囲を選択するときに、右クリックして選択します。入れると指定するセルを下にシフト, セルを右にシフトおよびその他のオプション。要するに、シフトパラメータは、垂直/通常 (上から下) または水平 (左から右) のスマート マーカーに対して同じ機能を果たします。
- コピースタイル 基本セルのスタイルをその列のすべてのセルにコピーします。
パラメータ noadd と skip を組み合わせて、交互の行にデータを挿入できます。テンプレートは下から上に処理されるため、最初の行に noadd を追加して、別の行の前に余分な行が挿入されないようにする必要があります。
複数のパラメーターがある場合は、スペースを入れずにコンマで区切ります: parameterA、parameterB、parameterC
次のスクリーンショットは、1 行おきにデータを挿入する方法を示しています。
テンプレートファイル | 出力ファイル |
---|---|
![]() |
![]() |
動的数式
動的数式を使用すると、数式がエクスポート プロセス中に挿入される行を参照している場合でも、Excel 数式をセルに挿入できます。動的数式は、挿入された行ごとに繰り返すことも、データ マーカーが配置されているセルのみを使用することもできます。
動的数式では、次の追加オプションを使用できます。
- r - 現在の行番号。
- 2、-1 - 現在の行番号へのオフセット。
例えば:
&=&=B{-1}/C{-1}~(skip:1)
動的数式マーカーでは、「-1」は除算演算用に設定される B 列と C 列のそれぞれの現在の行へのオフセットを示し、スキップ パラメーターは 1 行です。さらに、次の文字を指定する必要があります。
"~"
動的数式でさらにパラメータを適用するための区切り文字として。
次のスクリーンショットは、動的数式の繰り返しと結果の Excel ワークシートを示しています。
テンプレートファイル | 出力ファイル |
---|---|
![]() |
![]() |
Cell “C1” には数式が含まれていますA1*B1 、セル「C2」が含まれていますA2*B2セル「C3」にはA3*B3. |
スマートマーカーの処理は非常に簡単です。以下は 2 つのコード スニペットです。1 つは C# に、もう 1 つは VB にあり、その方法を示しています。
// 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 directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
//set the file path of designer spreadsheet containing smart markers | |
string designerFile = "templateDynamicFormulas.xlsx"; | |
//create your data set | |
DataSet dataset = new DataSet(); | |
DataTable dt = new DataTable("Answer"); | |
dataset.Tables.Add(dt); | |
DataColumn price = new DataColumn("Price", typeof(double)); | |
DataColumn amount = new DataColumn("Amount", typeof(int)); | |
dt.Columns.Add(price); | |
dt.Columns.Add(amount); | |
dt.Rows.Add(100.00, 2); | |
dt.Rows.Add(75.25, 3); | |
dt.Rows.Add(25.00, 5); | |
if (designerFile != null) | |
{ | |
// Instantiating a WorkbookDesigner object | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
// Open a designer spreadsheet containing smart markers | |
designer.Workbook = new Workbook(designerFile); | |
// Set the data source for the designer spreadsheet | |
designer.SetDataSource(dataset); | |
// Process the smart markers | |
designer.Process(); | |
} |
変数配列の使用
次のコード例は、スマート マーカーで変数配列を使用する方法を示しています。ワークブックの最初のワークシートの A1 セルに変数配列マーカーを動的に配置します。このセルには、マーカーに設定した値の文字列が含まれており、マーカーを処理して、マーカーに対してセルにデータを入力します。最後に、Excel ファイルを保存します。
// 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 a new Workbook designer. | |
WorkbookDesigner report = new WorkbookDesigner(); | |
// Get the first worksheet of the workbook. | |
Worksheet w = report.Workbook.Worksheets[0]; | |
// Set the Variable Array marker to a cell. | |
// You may also place this Smart Marker into a template file manually in Ms Excel and then open this file via Workbook. | |
w.Cells["A1"].PutValue("&=$VariableArray"); | |
// Set the DataSource for the marker(s). | |
report.SetDataSource("VariableArray", new string[] { "English", "Arabic", "Hindi", "Urdu", "French" }); | |
// Process the markers. | |
report.Process(false); | |
// Save the Excel file. | |
report.Workbook.Save(dataDir + "output.xlsx"); |
データのグループ化
一部の Excel レポートでは、読みやすく分析しやすくするために、データをグループに分割する必要がある場合があります。データをグループに分割する主な目的の 1 つは、レコードの各グループに対して計算を実行する (集計操作を実行する) ことです。
Aspose.Cells スマート マーカーを使用すると、フィールドごとにデータをグループ化し、データ セットまたはデータ グループの間に集計行を配置できます。たとえば、Customers.CustomerID でデータをグループ化すると、グループが変更されるたびに集計レコードを追加できます。
パラメーター
以下は、データのグループ化に使用されるスマート マーカー パラメーターの一部です。
グループ:ノーマル/マージ/リピート
選択できる 3 種類のグループをサポートしています。
- 正常 フィールド値によるグループ化は、列内の対応するレコードに対して繰り返されません。代わりに、データ グループごとに 1 回出力されます。
- マージ 通常のパラメータと同じ動作ですが、グループ セットごとにフィールドごとにグループ内のセルをマージします。
- 繰り返す フィールド値によるグループ化は、対応するレコードに対して繰り返されます。
例: &=Customers.CustomerID(group:merge)
スキップ
各グループの後、指定された数の行をスキップします。
例: &=Employees.EmployeeID(group:normal,skip:1)
小計N
グループ化フィールドに関連する指定されたフィールド データの集計操作を実行します。 N は、データのリスト内の小計を計算するときに使用される関数を指定する 1 から 11 までの数値を表します。 (1=AVERAGE、2=COUNT、3=COUNTA、4=MAX、5=MIN、…9=SUM など) 詳細については、Microsoft Excel のヘルプの小計リファレンスを参照してください。
形式は実際には次のように述べています。 subtotalN:Ref Ref はグループごとの列を参照します。
例えば、
- &=Products.Units(subtotal9:Products.ProductID) は集計関数を指定します単位に関するフィールド製品番号のフィールド製品テーブル。
- &=Tabx.Col3(subtotal9:Tabx.Col1) は、Col3フィールドのグループ化Col1テーブルでTabx.
- &=Table1.ColumnD(subtotal9:Table1.ColumnA&Table1.ColumnB) で集計関数を指定列 Dフィールドのグループ化列Aと列Bテーブルで表1.
この例は、実際のグループ化パラメーターの一部を示しています。 Northwind.mdb Microsoft Access データベースを使用し、“Order Details” という名前のテーブルからデータを抽出します。 Microsoft Excel で SmartMarker_Designer.xls というデザイナー ファイルを作成し、ワークシートのさまざまなセルにスマート マーカーを配置します。マーカーは、ワークシートを埋めるために処理されます。データは、グループ フィールドごとに配置および編成されます。
デザイナー ファイルには 2 つのワークシートがあります。最初に、下のスクリーンショットに示すように、グループ化パラメーターを使用してスマート マーカーを配置します。 3 つのスマート マーカー (グループ化パラメーター付き) が配置されます。 &=[注文内容].OrderID(group:merge,skip:1), &=[注文明細].Quantity(subtotal9:注文明細.OrderID)、および &=[Order Details].UnitPrice(subtotal9:Order Details.OrderID) はそれぞれ A5、B5、C5 に入ります。
SmartMarker_Designer.xls ファイルの最初のワークシートで、スマート マーカーが含まれています。 |
---|
![]() |
デザイナー ファイルの 2 番目のワークシートには、下の図に示すように、さらにスマート マーカーを配置します。次のスマート マーカーを配置します。 |
&=[注文内容].OrderID(グループ:通常), |
&=[注文内容].数量, |
&=[注文内容].単価, |
&=&=B(r)*C(r)、および |
&=subtotal9:Order Details.OrderID をそれぞれ A5、B5、C5、D5、C6 に変換します。 |
混合スマート マーカーを示す SmartMarker_Designer.xls ファイルの 2 番目のワークシート。 |
---|
![]() |
この例で使用されているソース コードは次のとおりです。 |
private void SmartMarkers_GroupingData() | |
{ | |
//Examples-CSharp-SmartMarkers-GroupingData-1.cs | |
// 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 a connection object, specify the provider info and set the data source. | |
OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + dataDir + "Northwind.mdb"); | |
// Open the connection object. | |
con.Open(); | |
// Create a command object and specify the SQL query. | |
OleDbCommand cmd = new OleDbCommand("Select * from [Order Details]", con); | |
// Create a data adapter object. | |
OleDbDataAdapter da = new OleDbDataAdapter(); | |
// Specify the command. | |
da.SelectCommand = cmd; | |
// Create a dataset object. | |
DataSet ds = new DataSet(); | |
// Fill the dataset with the table records. | |
da.Fill(ds, "Order Details"); | |
// Create a datatable with respect to dataset table. | |
DataTable dt = ds.Tables["Order Details"]; | |
// Create WorkbookDesigner object. | |
WorkbookDesigner wd = new WorkbookDesigner(); | |
// Open the template file (which contains smart markers). | |
wd.Workbook = new Workbook(dataDir + "Designer.xlsx"); | |
// Set the datatable as the data source. | |
wd.SetDataSource(dt); | |
// Process the smart markers to fill the data into the worksheets. | |
wd.Process(true); | |
// Save the excel file. | |
wd.Workbook.Save(dataDir + "output.xlsx"); | |
} | |
class OleDbCommand | |
{ | |
private string p; | |
private OleDbConnection con; | |
public OleDbCommand(string p, OleDbConnection con) | |
{ | |
// TODO: Complete member initialization | |
this.p = p; | |
this.con = con; | |
} | |
} | |
class OleDbConnection | |
{ | |
private string p; | |
public OleDbConnection(string p) | |
{ | |
// TODO: Complete member initialization | |
this.p = p; | |
} | |
internal void Open() | |
{ | |
} | |
} | |
class OleDbDataAdapter | |
{ | |
public OleDbCommand SelectCommand { get; set; } | |
internal void Fill(System.Data.DataSet ds, string p) | |
{ | |
} | |
} |
匿名型またはカスタム オブジェクトの使用
Aspose.Cells は、スマート マーカーで匿名型またはカスタム オブジェクトもサポートします。次の例は、これがどのように機能するかを示しています。スマート マーカーを使用して動的オブジェクトからデータをインポートするには、次の記事を参照してください。
private void SmartMarkers_UsingAnonymousTypes() | |
{ | |
// 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 workbookdesigner object. | |
WorkbookDesigner report = new WorkbookDesigner(); | |
// Get the first worksheet(default sheet) in the workbook. | |
Aspose.Cells.Worksheet sheet = report.Workbook.Worksheets[0]; | |
// Input some markers to the cells. | |
sheet.Cells["A1"].PutValue("Name"); | |
sheet.Cells["B1"].PutValue("Age"); | |
sheet.Cells["A2"].PutValue("&=MyProduct.Name"); | |
sheet.Cells["B2"].PutValue("&=MyProduct.Age"); | |
// Instantiate the list collection based on the custom class. | |
IList<Person> list = new List<Person>(); | |
// Provide values for the markers using the custom class object. | |
list.Add(new Person("Simon", 30)); | |
list.Add(new Person("Johnson", 33)); | |
// Set the data source. | |
report.SetDataSource("MyProduct", list); | |
// Process the markers. | |
report.Process(false); | |
// Save the excel file. | |
report.Workbook.Save(dataDir + "Smart Marker Customobjects.xls"); | |
} | |
// Definition of Custom class. | |
public class Person | |
{ | |
private string m_Name; | |
public string Name | |
{ | |
get { return m_Name; } | |
set { m_Name = value; } | |
} | |
private int m_Age; | |
public int Age | |
{ | |
get { return m_Age; } | |
set { m_Age = value; } | |
} | |
internal Person(string name, int age) | |
{ | |
this.m_Name = name; | |
this.m_Age = age; | |
} | |
} |
画像マーカー
Aspose.Cells スマートマーカーは画像マーカーもサポートしています。このセクションでは、スマート マーカーを使用して画像を挿入する方法について説明します。
画像パラメータ
画像を管理するためのスマート マーカー パラメータ。
- 画像:FitToCell - 画像をセルの行の高さと列の幅に自動調整します。
- 写真:ScaleN - 高さと幅を N パーセントにスケーリングします。
- 写真:幅:忍&高さ:忍 高さ N インチ、幅 N インチのイメージをレンダリングします。 Left と Top の位置をポイント単位で指定することもできます。
この例で使用されているソース コードは次のとおりです。
// 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); | |
// Get the image data. | |
byte[] imageData = File.ReadAllBytes(dataDir+ "aspose-logo.jpg"); | |
// Create a datatable. | |
DataTable t = new DataTable("Table1"); | |
// Add a column to save pictures. | |
DataColumn dc = t.Columns.Add("Picture"); | |
// Set its data type. | |
dc.DataType = typeof(object); | |
// Add a new new record to it. | |
DataRow row = t.NewRow(); | |
row[0] = imageData; | |
t.Rows.Add(row); | |
// Add another record (having picture) to it. | |
imageData = File.ReadAllBytes(dataDir+ "image2.jpg"); | |
row = t.NewRow(); | |
row[0] = imageData; | |
t.Rows.Add(row); | |
// Create WorkbookDesigner object. | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
// Open the template Excel file. | |
designer.Workbook = new Workbook(dataDir+ "TestSmartMarkers.xlsx"); | |
// Set the datasource. | |
designer.SetDataSource(t); | |
// Process the markers. | |
designer.Process(); | |
// Save the Excel file. | |
designer.Workbook.Save(dataDir+ "output.xls"); |
ネストされたオブジェクトの使用
Aspose.Cells は、スマート マーカーでネストされたオブジェクトをサポートします。ネストされたオブジェクトは単純でなければなりません。シンプルなテンプレート ファイルを使用します。ネストされたスマート マーカーを含むデザイナー スプレッドシートを参照してください。
ネストされたスマート マーカーを示す SM_NestedObjects.xlsx ファイルの最初のワークシート。 |
---|
![]() |
次の例は、これがどのように機能するかを示しています。 |
private void SmartMarkers_UsingNestedObjects() | |
{ | |
// 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); | |
// ****** Program ****** | |
// Initialize WorkbookDesigner object | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
// Load the template file | |
designer.Workbook = new Workbook(dataDir + "SM_NestedObjects.xlsx"); | |
// Instantiate the List based on the class | |
System.Collections.Generic.ICollection<Individual> list = new System.Collections.Generic.List<Individual>(); | |
// Create an object for the Individual class | |
Individual p1 = new Individual("Damian", 30); | |
// Create the relevant Wife class for the Individual | |
p1.Wife = new Wife("Dalya", 28); | |
// Create another object for the Individual class | |
Individual p2 = new Individual("Mack", 31); | |
// Create the relevant Wife class for the Individual | |
p2.Wife = new Wife("Maaria", 29); | |
// Add the objects to the list | |
list.Add(p1); | |
list.Add(p2); | |
// Specify the DataSource | |
designer.SetDataSource("Individual", list); | |
// Process the markers | |
designer.Process(false); | |
// Save the Excel file. | |
designer.Workbook.Save(dataDir + "output.xlsx"); | |
} | |
class Individual | |
{ | |
private String m_Name; | |
public String Name | |
{ | |
get { return m_Name; } | |
set { m_Name = value; } | |
} | |
private int m_Age; | |
public int Age | |
{ | |
get { return m_Age; } | |
set { m_Age = value; } | |
} | |
internal Individual(string name, int age) | |
{ | |
this.Name = name; | |
this.Age = age; | |
} | |
private Wife m_Wife; | |
public Wife Wife | |
{ | |
get { return m_Wife; } | |
set { m_Wife = value; } | |
} | |
} | |
public class Wife | |
{ | |
public Wife(string name, int age) | |
{ | |
this.m_name = name; | |
this.m_age = age; | |
} | |
private string m_name; | |
public string Name | |
{ | |
get { return m_name; } | |
set { m_name = value; } | |
} | |
private int m_age; | |
public int Age | |
{ | |
get { return m_age; } | |
set { m_age = value; } | |
} | |
} |
汎用リストをネストされたオブジェクトとして使用する
Aspose.Cells は、ネストされたオブジェクトとしてジェネリック リストの使用もサポートするようになりました。次のコードで生成された出力 Excel ファイルのスクリーンショットを確認してください。スクリーンショットでわかるように、Teacher オブジェクトにはネストされた複数の Student オブジェクトが含まれています。
![]() |
---|
// 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); | |
Workbook workbook = new Workbook(); | |
// Create a designer workbook | |
// Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Cells["A1"].PutValue("Teacher Name"); | |
worksheet.Cells["A2"].PutValue("&=Teacher.Name"); | |
worksheet.Cells["B1"].PutValue("Teacher Age"); | |
worksheet.Cells["B2"].PutValue("&=Teacher.Age"); | |
worksheet.Cells["C1"].PutValue("Student Name"); | |
worksheet.Cells["C2"].PutValue("&=Teacher.Students.Name"); | |
worksheet.Cells["D1"].PutValue("Student Age"); | |
worksheet.Cells["D2"].PutValue("&=Teacher.Students.Age"); | |
// Apply Style to A1:D1 | |
Range range = worksheet.Cells.CreateRange("A1:D1"); | |
Style style = workbook.CreateStyle(); | |
style.Font.IsBold = true; | |
style.ForegroundColor = Color.Yellow; | |
style.Pattern = BackgroundType.Solid; | |
StyleFlag flag = new StyleFlag(); | |
flag.All = true; | |
range.ApplyStyle(style, flag); | |
// Initialize WorkbookDesigner object | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
// Load the template file | |
designer.Workbook = workbook; | |
System.Collections.Generic.List<Teacher> list = new System.Collections.Generic.List<Teacher>(); | |
// Create an object for the Teacher class | |
Teacher h1 = new Teacher("Mark John", 30); | |
// Create the relevant student objects for the Teacher object | |
h1.Students = new List<Person>(); | |
h1.Students.Add(new Person("Chen Zhao", 14)); | |
h1.Students.Add(new Person("Jamima Winfrey", 18)); | |
h1.Students.Add(new Person("Reham Smith", 15)); | |
// Create another object for the Teacher class | |
Teacher h2 = new Teacher("Masood Shankar", 40); | |
// Create the relevant student objects for the Teacher object | |
h2.Students = new List<Person>(); | |
h2.Students.Add(new Person("Karishma Jathool", 16)); | |
h2.Students.Add(new Person("Angela Rose", 13)); | |
h2.Students.Add(new Person("Hina Khanna", 15)); | |
// Add the objects to the list | |
list.Add(h1); | |
list.Add(h2); | |
// Specify the DataSource | |
designer.SetDataSource("Teacher", list); | |
// Process the markers | |
designer.Process(); | |
// Autofit columns | |
worksheet.AutoFitColumns(); | |
// Save the Excel file. | |
designer.Workbook.Save(dataDir + "output.xlsx"); | |
Smart Markers の HTML プロパティの使用
次のサンプル コードでは、スマート マーカーの HTML プロパティの使用について説明します。処理されると、HTML のため、“Hello World” の “World” が太字で表示されます。鬼ごっこ。
// 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); | |
Workbook workbook = new Workbook(); | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
designer.Workbook = workbook; | |
workbook.Worksheets[0].Cells["A1"].PutValue("&=$VariableArray(HTML)"); | |
designer.SetDataSource("VariableArray", new String[] { "Hello <b>World</b>", "Arabic", "Hindi", "Urdu", "French" }); | |
designer.Process(); | |
workbook.Save(dataDir + "output.xls"); | |
行ごとではない
現在のデフォルトの処理方法は、smartmaker を 1 行ずつ処理することです。しかし、場合によっては、同じデータ テーブルのスマート マーカーをまとめて処理する必要があります。 それらが同じ行にあるかどうか、名前付き範囲「_CellsSmartMarkers」を指定し、処理を呼び出す前に WorkbookDesigner.LineByLine を false として指定する必要があります。
||
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(); | |
Style style = workbook.CreateStyle(); | |
style.Pattern = BackgroundType.Solid; | |
style.ForegroundColor = Color.Black; | |
style.Font.Color = Color.White; | |
// Create a designer workbook | |
// Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Cells["A1"].PutValue("Teacher Name"); | |
worksheet.Cells["A1"].SetStyle(style); | |
worksheet.Cells["A2"].PutValue("&=Teacher.Name"); | |
worksheet.Cells["B1"].PutValue("Teacher Age"); | |
worksheet.Cells["B1"].SetStyle(style); | |
worksheet.Cells["B2"].PutValue("&=Teacher.Age"); | |
worksheet.Cells["A3"].PutValue("Student Name"); | |
worksheet.Cells["A3"].SetStyle(style); | |
worksheet.Cells["A4"].PutValue("&=Teacher.Students.Name"); | |
worksheet.Cells["B3"].PutValue("Student Age"); | |
worksheet.Cells["B3"].SetStyle(style); | |
worksheet.Cells["B4"].PutValue("&=Teacher.Students.Age"); | |
worksheet.AutoFitColumns(); | |
//A named range "_CellsSmartMarkers" must be added for checking which range contains all smart markers about a table. | |
worksheet.Cells.CreateRange("A1:B4").Name = "_CellsSmartMarkers"; | |
// Initialize WorkbookDesigner object | |
WorkbookDesigner designer = new WorkbookDesigner(); | |
// Load the template file | |
designer.Workbook = workbook; | |
System.Collections.Generic.List<Teacher> list = new System.Collections.Generic.List<Teacher>(); | |
// Create an object for the Teacher class | |
Teacher h1 = new Teacher("Mark John", 30); | |
// Create the relevant student objects for the Teacher object | |
h1.Students = new List<Person>(); | |
h1.Students.Add(new Person("Chen Zhao", 14)); | |
h1.Students.Add(new Person("Jamima Winfrey", 18)); | |
h1.Students.Add(new Person("Reham Smith", 15)); | |
// Create another object for the Teacher class | |
Teacher h2 = new Teacher("Masood Shankar", 40); | |
// Create the relevant student objects for the Teacher object | |
h2.Students = new List<Person>(); | |
h2.Students.Add(new Person("Karishma Jathool", 16)); | |
h2.Students.Add(new Person("Angela Rose", 13)); | |
h2.Students.Add(new Person("Hina Khanna", 15)); | |
// Add the objects to the list | |
list.Add(h1); | |
list.Add(h2); | |
// Specify the DataSource | |
designer.SetDataSource("Teacher", list); | |
designer.LineByLine = false; | |
// Process the markers | |
designer.Process(); | |
// Autofit columns | |
worksheet.AutoFitColumns(); | |
// Save the Excel file. | |
designer.Workbook.Save(dataDir + "output.xlsx"); | |
public class Person | |
{ | |
private string m_Name; | |
public string Name | |
{ | |
get { return m_Name; } | |
set { m_Name = value; } | |
} | |
private int m_Age; | |
public int Age | |
{ | |
get { return m_Age; } | |
set { m_Age = value; } | |
} | |
internal Person(string name, int age) | |
{ | |
this.m_Name = name; | |
this.m_Age = age; | |
} | |
} | |
public class Teacher | |
{ | |
private string m_Name; | |
public string Name | |
{ | |
get { return m_Name; } | |
set { m_Name = value; } | |
} | |
private int m_Age; | |
public int Age | |
{ | |
get { return m_Age; } | |
set { m_Age = value; } | |
} | |
private List<Person> mStudents; | |
public List<Person> Students | |
{ | |
get { return mStudents; } | |
set { mStudents = value; } | |
} | |
public Teacher(string name, int age) | |
{ | |
this.Name = name; | |
this.Age = age; | |
} | |
} |
スマート マーカーを使用してデータを結合する際の通知の取得
完了する前に、セル参照または処理中の特定のスマート マーカーに関する通知を取得する必要がある場合があります。これは、WorkbookDesigner.CallBack プロパティと ISmartMarkerCallBack を使用して実現できます。