Split All or Specific Excel Worksheets to New Workbook, TIFF or Image Formats in Ruby

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Cells Cloud logoSpreadsheets are widely used to store, manipulate, organize and present a small or large set of data within a system of cells, organized into rows and columns.

Aspose.Cells Cloud allows you to merge, split, create, read, update or convert Microsoft Excel workbooks in any language including .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. You can use it with any language or platform that supports REST. (Almost all platforms and languages support REST and provide native REST clients to work with REST APIs).

This post covers how to split a workbook into single worksheets and save all or specific worksheets as new workbooks, TIFFs or images in Ruby. You can check the topic split Microsoft Excel workbooks for information on other languages. To split workbooks, you need to upload the input Excel files to Aspose Cloud or any supported third party storage and then send a POST request to the Aspose Cloud service. The following steps describe the process in detail.

Split a Workbook using Ruby REST

This REST example uses the RestClient library to send HTTP requests and handle HTTP responses so you need to install RestClient to use these examples.

You can use the following URI to split a workbook on Aspose Cloud or any supported third party storage.

http://api.aspose.com/v1.1/cells/Sample.xlsx/split?format=png

Use following optional parameters with the above URI. All or specific parameters can be used according to your requirement. If no parameter is specified, then all worksheets are split to the specified format.

  • storage – Used to set the storage name if you are using third party storage.
  • folder – Used to set the name and path of the folder where the input file has been uploaded.
  • from – Used to set the start page number if you are splitting specific pages.
  • to – Used to set the end page number if you are splitting specific pages.

After building the URI:

  1. Set the App SID and App Key and sign the URI.
    See section 1 of the code below and the Sign URI method for more details.
  2. Send a POST request to the Aspose Cloud service.
    See section 2 of the code below for more details.

Following is the code to split a workbook.


####### Section 1 ######

     

app_sid = '####### Section 1 ######

     

app_sid = '77******-1***-4***-a***-80**********'

app_key = '*********************'

Aspose::Cloud::Common::AsposeApp.new(app_sid, app_key)

 

#build URI to split workbook

str_uri = 'http://api.aspose.com/v1.1/cells/Sample.xlsx/split?format=png';

 

#uncomment following line to split specific worksheets

#str_uri = 'http://api.aspose.com/v1.1/cells/Sample.xlsx/split?from=2&to=3&format=tiff';

                 

#sign URI

signed_uri = Aspose::Cloud::Common::Utils.sign(str_uri);

 

####### End Section 1 ######

 

####### Section 2 ######

#Split spreadsheet file

response_stream = RestClient.post(signed_uri, '', {:accept=>:json})

 

####### End Section 2 ######

Download Split Files

Once files are split, you can download the split worksheets as new files. To download the split files, you can convert a response stream to JSON and loop through each Href element to get the names of the split files and download them.

Following is the code to download split files.


stream_hash = JSON.parse(response_stream)

 

stream_hash['Result']['Documents'].each do |document|

 

      #Build and sign URI to download split files

      file_name = File.basename(document['link']['Href'])

      str_uri = 'http://api.aspose.com/v1.1/storage/file/' + file_name;            

      signed_uri = Aspose::Cloud::Common::Utils.sign(str_uri);

     

      file_name = File.basename(str_uri)

      #Download and save split files

      response_stream = RestClient.get(signed_uri, :accept => 'application/json')

      Aspose::Cloud::Common::Utils.save_file(response_stream, file_name)

 

end