Esempio n. 1
0
        public HttpResponseMessage Export(string sessionId, string filename)
        {
            if (!userRepository.CanManageExport(sessionId))
            {
                return(null);
            }

            XSSFWorkbook  wb      = new XSSFWorkbook();
            List <Device> devices = deviceRepository.GetDevices();
            ISheet        sheet;

            sheet = wb.CreateSheet("Average temp of devices");

            var deviceName      = sheet.CreateRow(0);
            var avgTemperatures = sheet.CreateRow(1);

            for (int j = 0; j < devices.Count; j++)
            {
                deviceName.CreateCell(j).SetCellValue(devices[j].Name);
                avgTemperatures.CreateCell(j).SetCellValue(temperatureRepository.GetAverageTemperature(devices[j].Id));
            }


            for (int i = 0; i < devices.Count; i++)
            {
                sheet = wb.CreateSheet(devices[i].Name);
                List <int> temperatures = temperatureRepository.GetTemperatures(devices[i].Id);

                var weekNumber      = sheet.CreateRow(0);
                var weekTemperature = sheet.CreateRow(1);

                for (int j = 0; j < temperatures.Count; j++)
                {
                    weekNumber.CreateCell(j).SetCellValue(j + 1);
                    weekTemperature.CreateCell(j).SetCellValue(temperatures[j]);
                }
            }

            using (var memoryStream = new MemoryStream())
            {
                wb.Write(memoryStream);
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(memoryStream.ToArray())
                };

                response.Content.Headers.ContentType = new MediaTypeHeaderValue
                                                           ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.Content.Headers.ContentDisposition =
                    new ContentDispositionHeaderValue("attachment")
                {
                    FileName = filename
                };

                return(response);
            }
        }