Convert Spreadsheets with Task API - Step by Step Tutorial

Introduction

In this tutorial, you’ll learn how to use the Convert task in Aspose.Cells Cloud API to transform Excel files into various formats including PDF, CSV, HTML, and image formats. File conversion is one of the most common requirements when working with spreadsheets, and the Convert task makes this process straightforward through simple API calls.

Prerequisites

Before starting this tutorial, make sure you have:

  • An Aspose Cloud account (sign up for free here)
  • Your Client ID and Client Secret from the Aspose Cloud dashboard
  • Basic understanding of REST API concepts
  • Excel files to use for conversion testing
  • Familiarity with XML/JSON for structuring requests

Understanding the Convert Task

The Convert task transforms an Excel file from one format to another. You need to specify:

  1. Source Workbook - The Excel file you want to convert
  2. Destination Format - What format you want to convert to (represented by the file extension)
  3. Format-Specific Options - Optional settings to customize the conversion

Tutorial: Converting Excel to PDF

Let’s start with one of the most common conversion needs: transforming an Excel workbook into a PDF document.

Step 1: Structure Your Basic Conversion Request

<TaskData>
  <Tasks>
    <TaskDescription>
      <TaskType>Convert</TaskType>
      <ConvertTaskParameter>
        <Workbook>
          <FileSourceType>CloudFileSystem</FileSourceType>
          <FilePath>Sample.xlsx</FilePath>
        </Workbook>
        <DestinationFile>ConvertedOutput.pdf</DestinationFile>
      </ConvertTaskParameter>
    </TaskDescription>
    <TaskDescription>
      <TaskType>SaveResult</TaskType>
      <SaveResultTaskParameter>
        <ResultSource>InMemoryFiles</ResultSource>
        <ResultDestination>
          <DestinationType>OutputStream</DestinationType>
          <InputFile>ConvertedOutput.pdf</InputFile>
          <OutputFile>Result.pdf</OutputFile>
        </ResultDestination>
      </SaveResultTaskParameter>
    </TaskDescription>
  </Tasks>
</TaskData>

Step 2: Customize the PDF Conversion

To control the appearance and behavior of the generated PDF, add PDF-specific options:

<TaskDescription>
  <TaskType>Convert</TaskType>
  <ConvertTaskParameter>
    <Workbook>
      <FileSourceType>CloudFileSystem</FileSourceType>
      <FilePath>Sample.xlsx</FilePath>
    </Workbook>
    <DestinationFile>ConvertedOutput.pdf</DestinationFile>
    <PdfSaveOptions>
      <OnePagePerSheet>true</OnePagePerSheet>
      <AllColumnsInOnePagePerSheet>true</AllColumnsInOnePagePerSheet>
      <PrintingPageType>IgnoreBlank</PrintingPageType>
      <Compliance>PdfA1b</Compliance>
      <DefaultFont>Arial</DefaultFont>
    </PdfSaveOptions>
  </ConvertTaskParameter>
</TaskDescription>

Step 3: Execute the API Request

You can execute this request using cURL or any of the Aspose.Cells Cloud SDKs.

Using cURL

curl -v "https://api.aspose.cloud/v3.0/cells/task/runtask" \
-X POST \
-H "accept: application/xml" \
-H "Content-Type: application/xml" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d "YOUR_XML_REQUEST"

Using C# SDK

var xml = @"<TaskData>
  <Tasks>
    <TaskDescription>
      <TaskType>Convert</TaskType>
      <ConvertTaskParameter>
        <Workbook>
          <FileSourceType>CloudFileSystem</FileSourceType>
          <FilePath>Sample.xlsx</FilePath>
        </Workbook>
        <DestinationFile>ConvertedOutput.pdf</DestinationFile>
        <PdfSaveOptions>
          <OnePagePerSheet>true</OnePagePerSheet>
          <AllColumnsInOnePagePerSheet>true</AllColumnsInOnePagePerSheet>
        </PdfSaveOptions>
      </ConvertTaskParameter>
    </TaskDescription>
    <TaskDescription>
      <TaskType>SaveResult</TaskType>
      <SaveResultTaskParameter>
        <ResultSource>InMemoryFiles</ResultSource>
        <ResultDestination>
          <DestinationType>OutputStream</DestinationType>
          <InputFile>ConvertedOutput.pdf</InputFile>
          <OutputFile>Result.pdf</OutputFile>
        </ResultDestination>
      </SaveResultTaskParameter>
    </TaskDescription>
  </Tasks>
