Tutorial: Batch Converting Multiple Excel Files 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
- Multiple Excel files uploaded to your cloud storage for batch processing
- Basic understanding of regular expressions (for file matching patterns)
Introduction to Batch Conversion
Batch conversion is an efficient way to process multiple Excel files at once, saving time and API calls compared to processing files individually. The Aspose.Cells Cloud API provides powerful batch processing capabilities that allow you to:
- Convert multiple Excel files to a target format in a single API call
- Use regular expressions to select specific files for processing
- Apply consistent conversion settings across all files
- Process files directly from your cloud storage
In this tutorial, we’ll explore how to efficiently convert multiple Excel files to PDF format in a single operation.
Understanding Batch Conversion Request Structure
The batch conversion endpoint accepts a JSON request with the following key components:
- SourceFolder: The folder in cloud storage containing your Excel files
- MatchCondition: Rules to determine which files to process (using regex patterns)
- Format: The target format for conversion (PDF, HTML, CSV, etc.)
- OutFolder: The destination folder for the converted files
- SaveOptions: Settings to control the conversion process
Let’s examine each component in detail.
File Selection with MatchCondition
The MatchCondition
property allows you to define which files should be included in the batch process using:
- RegexPattern: A regular expression pattern to match filenames
- FullMatchConditions: An array of exact filename matches
For example, to process all Excel files that start with “Book” and end with “.xlsx”:
"MatchCondition": {
"RegexPattern": "(^Book)(.+)(xlsx$)"
}
Conversion Format
The Format
property specifies the target format. Aspose.Cells Cloud supports numerous formats including:
- PDF, HTML, CSV, TIFF, XPS
- Image formats like PNG, JPEG, BMP, SVG
- Other spreadsheet formats like XLS, ODS
SaveOptions Configuration
The SaveOptions
property allows you to customize the conversion process with format-specific options:
"SaveOptions": {
"SaveFormat": "pdf",
"CalculateFormula": true,
"EnableHTTPCompression": true,
"OnePagePerSheet": true,
"Compliance": "None"
}
Common options include:
- CalculateFormula: Update formula results before saving
- EnableHTTPCompression: Compress data during transmission
- OnePagePerSheet: For PDF, places each worksheet on a separate page
Implementing Batch Conversion
Now let’s implement batch conversion using different programming languages.
Try It Yourself: Using cURL
curl -v "https://api.aspose.cloud/v3.0/cells/batch/convert" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer <your_jwt_token>" \
-d "{
\"SourceFolder\": \"MyExcelFiles\",
\"OutFolder\": \"ConvertedFiles\",
\"MatchCondition\": {
\"RegexPattern\": \"(^Book)(.+)(xlsx$)\"
},
\"Format\": \"pdf\",
\"SaveOptions\": {
\"SaveFormat\": \"pdf\",
\"CalculateFormula\": true,
\"EnableHTTPCompression\": true,
\"OnePagePerSheet\": true
}
}"
Replace <your_jwt_token>
with your actual authentication token. This request will convert all Excel files in the “MyExcelFiles” folder whose names start with “Book” and end with “xlsx” to PDF format, placing the results in the “ConvertedFiles” folder.
Implementation in C#
// Initialize the API with your client credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload some files for testing
var uploadFolder = "MyExcelFiles";
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> { { "Book1.xlsx", File.OpenRead("Book1.xlsx") } },
path: $"{uploadFolder}/Book1.xlsx"
));
cellsApi.UploadFile(new UploadFileRequest(
uploadFiles: new Dictionary<string, Stream> { { "Book2.xlsx", File.OpenRead("Book2.xlsx") } },
path: $"{uploadFolder}/Book2.xlsx"
));
// Create a match condition to select files
var matchCondition = new MatchConditionRequest
{
RegexPattern = "(^Book)(.+)(xlsx$)"
};
// Create save options
var saveOptions = new PdfSaveOptions
{
SaveFormat = "pdf",
CalculateFormula = true,
EnableHTTPCompression = true,
OnePagePerSheet = true,
CreateDirectory = true
};
// Prepare the batch convert request
var batchRequest = new BatchConvertRequest
{
SourceFolder = uploadFolder,
OutFolder = "ConvertedFiles",
MatchCondition = matchCondition,
Format = "pdf",
SaveOptions = saveOptions
};
// Execute the batch conversion
var result = cellsApi.PostBatchConvert(new PostBatchConvertRequest(
batchConvertRequest: batchRequest
));
Console.WriteLine($"Batch conversion completed with status: {result.Status}");
Implementation in Java
// Initialize API client with your credentials
CellsApi cellsApi = new CellsApi(clientId, clientSecret);
// Upload some files for testing (if needed)
String uploadFolder = "MyExcelFiles";
cellsApi.uploadFile(new UploadFileRequest(
new File("Book1.xlsx"),
uploadFolder + "/Book1.xlsx",
null
));
cellsApi.uploadFile(new UploadFileRequest(
new File("Book2.xlsx"),
uploadFolder + "/Book2.xlsx",
null
));
// Create a match condition to select files
MatchConditionRequest matchCondition = new MatchConditionRequest();
matchCondition.setRegexPattern("(^Book)(.+)(xlsx$)");
// Create save options
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setSaveFormat("pdf");
saveOptions.setCalculateFormula(true);
saveOptions.setEnableHTTPCompression(true);
saveOptions.setOnePagePerSheet(true);
saveOptions.setCreateDirectory(true);
// Prepare the batch convert request
BatchConvertRequest batchRequest = new BatchConvertRequest();
batchRequest.setSourceFolder(uploadFolder);
batchRequest.setOutFolder("ConvertedFiles");
batchRequest.setMatchCondition(matchCondition);
batchRequest.setFormat("pdf");
batchRequest.setSaveOptions(saveOptions);
// Execute the batch conversion
CellsCloudResponse response = cellsApi.postBatchConvert(
new PostBatchConvertRequest(batchRequest)
);
System.out.println("Batch conversion completed with status: " + response.getStatus());
Advanced Batch Conversion Techniques
Converting to Multiple Formats in Sequence
You can convert the same set of files to different formats by making multiple batch requests:
// First batch conversion - to PDF
batchRequest.Format = "pdf";
var pdfResult = cellsApi.PostBatchConvert(new PostBatchConvertRequest(
batchConvertRequest: batchRequest
));
// Second batch conversion - to HTML
batchRequest.Format = "html";
batchRequest.OutFolder = "ConvertedHtmlFiles";
var htmlResult = cellsApi.PostBatchConvert(new PostBatchConvertRequest(
batchConvertRequest: batchRequest
));
Using Exact Filename Matching
Instead of regex patterns, you can specify exact filenames:
var matchCondition = new MatchConditionRequest
{
FullMatchConditions = new List<string> { "Book1.xlsx", "QuarterlyReport.xlsx" }
};
Format-Specific Options
Different target formats support different options. Here are examples for common formats:
PDF Specific Options
var pdfOptions = new PdfSaveOptions
{
SaveFormat = "pdf",
OnePagePerSheet = true,
Compliance = "PDF/A-1b",
DefaultFont = "Arial",
PrintingPageType = "IgnoreBlank"
};
HTML Specific Options
var htmlOptions = new HtmlSaveOptions
{
SaveFormat = "html",
ExportImagesAsBase64 = true,
ExportGridLines = false,
ExportActiveWorksheetOnly = true
};
Monitoring Batch Progress
The batch conversion process is asynchronous, and the API returns immediately with a status code. To check the status or results:
Check the Initial Response: A status code of 200 indicates the batch process was successfully initiated.
Verify Output Files: After the process completes, check your output folder in cloud storage for the converted files.
Download Results: Use the CellsApi.DownloadFile method to retrieve the converted files:
var fileStream = cellsApi.DownloadFile(new DownloadFileRequest(
path: "ConvertedFiles/Book1.pdf"
));
using (var outputStream = File.Create("LocalBook1.pdf"))
{
fileStream.CopyTo(outputStream);
}
Troubleshooting Common Issues
Issue: Files Not Being Selected
Solution: Double-check your regex pattern or exact file matches. Test your regex pattern using an online regex tester to ensure it matches the intended files.
Issue: Conversion Taking Too Long
Solution: For large batches, consider:
- Enabling HTTP compression
- Breaking the process into smaller batches
- Using simpler save options (disable features you don’t need)
Issue: Missing Output Files
Solution: Verify the CreateDirectory
option is set to true if your output folder doesn’t already exist.
What You’ve Learned
In this tutorial, you’ve learned:
- How to batch convert multiple Excel files in a single API request
- How to select specific files using regex patterns and exact matches
- How to apply custom conversion settings for different output formats
- How to implement batch conversion in C# and Java
- How to monitor and verify the batch conversion process
Further Practice
To reinforce your learning, try these exercises:
- Create a batch conversion process that converts Excel files to both PDF and HTML formats
- Implement a solution that monitors a folder for new Excel files and automatically batch converts them
- Create a batch process that selects files based on their creation date using metadata
Next Steps
Continue your learning journey with these related tutorials: