Tutorial: Assembling Data in Excel with Aspose.Cells Cloud API

Prerequisites

Before starting this tutorial, make sure you have:

  1. An Aspose Cloud account with an active subscription or free trial
  2. Your Client ID and Client Secret from the Aspose Cloud Dashboard
  3. Basic understanding of Excel templates and smart markers
  4. Sample template and data files for testing (examples provided in this tutorial)

Introduction to Data Assembly in Excel

Data assembly in Excel refers to the process of combining data from multiple sources into a structured Excel report. This is particularly useful for:

  • Automating repetitive report generation
  • Creating consistent financial statements
  • Building dynamic dashboards from raw data
  • Generating customized Excel reports for different stakeholders

The Aspose.Cells Cloud API provides a powerful data assembly mechanism using smart markers, which are special tags placed in Excel templates that tell the API where and how to insert data.

Understanding Smart Markers

Smart markers are special tags you place in Excel cells that serve as placeholders for dynamic data. When processed by the Aspose.Cells API, these markers are replaced with actual data values.

The basic format of a smart marker is:

&=DataSource.FieldName

Where:

  • DataSource is the name of the data source
  • FieldName is the name of the field within that data source

Common Smart Marker Types

Smart Marker TypeFormatDescription
Simple Value&=ds.NameInserts a single value
Vertical Range&=ds.Name(1,0)Creates a vertical list of values
Horizontal Range&=ds.Name(0,1)Creates a horizontal list of values
Table&=ds.TableName(1,1)Creates a table with multiple columns
Image&=ds.Image:ImageFieldNameInserts an image
Variable&=$VariableInserts a value from a variable

Creating an Excel Template with Smart Markers

Let’s start by creating a simple Excel template for a sales report. You’ll need Excel or any compatible spreadsheet software.

Step 1: Design Your Template Layout

  1. Open a new Excel workbook
  2. Create a header section with your company name, report title, etc.
  3. Design the structure for your data tables
  4. Add any charts or graphics you want in the final report

Step 2: Add Smart Markers

Place smart markers in the cells where you want dynamic data to appear:

  1. For the report date, add: &=$ReportDate
  2. For a sales summary table, add: &=ds.Sales(1,1) in cell A10
  3. For a chart title, add: &=ds.Title

Your template might look something like this:

CellContent
B2Sales Report
B3Date: &=$ReportDate
A10&=ds.Sales(1,1)
D20&=ds.Chart:SalesChart

Step 3: Save Your Template

Save this file as template.xlsx for use with the API.

Using the Assembly API

Now we’ll use the Aspose.Cells Cloud API to process this template with actual data.

Try It Yourself: Using cURL

curl -v "https://api.aspose.cloud/v3.0/cells/assembly?datasource=ds&format=xlsx" \
-X POST \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>" \
-F "template=@template.xlsx" \
-F "ds=@data.xlsx"

Replace <your_jwt_token> with your actual authentication token. This request uses template.xlsx as the template and data.xlsx as the data source.

Understanding the API Parameters

  • datasource: The name of the data source (must match what’s used in your smart markers)
  • format: The output format (usually xlsx)
  • template: Your Excel template with smart markers
  • ds: Your data source file (can be another Excel file, JSON, XML, etc.)

Implementation in C#

// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);

// Prepare the files
var files = new Dictionary<string, Stream>
{
    { "template.xlsx", File.OpenRead("template.xlsx") },
    { "ds", File.OpenRead("data.xlsx") }
};

// Execute the assembly
var response = cellsApi.PostAssemble(
    files,
    "ds",    // datasource parameter value
    "xlsx"   // output format
);

// Save the resulting Excel file
using (var fileStream = File.Create("assembled_report.xlsx"))
{
    response.CopyTo(fileStream);
}

Console.WriteLine("Data assembly completed successfully!");

Implementation in Python

import requests
import base64
import json

# Authentication setup
client_id = 'your_client_id'
client_secret = 'your_client_secret'

# Get authentication token
auth_url = "https://api.aspose.cloud/connect/token"
auth_data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret
}
auth_response = requests.post(auth_url, data=auth_data)
token = auth_response.json().get('access_token')

# API endpoint
url = "https://api.aspose.cloud/v3.0/cells/assembly"

# Prepare the files
files = {
    'template': open('template.xlsx', 'rb'),
    'ds': open('data.xlsx', 'rb')
}

# Set parameters
params = {
    'datasource': 'ds',
    'format': 'xlsx'
}

# Set headers
headers = {
    'Authorization': f'Bearer {token}'
}

# Execute the API call
response = requests.post(url, headers=headers, params=params, files=files)

# Check if successful
if response.status_code == 200:
    # Save the response content
    result = response.json()
    for file_info in result.get('Files', []):
        # Decode and save the file
        file_content = base64.b64decode(file_info['FileContent'])
        with open('assembled_report.xlsx', 'wb') as f:
            f.write(file_content)
        print(f"Report saved as assembled_report.xlsx ({file_info['FileSize']} bytes)")
else:
    print(f"Error: {response.status_code}")
    print(response.text)

Creating Different Types of Data Sources

Aspose.Cells Cloud can use various data sources for assembly. Let’s explore the most common ones.

