DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 73 posts at DZone. View Full User Profile

Split Worksheets of an Excel Workbook to TIFF or PNG Using C# in Cloud

02.12.2014
| 1530 views |
  • submit to reddit
AsposeApp.AppSID  = "77***********************************";
AsposeApp.AppKey = "9a*******************************";
string outputPath = "C:\\TempFiles\\";

//build URI to split workbook
string strURI = "http://api.aspose.com/v1.1/cells/Sample.xlsx/split?format=png";
//sign URI
string signedURI = Utils.Sign(strURI);
StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
//further process JSON response
string strJSON = reader.ReadToEnd();
//Parse the json string to JObject
JObject parsedJSON = JObject.Parse(strJSON);
SplitWorkbookResponse responseStream = JsonConvert.DeserializeObject<SplitWorkbookResponse>(parsedJSON.ToString());


foreach (DocumentResponse splitSheet in responseStream.Result.Documents)
{
    string splitFileName = System.IO.Path.GetFileName(splitSheet.link.Href);
    //build URI to download split worksheets
    strURI = "http://api.aspose.com/v1.1/storage/file/" + splitFileName;
    //sign URI
    signedURI = Utils.Sign(strURI);
    //save split worksheets
    using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
    {
        Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
    }
}

    // class definitions
    public class SplitWorkbookResponse : Aspose.Cloud.Common.BaseResponse
    {
        public SplitWorkbook Result { get; set; }
    }
    public class SplitWorkbook
    {
        public DocumentResponse[] Documents { get; set; }
    }
    public class DocumentResponse
    {
        public int Id { get; set; }
        public LinkResponse link { get; set; }
    }
    /// <summary>
    /// represents link part of the response
    /// </summary>
    public class LinkResponse
    {
        public string Href { get; set; }
        public string Rel { get; set; }
        public string Title { get; set; }
        public string Type { get; set; }
    }

Split selected worksheets to TIFFs 
 
AsposeApp.AppSID  = "77***********************************";
AsposeApp.AppKey = "9a*******************************";
string outputPath = "C:\\TempFiles\\";

//build URI to split workbook
string strURI = "http://api.aspose.com/v1.1/cells/Sample.xlsx/split?from=1&to=2&format=tiff";
//sign URI
string signedURI = Utils.Sign(strURI);
StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
//further process JSON response
string strJSON = reader.ReadToEnd();
//Parse the json string to JObject
JObject parsedJSON = JObject.Parse(strJSON);
SplitWorkbookResponse responseStream = JsonConvert.DeserializeObject<SplitWorkbookResponse>(parsedJSON.ToString());


foreach (DocumentResponse splitSheet in responseStream.Result.Documents)
{
    string splitFileName = System.IO.Path.GetFileName(splitSheet.link.Href);
    //build URI to download split worksheets
    strURI = "http://api.aspose.com/v1.1/storage/file/" + splitFileName;
    //sign URI
    signedURI = Utils.Sign(strURI);
    //save split worksheets
    using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
    {
        Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
    }
}

    // class definitions
    public class SplitWorkbookResponse : Aspose.Cloud.Common.BaseResponse
    {
        public SplitWorkbook Result { get; set; }
    }
    public class SplitWorkbook
    {
        public DocumentResponse[] Documents { get; set; }
    }
    public class DocumentResponse
    {
        public int Id { get; set; }
        public LinkResponse link { get; set; }
    }
    /// <summary>
    /// represents link part of the response
    /// </summary>
    public class LinkResponse
    {
        public string Href { get; set; }
        public string Rel { get; set; }
        public string Title { get; set; }
        public string Type { get; set; }
    } 

This technical tip shows how developers can split worksheets of a workbook to a separate workbook, TIFF, PNG or any supported image format in the cloud.  This example allows you to split all or specific worksheets of a workbook file and save each worksheet as a new workbook, TIFF or any supported image format using Aspose.Cells for Cloud API in your applications. You can use our REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. Some of the code samples are provided for the above languages.