C# and VB.NET Code to Split Worksheets of an Excel Workbook to TIFF or PNG in Cloud


/ Published in: C#
Save to your folder(s)

This technical tip shows how developers can split worksheets of an Excel 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.


Copy this code and paste it in your HTML
  1. Please take a look over the following code snippet to Split all Worksheets to PNGs
  2.  
  3.  
  4. [C# Code]
  5.  
  6.  
  7. AsposeApp.AppSID = "77***********************************";
  8. AsposeApp.AppKey = "9a*******************************";
  9. string outputPath = "C:\\TempFiles\\";
  10.  
  11. //build URI to split workbook
  12. string strURI = "http://api.aspose.com/v1.1/cells/Sample.xlsx/split?format=png";
  13. //sign URI
  14. string signedURI = Utils.Sign(strURI);
  15. StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
  16. //further process JSON response
  17. string strJSON = reader.ReadToEnd();
  18. //Parse the json string to JObject
  19. JObject parsedJSON = JObject.Parse(strJSON);
  20. SplitWorkbookResponse responseStream = JsonConvert.DeserializeObject<SplitWorkbookResponse>(parsedJSON.ToString());
  21.  
  22.  
  23. foreach (DocumentResponse splitSheet in responseStream.Result.Documents)
  24. {
  25. string splitFileName = System.IO.Path.GetFileName(splitSheet.link.Href);
  26. //build URI to download split worksheets
  27. strURI = "http://api.aspose.com/v1.1/storage/file/" + splitFileName;
  28. //sign URI
  29. signedURI = Utils.Sign(strURI);
  30. //save split worksheets
  31. using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
  32. {
  33. Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
  34. }
  35. }
  36.  
  37. // class definitions
  38. public class SplitWorkbookResponse : Aspose.Cloud.Common.BaseResponse
  39. {
  40. public SplitWorkbook Result { get; set; }
  41. }
  42. public class SplitWorkbook
  43. {
  44. public DocumentResponse[] Documents { get; set; }
  45. }
  46. public class DocumentResponse
  47. {
  48. publicint Id { get; set; }
  49. public LinkResponse link { get; set; }
  50. }
  51. /// <summary>
  52. /// represents link part of the response
  53. /// </summary>
  54. public class LinkResponse
  55. {
  56. public string Href { get; set; }
  57. public string Rel { get; set; }
  58. public string Title { get; set; }
  59. public string Type { get; set; }
  60. }
  61.  
  62.  
  63. Split selected worksheets to TIFFs
  64.  
  65.  
  66. [C# Code]
  67.  
  68.  
  69. AsposeApp.AppSID = "77***********************************";
  70. AsposeApp.AppKey = "9a*******************************";
  71. string outputPath = "C:\\TempFiles\\";
  72.  
  73. //build URI to split workbook
  74. string strURI = "http://api.aspose.com/v1.1/cells/Sample.xlsx/split?from=1&to=2&format=tiff";
  75. //sign URI
  76. string signedURI = Utils.Sign(strURI);
  77. StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
  78. //further process JSON response
  79. string strJSON = reader.ReadToEnd();
  80. //Parse the json string to JObject
  81. JObject parsedJSON = JObject.Parse(strJSON);
  82. SplitWorkbookResponse responseStream = JsonConvert.DeserializeObject<SplitWorkbookResponse>(parsedJSON.ToString());
  83.  
  84.  
  85. foreach (DocumentResponse splitSheet in responseStream.Result.Documents)
  86. {
  87. string splitFileName = System.IO.Path.GetFileName(splitSheet.link.Href);
  88. //build URI to download split worksheets
  89. strURI = "http://api.aspose.com/v1.1/storage/file/" + splitFileName;
  90. //sign URI
  91. signedURI = Utils.Sign(strURI);
  92. //save split worksheets
  93. using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
  94. {
  95. Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
  96. }
  97. }
  98.  
  99. // class definitions
  100. public class SplitWorkbookResponse : Aspose.Cloud.Common.BaseResponse
  101. {
  102. public SplitWorkbook Result { get; set; }
  103. }
  104. public class SplitWorkbook
  105. {
  106. public DocumentResponse[] Documents { get; set; }
  107. }
  108. public class DocumentResponse
  109. {
  110. publicint Id { get; set; }
  111. public LinkResponse link { get; set; }
  112. }
  113. /// <summary>
  114. /// represents link part of the response
  115. /// </summary>
  116. public class LinkResponse
  117. {
  118. public string Href { get; set; }
  119. public string Rel { get; set; }
  120. public string Title { get; set; }
  121. public string Type { get; set; }
  122. }

URL: http://www.aspose.com/docs/display/cellscloud/Split+Excel+Workbooks

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.