</TaskData>";

ServiceHelper helper = new ServiceHelper(sid, key);
using (HttpWebResponse response = helper.CallPost("http://api.aspose.com/v3.0/cells/task/runtask", xml, "application/xml"))
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream st = response.GetResponseStream();
        FileStream fs = new FileStream("Result.pdf", FileMode.OpenOrCreate);
        st.CopyTo(fs);
    }
}

Try It Yourself

  1. Try converting an Excel file to PDF with different options:
    • Set OnePagePerSheet to false to see how multiple worksheets appear
    • Change the Compliance setting to different PDF standards
    • Experiment with page orientation settings

Tutorial: Converting Excel to Images

Now let’s learn how to convert Excel worksheets to image formats like PNG, JPEG, or TIFF.

Step 1: Structure Your Image Conversion Request

<TaskData>
  <Tasks>
    <TaskDescription>
      <TaskType>Convert</TaskType>
      <ConvertTaskParameter>
        <Workbook>
          <FileSourceType>CloudFileSystem</FileSourceType>
          <FilePath>Source.xlsx</FilePath>
        </Workbook>
        <DestinationFile>Temp.tiff</DestinationFile>
        <ImageSaveOptions>
          <HorizontalResolution>200</HorizontalResolution>
          <OnePagePerSheet>true</OnePagePerSheet>
          <VerticalResolution>100</VerticalResolution>
        </ImageSaveOptions>
      </ConvertTaskParameter>
    </TaskDescription>
    <TaskDescription>
      <TaskType>SaveResult</TaskType>
      <SaveResultTaskParameter>
        <ResultSource>InMemoryFiles</ResultSource>
        <ResultDestination>
          <DestinationType>OutputStream</DestinationType>
          <InputFile>Temp.tiff</InputFile>
          <OutputFile>Output.tiff</OutputFile>
        </ResultDestination>
      </SaveResultTaskParameter>
    </TaskDescription>
  </Tasks>
</TaskData>

Step 2: Understanding Image Conversion Options

The key settings for image conversion include:

  1. HorizontalResolution/VerticalResolution: Controls the DPI of the resulting image
  2. OnePagePerSheet: Determines if each worksheet becomes a separate image
  3. ImageFormat: Specifies the image format when needed
  4. TiffCompression: Controls compression when converting to TIFF

Step 3: Execute the Image Conversion

Execute the request using your preferred method (cURL, SDK, etc.) as shown earlier.

Tutorial: Converting Excel to CSV or Other Data Formats

Let’s look at converting Excel to data formats like CSV or JSON:

<TaskData>
  <Tasks>
    <TaskDescription>
      <TaskType>Convert</TaskType>
      <ConvertTaskParameter>
        <Workbook>
          <FileSourceType>CloudFileSystem</FileSourceType>
          <FilePath>DataSample.xlsx</FilePath>
        </Workbook>
        <DestinationFile>Export.csv</DestinationFile>
        <CsvSaveOptions>
          <Separator>,</Separator>
          <QuoteType>Minimal</QuoteType>
          <EncodeType>UTF8</EncodeType>
        </CsvSaveOptions>
      </ConvertTaskParameter>
    </TaskDescription>
    <TaskDescription>
      <TaskType>SaveResult</TaskType>
      <SaveResultTaskParameter>
        <ResultSource>InMemoryFiles</ResultSource>
        <ResultDestination>
          <DestinationType>OutputStream</DestinationType>
          <InputFile>Export.csv</InputFile>
          <OutputFile>FinalData.csv</OutputFile>
        </ResultDestination>
      </SaveResultTaskParameter>
    </TaskDescription>
  </Tasks>
