Tutorial: How to Create an Excel Workbook with Aspose.Cells Cloud API
Prerequisites
Before starting this tutorial, make sure you have:
- An Aspose Cloud account with an active subscription or free trial
- Your Client ID and Client Secret from the Aspose Cloud Dashboard
- Basic understanding of REST API concepts
- Familiarity with your preferred programming language (examples provided in multiple languages)
Understanding Workbook Creation
Creating Excel workbooks programmatically is often the first step in many spreadsheet automation workflows. Aspose.Cells Cloud API provides multiple ways to create workbooks:
- Creating empty workbooks
- Creating workbooks from templates
- Creating workbooks with Smart Markers
Let’s explore each approach step by step.
1. Creating an Empty Excel Workbook
The simplest way to generate a new Excel file is to create an empty workbook. This gives you a blank canvas to populate with your data.
Try It Yourself: Creating an Empty Workbook
Follow these steps to create your first empty workbook:
- Prepare your authentication credentials
- Make the API request to create an empty workbook
- Verify the created workbook
Using cURL
curl -X PUT "https://api.aspose.cloud/v3.0/cells/newworkbook.xlsx?isWriteOver=false" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
SDK Examples
Let’s see how to create an empty workbook using different programming languages:
Python
# Tutorial Code Example - Creating an Empty Excel Workbook
import asposecellscloud
from asposecellscloud.apis.cells_api import CellsApi
from asposecellscloud.models import *
from asposecellscloud.requests import *
# Configure authentication
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
api = CellsApi(client_id, client_secret)
# Specify the name for the new workbook
name = "new_workbook.xlsx"
# Create a request to generate an empty workbook
request = PutWorkbookCreateRequest(name=name, is_write_over=True)
# Execute the request
response = api.put_workbook_create(request)
# Check if the workbook was created successfully
if response.status == "OK":
print(f"Success! Empty workbook '{name}' created.")
print(f"Workbook properties: {response.workbook}")
else:
print(f"Error creating workbook: {response.status}")
C#
// Tutorial Code Example - Creating an Empty Excel Workbook
using System;
using Aspose.Cells.Cloud.SDK.Api;
using Aspose.Cells.Cloud.SDK.Model;
using Aspose.Cells.Cloud.SDK.Request;
namespace AsposeCellsCloudTutorial
{
class Program
{
static void Main(string[] args)
{
// Configure authentication
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
var api = new CellsApi(clientId, clientSecret);
// Specify the name for the new workbook
var name = "new_workbook.xlsx";
// Create a request to generate an empty workbook
var request = new PutWorkbookCreateRequest
{
Name = name,
IsWriteOver = true
};
try
{
// Execute the request
var response = api.PutWorkbookCreate(request);
// Check if the workbook was created successfully
Console.WriteLine($"Success! Empty workbook '{name}' created.");
Console.WriteLine($"Workbook properties: {response.Workbook}");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating workbook: {ex.Message}");
}
}
}
}
Java
// Tutorial Code Example - Creating an Empty Excel Workbook
import com.aspose.cells.cloud.api.CellsApi;
import com.aspose.cells.cloud.model.*;
import com.aspose.cells.cloud.request.*;
public class CreateEmptyWorkbook {
public static void main(String[] args) {
// Configure authentication
String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";
CellsApi api = new CellsApi(clientId, clientSecret);
try {
// Specify the name for the new workbook
String name = "new_workbook.xlsx";
// Create a request to generate an empty workbook
PutWorkbookCreateRequest request = new PutWorkbookCreateRequest();
request.setName(name);
request.setIsWriteOver(true);
// Execute the request
WorkbookResponse response = api.putWorkbookCreate(request);
// Check if the workbook was created successfully
System.out.println("Success! Empty workbook '" + name + "' created.");
System.out.println("Workbook properties: " + response.getWorkbook());
} catch (Exception e) {
System.out.println("Error creating workbook: " + e.getMessage());
}
}
}
2. Creating a Workbook from a Template
For more structured workbooks, you can create files based on existing templates. This is useful when you need consistent formatting or predefined formulas.
Try It Yourself: Creating a Workbook from a Template
- First, upload your template file to Aspose.Cells Cloud storage
- Then create a new workbook based on that template
- Verify the created workbook contains all elements from the template
Using cURL
curl -X PUT "https://api.aspose.cloud/v3.0/cells/newworkbook.xlsx?templateFile=template.xlsx&isWriteOver=true" \
-H "accept: application/json" \
-H "authorization: Bearer YOUR_ACCESS_TOKEN"
Python Example
# Tutorial Code Example - Creating a Workbook from Template
import asposecellscloud
from asposecellscloud.apis.cells_api import CellsApi
from asposecellscloud.models import *
from asposecellscloud.requests import *
# Configure authentication
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
api = CellsApi(client_id, client_secret)
# Specify file names
template_file = "template.xlsx"
new_workbook = "from_template.xlsx"
# Create a request to generate a workbook from template
request = PutWorkbookCreateRequest(
name=new_workbook,
template_file=template_file,
is_write_over=True
)
# Execute the request
response = api.put_workbook_create(request)
# Check if the workbook was created successfully
if response.status == "OK":
print(f"Success! Workbook '{new_workbook}' created from template '{template_file}'.")
else:
print(f"Error creating workbook: {response.status}")
3. Creating a Workbook with Smart Markers
Smart Markers are a powerful feature that allows you to create data-populated workbooks. They act as placeholders in your template that get replaced with actual data.
Try It Yourself: Using Smart Markers
- Create a template file with Smart Markers
- Prepare your data file (typically XML)
- Use the API to process the template with data
Smart Marker Example
# Tutorial Code Example - Creating a Workbook with Smart Markers
import asposecellscloud
from asposecellscloud.apis.cells_api import CellsApi
from asposecellscloud.models import *
from asposecellscloud.requests import *
# Configure authentication
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
api = CellsApi(client_id, client_secret)
# Specify file names
workbook_name = "smartmarker_template.xlsx"
xml_file = "Sample_SmartMarker_Data.xml"
# Create request to process Smart Markers
request = PostWorkbookGetSmartMarkerResultRequest(
name=workbook_name,
xml_file=xml_file
)
# Execute the request
response = api.post_workbook_get_smart_marker_result(request)
# The processed file is returned in the response
if response is not None:
print("Success! Smart Marker template processed with data.")
# You can save the response.body to a file
else:
print("Error processing Smart Marker template.")
Troubleshooting Tips
When creating workbooks, you might encounter these common issues:
Authentication errors:
- Ensure your Client ID and Secret are correct
- Check that your access token is valid and not expired
File already exists:
- Set
isWriteOver=false
to prevent overwriting existing files - Or set
isWriteOver=true
to replace existing files
- Set
Template file not found:
- Verify the template file exists in your storage
- Check that the path to the template file is correct
Smart Marker processing errors:
- Ensure your XML data format matches the Smart Markers in the template
- Verify that Smart Markers in the template are correctly formatted
What You’ve Learned
In this tutorial, you’ve learned how to:
- Create empty Excel workbooks through the Aspose.Cells Cloud API
- Generate workbooks based on template files
- Use Smart Markers to create data-populated workbooks
- Handle common workbook creation scenarios and troubleshoot issues
Further Practice
To reinforce your learning, try these exercises:
- Create a workbook with multiple worksheets
- Generate a workbook from a template and add custom formatting
- Create a complex Smart Marker template that generates a report from your data