Beispiel #1
0
        private void SaveXlsxPackageToServer(OperationPackage package, string packageFileName, SftpClient client)
        {
            var filenameXlsx = packageFileName
                               + ".xlsx";

            using var excel      = viewExecutor.ExecuteXlsx(package, Properties.PackageName, UseAllSets);
            using var streamXlsx = new MemoryStream();

            excel.SaveAs(streamXlsx);
            streamXlsx.Position = 0;
            client.UploadFile(streamXlsx, Path.Combine(FolderPath, filenameXlsx));
        }
        private void AddDataSetsXlsx(OperationPackage package, MailMessage msg,
                                     MemoryStream streamXlsx, string fileName)
        {
            var excel = viewExecutor.ExecuteXlsx(package, fileName, UseAllSetsXlsx);

            excel.SaveAs(streamXlsx);
            excel.Dispose();

            streamXlsx.Position = 0;

            msg.Attachments.Add(new Attachment(streamXlsx, $@"{fileName}.xlsx",
                                               @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        }
Beispiel #3
0
        private void SaveXlsxPackageToServer(OperationPackage package, string packageFileName,
                                             NetworkCredential credentials)
        {
            var packageNameXlsx = packageFileName
                                  + ".xlsx";

            var uri = Path.Combine(Host, FolderPath, packageNameXlsx);

            if (WebRequest.Create(uri) is FtpWebRequest request)
            {
                request.Method      = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = credentials;
                using (var excel = viewExecutor.ExecuteXlsx(package, Properties.PackageName, UseAllSets))
                {
                    using (Stream requestStream = request.GetRequestStream())
                        excel.SaveAs(requestStream);
                }
            }
        }
Beispiel #4
0
        } //ctor

        public void Execute(IReportTaskRunContext taskContext)
        {
            var package = taskContext.Packages[Properties.PackageName];

            if (!RunIfVoidPackage && package.DataSets.Count == 0)
            {
                return;
            }

            string filename = (string.IsNullOrEmpty(ReportName)
                                  ? $@"{Properties.PackageName}"
                                  : taskContext.SetStringParameters(ReportName))
                              + (DateInName
                                  ? null
                                  : $" {DateTime.Now:dd.MM.yy}");

            string filenameJson = $@"{filename}.json";
            string filenameXlsx = $@"{filename}.xlsx";

            using (var client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], 25))
                using (var msg = new MailMessage())
                {
                    client.DeliveryFormat = SmtpDeliveryFormat.International;
                    client.EnableSsl      = true;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;

                    msg.From = new MailAddress(ConfigurationManager.AppSettings["from"]);
                    msg.AddRecepientsFromGroup(addresses);

                    if (!string.IsNullOrEmpty(RecepientsDatasetName))
                    {
                        msg.AddRecepientsFromPackage(taskContext.Packages[RecepientsDatasetName]);
                    }

                    msg.Subject = filename;

                    var dataset = parser.GetPackageValues(package).First();

                    viewExecutor = dataset.GroupColumns != null && dataset.GroupColumns.Any()
                    ? autofac.ResolveNamed <IViewExecutor>("GroupedViewex")
                    : !string.IsNullOrEmpty(ViewTemplate)
                        ? autofac.ResolveNamed <IViewExecutor>("CommonTableViewEx")
                        : autofac.ResolveNamed <IViewExecutor>("commonviewex");

                    if (HasHtmlBody)
                    {
                        msg.IsBodyHtml = true;
                        msg.Body       = viewExecutor.ExecuteHtml(ViewTemplate, package);
                    }

                    MemoryStream streamJson = null;
                    MemoryStream streamXlsx = null;

                    try
                    {
                        if (HasJsonAttachment)
                        {
                            var sets       = parser.GetPackageValues(package);
                            var dataToSave = UseAllSetsJson
                            ? JsonConvert.SerializeObject(sets)
                            : JsonConvert.SerializeObject(sets.First());

                            streamJson =
                                new MemoryStream(System.Text.Encoding.UTF8
                                                 .GetBytes(dataToSave));
                            msg.Attachments.Add(new Attachment(streamJson, filenameJson,
                                                               @"application/json"));
                        }

                        if (HasXlsxAttachment)
                        {
                            streamXlsx = new MemoryStream();
                            var excel = viewExecutor.ExecuteXlsx(package, filename, UseAllSetsXlsx);
                            excel.SaveAs(streamXlsx);
                            excel.Dispose();
                            streamXlsx.Position = 0;
                            msg.Attachments.Add(new Attachment(streamXlsx, filenameXlsx,
                                                               @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
                        }

                        var tryCount = 0;
                        while (tryCount < 3)
                        {
                            try
                            {
                                client.Send(msg);
                                break;
                            }
                            catch (Exception exc)
                            {
                                if (tryCount == 2)
                                {
                                    throw new Exception("Message not sent", exc);
                                }
                                else
                                {
                                    tryCount++;
                                }
                            }
                        }
                    }

                    finally
                    {
                        streamJson?.Dispose();

                        streamXlsx?.Dispose();
                    }
                }
        } //method