Using Excel as a Data Source

An Excel file is the most straightforward data source. The worksheet names become the data source names in your smart markers.

  1. Create a new Excel file named data.xlsx
  2. Create a worksheet named “Sales” with columns for Product, Quantity, Price, and Total
  3. Fill in sample data in multiple rows
  4. Create another worksheet named “Variables” with a single row for “ReportDate” and its value

Using JSON as a Data Source

JSON is a flexible option, especially for web applications:

{
  "Sales": [
    { "Product": "Product A", "Quantity": 100, "Price": 10.5, "Total": 1050 },
    { "Product": "Product B", "Quantity": 75, "Price": 12.25, "Total": 918.75 },
    { "Product": "Product C", "Quantity": 120, "Price": 8.75, "Total": 1050 }
  ],
  "Variables": {
    "ReportDate": "2025-04-04"
  },
  "Title": "Monthly Sales Summary"
}

Save this as data.json and use it with the API:

curl -v "https://api.aspose.cloud/v3.0/cells/assembly?datasource=ds&format=xlsx" \
-X POST \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>" \
-F "template=@template.xlsx" \
-F "ds=@data.json"

Advanced Smart Marker Techniques

Let’s explore some advanced techniques for more complex scenarios.

Formulas in Smart Markers

You can include formulas in your smart markers:

&=ds.Sales(1,1){"=SUM(D2:D100)"}

This will create a formula that sums column D after the data is populated.

Conditional Formatting with Smart Markers

Apply conditional formatting based on values:

&=ds.Sales(1,1){"[>1000]=BACKCOLOR:red;TEXTCOLOR:white"}

This will highlight cells with values greater than 1000 with a red background and white text.

Including Images

To include images in your reports:

  1. In your data source, include image data as base64 encoded strings
  2. In your template, use the image smart marker format:
&=ds.Logo:CompanyLogo

Nested Data Sources

For hierarchical data, you can use nested smart markers:

&=ds.Departments(1,0).Employees(1,1)

This will create a nested list of employees within departments.

Real-World Example: Financial Dashboard

Let’s put it all together with a comprehensive example: creating a financial dashboard.

Template Structure

Design your template with sections for:

  1. Company overview
  2. Financial summary
  3. Department-specific financial data
  4. Performance charts

Smart Marker Implementation

Example smart markers for this dashboard:

&=$CompanyName                               // A1: Company name
&=$ReportDate                                // A2: Report date
&=ds.FinancialSummary(1,1)                   // A5: Financial summary table
&=ds.Departments(1,0).FinancialData(1,1)     // A15: Department financial tables
&=ds.Chart:RevenueChart                      // D5: Revenue chart
&=ds.Chart:ExpensesChart                     // D15: Expenses chart

Data Source JSON

{
  "FinancialSummary": [
    { "Metric": "Total Revenue", "Value": 1250000, "PreviousYear": 1150000, "Change": "8.7%" },
    { "Metric": "Total Expenses", "Value": 875000, "PreviousYear": 810000, "Change": "8.0%" },
    { "Metric": "Net Profit", "Value": 375000, "PreviousYear": 340000, "Change": "10.3%" }
  ],
  "Departments": [
    {
      "Name": "Sales",
      "FinancialData": [
        { "Metric": "Revenue", "Q1": 125000, "Q2": 150000, "Q3": 175000, "Q4": 200000 },
        { "Metric": "Expenses", "Q1": 75000, "Q2": 80000, "Q3": 90000, "Q4": 95000 }
      ]
    },
    {
      "Name": "Marketing",
      "FinancialData": [
        { "Metric": "Revenue", "Q1": 50000, "Q2": 60000, "Q3": 70000, "Q4": 80000 },
        { "Metric": "Expenses", "Q1": 40000, "Q2": 45000, "Q3": 50000, "Q4": 55000 }
      ]
    }
  ],
  "Variables": {
    "CompanyName": "Acme Corporation",
    "ReportDate": "April 4, 2025"
  }
}

Troubleshooting Common Issues

Issue: Smart Markers Not Processing

Solution: Verify that the data source name in your API call matches the one used in your smart markers. Check that your smart marker syntax is correct.

Issue: Missing or Incorrect Data

Solution: Ensure your data source contains all the fields referenced in your smart markers. Check field names for typos.

Issue: Formatting Problems

Solution: If formatting is not applied correctly, check your template formatting and smart marker syntax. Try simplifying your conditional formatting rules.

Issue: Large File Processing

Solution: For large data sets, consider:

  • Splitting the template into smaller sections
  • Using the EnableHTTPCompression parameter
  • Processing batches of data incrementally

What You’ve Learned

In this tutorial, you’ve learned:

  • How to create Excel templates with smart markers
  • How to use the Aspose.Cells Cloud API for data assembly
  • Different types of smart markers and their applications
  • How to work with various data sources (Excel, JSON)
  • Advanced techniques for formatting and nested data
  • A real-world financial dashboard implementation

Further Practice

To reinforce your learning, try these exercises:

  1. Create a sales report template with smart markers that includes charts and conditional formatting
  2. Implement an automated report generation system that assembles data from multiple sources
  3. Extend the financial dashboard example with additional metrics and visualizations

Helpful Resources