Posted By

nausherwan on 06/02/14


Tagged

data sort excel sorting workbook Aspose asposecells worsheet


Versions (?)

C# Sample Code to Sort Data in Worksheet using Aspose.Cells for Cloud API


 / Published in: C#
 

URL: http://www.aspose.com/docs/display/cellscloud/Sort+Worksheet+Data

This example shows how to sort data in a worksheet 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.

  1. //sepcify App SID2
  2. string appsSID = "77******-1***-4***-a***-80**********";
  3. //sepcify App Key
  4. string appKey = "********************************";
  5.  
  6.  
  7. //build URI to sort data
  8. string strURI = "http://api.aspose.com/v1.1/cells/input.xlsx/worksheets/Sheet1/sort?cellArea=A1:C13";
  9.  
  10. Console.WriteLine("Sorting worksheet data...");
  11. //sign the remote URI
  12. string signedURI = Sign(strURI, appSID, appKey);
  13.  
  14. //Create Datasort Object
  15. DataSort dataSort = new DataSort();
  16. //Set Sorting Conditions
  17. dataSort.HasHeaders = false;
  18. dataSort.CaseSensitive = false;
  19. dataSort.SortLeftToRight = true;
  20. List<SortKey> sortKey = new List<SortKey>();
  21. SortKey sort = new SortKey();
  22. sort.Key = 4;
  23. sort.SortOrder = "descending";
  24. sortKey.Add(sort);
  25. dataSort.KeyList = sortKey;
  26. string strJSON = JsonConvert.SerializeObject(dataSort);
  27.  
  28. Stream responseStream = ProcessCommand(signedURI, "POST", strJSON);
  29. StreamReader reader = new StreamReader(responseStream);
  30. string strResponse = reader.ReadToEnd();
  31. //Parse the json string to JObject
  32. JObject pJSON = JObject.Parse(strResponse);
  33. BaseResponse baseResponse = JsonConvert.DeserializeObject<BaseResponse>(pJSON.ToString());
  34.  
  35. if (baseResponse.Status == "OK")
  36. Console.WriteLine("Data has been sorted successfully");
  37.  
  38.  
  39.  
  40. /// <summary>
  41. /// Sign method
  42. /// </summary>
  43. public static string Sign(string url, string AppSID, string AppKey)
  44. {
  45. try
  46. {
  47. // Add appSID parameter.
  48. UriBuilder builder = new UriBuilder(url);
  49. if (builder.Query != null && builder.Query.Length > 1)
  50. builder.Query = builder.Query.Substring(1) + "&appSID=" + AppSID;
  51. else
  52. builder.Query = "appSID=" + AppSID;
  53. // Remove final slash here as it can be added automatically.
  54. builder.Path = builder.Path.TrimEnd('/');
  55. // Compute the hash.
  56.  
  57. byte[] privateKey = System.Text.Encoding.UTF8.GetBytes(AppKey);
  58.  
  59. System.Security.Cryptography.HMACSHA1 algorithm = new System.Security.Cryptography.HMACSHA1(privateKey);
  60. //System.Text.ASCIIEncoding
  61. byte[] sequence = System.Text.ASCIIEncoding.ASCII.GetBytes(builder.Uri.AbsoluteUri);
  62. byte[] hash = algorithm.ComputeHash(sequence);
  63. string signature = Convert.ToBase64String(hash);
  64. // Remove invalid symbols.
  65. signature = signature.TrimEnd('=');
  66. signature = System.Web.HttpUtility.UrlEncode(signature);
  67. // Convert codes to upper case as they can be updated automatically.
  68. signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", e => e.Value.ToUpper());
  69. //signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", delegate(string e){ e.Value.ToUpper()});
  70. // Add the signature to query string.
  71. return string.Format("{0}&signature={1}", builder.Uri.AbsoluteUri, signature);
  72. }
  73. catch (Exception ex)
  74. {
  75. throw new Exception(ex.Message);
  76. }
  77. }
  78.  
  79.  
  80. /// <summary>
  81. /// Process Command method
  82. /// </summary>
  83. public static Stream ProcessCommand(string strURI, string strHttpCommand, string strContent)
  84. {
  85. try
  86. {
  87. byte[] arr = System.Text.Encoding.UTF8.GetBytes(strContent);
  88.  
  89. Uri address = new Uri(strURI);
  90. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(address);
  91. request.Method = strHttpCommand;
  92. request.ContentType = "application/json";
  93.  
  94. request.ContentLength = arr.Length;
  95.  
  96. Stream dataStream = request.GetRequestStream();
  97. dataStream.Write(arr, 0, arr.Length);
  98. dataStream.Close();
  99.  
  100. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  101. return response.GetResponseStream();
  102. }
  103. catch (System.Net.WebException webex)
  104. {
  105. string message = webex.Message;
  106. if (webex.Response != null)
  107. {
  108. if (webex.Response.ContentLength != 0)
  109. {
  110. using (Stream stream = webex.Response.GetResponseStream())
  111. {
  112. using (StreamReader reader = new StreamReader(stream))
  113. {
  114. message = reader.ReadToEnd();
  115. }
  116. }
  117. }
  118. }
  119. throw new Exception(message);
  120. }
  121. catch (Exception Ex)
  122. {
  123. throw new Exception(Ex.Message);
  124. }
  125. }
  126.  
  127.  
  128. /// <summary>
  129. /// this class is the base class for every response class
  130. /// </summary>
  131. public class BaseResponse
  132. {
  133. public BaseResponse() { }
  134.  
  135. public string Code { get; set; }
  136. public string Status { get; set; }
  137. }
  138.  
  139. /// <summary>
  140. /// this class is data sort class
  141. /// </summary>
  142. public class DataSort
  143. {
  144. public DataSort()
  145. {
  146.  
  147. }
  148.  
  149. public bool CaseSensitive { get; set; }
  150. public bool HasHeaders { get; set; }
  151. public bool SortLeftToRight { get; set; }
  152. public List <SortKey> KeyList { get; set; }
  153.  
  154. }
  155. /// <summary>
  156. /// this class is sort key
  157. /// </summary>
  158. public class SortKey
  159. {
  160. public SortKey()
  161. {
  162.  
  163. }
  164.  
  165. public int Key { get; set; }
  166. public string SortOrder { get; set; }
  167.  
  168. }

Report this snippet  

You need to login to post a comment.