</TaskData>

CSV Conversion Options

Important CSV options include:

  • Separator: Character to use between values (comma, tab, etc.)
  • QuoteType: How to handle text quoting
  • EncodeType: Character encoding (UTF8, UTF16, etc.)

Advanced: Combining Conversion with Other Tasks

Let’s create a more complex workflow that:

  1. First imports data into a workbook
  2. Then converts the workbook to PDF
<TaskData>
  <Tasks>
    <!-- Task 1: Import data -->
    <TaskDescription>
      <TaskType>ImportData</TaskType>
      <ImportDataTaskParameter>
        <Workbook>
          <FileSourceType>CloudFileSystem</FileSourceType>
          <FilePath>Template.xlsx</FilePath>
        </Workbook>
        <ImportBatchDataOption>
          <DestinationWorksheet>Sheet1</DestinationWorksheet>
          <IsInsert>true</IsInsert>
          <BatchData>
            <!-- Your data cells here -->
            <CellValue>
              <rowIndex>0</rowIndex>
              <columnIndex>0</columnIndex>
              <type>String</type>
              <value>Product</value>
            </CellValue>
            <!-- More data cells -->
          </BatchData>
        </ImportBatchDataOption>
      </ImportDataTaskParameter>
    </TaskDescription>
    
    <!-- Task 2: Convert to PDF -->
    <TaskDescription>
      <TaskType>Convert</TaskType>
      <ConvertTaskParameter>
        <Workbook>
          <FileSourceType>InMemoryFiles</FileSourceType>
          <FilePath>Template.xlsx</FilePath>
        </Workbook>
        <DestinationFile>Report.pdf</DestinationFile>
        <PdfSaveOptions>
          <OnePagePerSheet>true</OnePagePerSheet>
        </PdfSaveOptions>
      </ConvertTaskParameter>
    </TaskDescription>
    
    <!-- Task 3: Save the result -->
    <TaskDescription>
      <TaskType>SaveResult</TaskType>
      <SaveResultTaskParameter>
        <ResultSource>InMemoryFiles</ResultSource>
        <ResultDestination>
          <DestinationType>OutputStream</DestinationType>
          <InputFile>Report.pdf</InputFile>
          <OutputFile>FinalReport.pdf</OutputFile>
        </ResultDestination>
      </SaveResultTaskParameter>
    </TaskDescription>
  </Tasks>
</TaskData>

Common Issues and Troubleshooting

Issue: Missing or Improperly Rendered Content

If your converted file is missing content or doesn’t look right:

  • Check that the source file exists and contains expected data
  • Verify format-specific options are appropriate for your data
  • For PDF/images, try adjusting page settings and scaling options

Issue: Character Encoding Problems in CSV/Text Formats

If special characters appear incorrectly in CSV output:

  • Explicitly set the EncodeType to match your needs
  • Try different quote settings if your data contains delimiters

Supported Conversion Formats

Aspose.Cells Cloud API supports converting Excel files to:

  • Document formats: PDF, XPS, HTML, MHTML
  • Image formats: PNG, JPEG, BMP, TIFF, GIF, EMF, SVG
  • Data formats: CSV, TSV, JSON, XML
  • Other Excel formats: XLSX, XLS, XLSB, XLSM, ODS

What You’ve Learned

In this tutorial, you’ve learned:

  • How to convert Excel files to PDF with custom options
  • Techniques for generating image representations of worksheets
  • Methods for exporting Excel data to formats like CSV
  • How to combine conversion with other tasks for complex workflows

Further Practice

  1. Convert a multi-sheet workbook to separate PDF files
  2. Export multiple worksheets to separate CSV files
  3. Create a workflow that imports data, formats it, and exports to multiple formats
  4. Experiment with different image formats and resolution settings

Next Steps

Now that you’ve mastered file conversion, you might want to explore other aspects of the Aspose.Cells Cloud API:

Helpful Resources