Guest User

Untitled

a guest
Nov 20th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #r "DocumentFormat.OpenXml.dll"
  2. #r "WindowsBase.dll"
  3.  
  4. using System.Net;
  5. using System.IO;
  6. using DocumentFormat.OpenXml.Packaging;
  7. using DocumentFormat.OpenXml.Spreadsheet;
  8.  
  9. public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
  10. {
  11. log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
  12.  
  13. WebClient client = new WebClient();
  14.  
  15. byte[] buffer = client.DownloadData("http://amor-webapp-test.azurewebsites.net/Content/hello.xlsx");
  16. MemoryStream stream = new MemoryStream();
  17. stream.Write(buffer, 0, buffer.Length);
  18. stream.Position = 0;
  19. using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, false))
  20. {
  21. WorkbookPart workbookPart = doc.WorkbookPart;
  22. SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
  23. SharedStringTable sst = sstpart.SharedStringTable;
  24.  
  25. WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
  26. Worksheet sheet = worksheetPart.Worksheet;
  27.  
  28. var cells = sheet.Descendants<Cell>();
  29. var rows = sheet.Descendants<Row>();
  30.  
  31. log.Info(string.Format("Row count = {0}", rows.LongCount()));
  32. log.Info(string.Format("Cell count = {0}", cells.LongCount()));
  33.  
  34. // One way: go through each cell in the sheet
  35. foreach (Cell cell in cells)
  36. {
  37. if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
  38. {
  39. int ssid = int.Parse(cell.CellValue.Text);
  40. string str = sst.ChildElements[ssid].InnerText;
  41. log.Info(string.Format("Shared string {0}: {1}", ssid, str));
  42. }
  43. else if (cell.CellValue != null)
  44. {
  45. log.Info(string.Format("Cell contents: {0}", cell.CellValue.Text));
  46. }
  47. }
  48. }
  49.  
  50. return req.CreateResponse(HttpStatusCode.OK, "Hello ");
  51. }
  52.  
  53. #r "Excel.dll"
  54. #r "System.Data"
  55.  
  56. using System.Net;
  57. using System.IO;
  58. using Excel;
  59. using System.Data;
  60.  
  61. public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
  62. {
  63. log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
  64.  
  65. WebClient client = new WebClient();
  66.  
  67. byte[] buffer = client.DownloadData("http://amor-webapp-test.azurewebsites.net/Content/abcdefg.xls");
  68. MemoryStream stream = new MemoryStream();
  69. stream.Write(buffer, 0, buffer.Length);
  70. stream.Position = 0;
  71.  
  72. IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
  73.  
  74. DataSet result = excelReader.AsDataSet();
  75.  
  76. for (int i = 0; i < result.Tables.Count; i++)
  77. {
  78. log.Info(result.Tables[i].TableName +" has " + result.Tables[i].Rows.Count + " rows.");
  79. }
  80.  
  81. return req.CreateResponse(HttpStatusCode.OK, "Hello ");
  82. }
  83.  
  84. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(originalExcelUrl);
  85. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  86. using (Stream stream = response.GetResponseStream())
  87. {
  88. var doc = SpreadsheetDocument.Open(stream, true);
  89. // etc
  90. }
Add Comment
Please, Sign In to add comment