Example #1
0
        private void exportTargetFile(string id, string ext, string type, ReportViewer rv, ReportSettings settings)
        {
            var tempImagePath = Path.Combine(Path.GetTempPath(), id + ext);
            var dir           = Path.GetDirectoryName(tempImagePath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
            }
            var fileBuffer = rv.LocalReport.Render(type, settings.IsChanged() ? settings.ToString() : null);

            File.WriteAllBytes(tempImagePath, fileBuffer);
        }
Example #2
0
        private void OutputGenerate(ControllerContext context)
        {
            var reportFile = context.HttpContext.Server.MapPath(_reportFile);
            var oldReport  = context.HttpContext.Request.QueryString["__oldrdlReport"];

            DeletePreGeneratedFile(oldReport);
            if (!File.Exists(reportFile))
            {
                OutputJson(new GenerateReasult
                {
                    Code    = 1,
                    Message = "报表" + _reportFile + "不存在!"
                }, context);
                return;
            }
            var rv = new ReportViewer();

            rv.LocalReport.ReportPath = reportFile;
            var settings = new ReportSettings();

            if (_reportBuilder != null)
            {
                try
                {
                    _reportBuilder(rv.LocalReport, settings);
                }
                catch (Exception ex)
                {
                    OutputJson(new GenerateReasult
                    {
                        Code    = 2,
                        Message = ex.Message
                    }, context);
                    return;
                }
            }
            try
            {
                rv.LocalReport.Refresh();
                var reportFileId = Guid.NewGuid().ToString();
                var img          = _exportFiles.ElementAt(0);

                exportTargetFile(reportFileId, img.Value, img.Key, rv, settings);

                Task.Factory.StartNew(args =>
                {
                    try
                    {
                        if (!(args is Tuple <string, ReportViewer, ReportSettings> tuple))
                        {
                            return;
                        }
                        for (var i = 1; i < _exportFiles.Count; i++)
                        {
                            var cfg = _exportFiles.ElementAt(i);
                            exportTargetFile(tuple.Item1, cfg.Value, cfg.Key, tuple.Item2, tuple.Item3);
                        }
                    }
                    // ReSharper disable once EmptyGeneralCatchClause
                    catch
                    {
                    }
                }, new Tuple <string, ReportViewer, ReportSettings>(reportFileId, rv, settings));

                var tiffImg   = Image.FromFile(Path.Combine(Path.GetTempPath(), reportFileId + ".tif"));
                var guid      = tiffImg.FrameDimensionsList[0];
                var dimension = new FrameDimension(guid);
                var pageCount = tiffImg.GetFrameCount(dimension);
                tiffImg.Dispose();

                OutputJson(new GenerateReasult
                {
                    Code         = 0,
                    FileId       = reportFileId,
                    PageCount    = pageCount,
                    DownloadName = settings.DownLoadFileName
                }, context);
            }
            catch (Exception ex)
            {
                OutputJson(new GenerateReasult
                {
                    Code    = 3,
                    Message = GetFirstExceptionMessage(ex)
                }, context);
            }
        }