Example #1
0
        protected virtual void DoSaveChart(string sheetName, string fileName, SaveChartOptions options)
        {
            options ??= new SaveChartOptions();
            var spread = options.Spreadsheet?.Workbook ?? Workbook;

            ChartObject chart = null;
            Image       chartImage;
            var         chartSheet = spread.ChartSheets[sheetName];

            if (chartSheet != null)
            {
                chart = chartSheet.Chart;
            }
            if (chart == null)
            {
                var worksheet = spread.Worksheets[sheetName];
                if (worksheet != null && worksheet.Charts.Count > Math.Max(0, options.ChartIndex ?? 0))
                {
                    chart = worksheet.Charts[options.ChartIndex ?? 0];
                }
            }

            if (chart == null)
            {
                throw new Exception("Cannot find chart.");
            }

            if (options.Size != null && options.Size.Length != 2)
            {
                throw new Exception("Invalid size of the image. Must be 2-elements integer array.");
            }
            if (options.Size != null)
            {
                var imageSize = new Size(options.Size[0], options.Size[1]);
                chartImage = chart.GetImage(imageSize).NativeImage;
            }
            else
            {
                chartImage = chart.GetImage().NativeImage;
            }

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new Exception("Filename for image is not specified.");
            }

            fileName = Project.Current.MapPath(fileName);
            var dir = Path.GetDirectoryName(fileName);

            if (!Directory.Exists(dir))
            {
                throw new Exception($"Directory '{dir}' does not exist.");
            }

            if (File.Exists(fileName))
            {
                if (options.Replace)
                {
                    File.Delete(fileName);
                }
                else
                {
                    throw new Exception($"File '{fileName}' already exists.");
                }
            }

            System.Drawing.Imaging.ImageFormat format = (options.Format ?? GetImageFormatFromFileName(fileName)) switch
            {
                ImageFileFormat.Png => System.Drawing.Imaging.ImageFormat.Png,
                ImageFileFormat.Tiff => System.Drawing.Imaging.ImageFormat.Tiff,
                ImageFileFormat.Bmp => System.Drawing.Imaging.ImageFormat.Bmp,
                ImageFileFormat.Gif => System.Drawing.Imaging.ImageFormat.Gif,
                ImageFileFormat.Jpeg => System.Drawing.Imaging.ImageFormat.Jpeg,
                _ => System.Drawing.Imaging.ImageFormat.Png
            };
            chartImage.Save(fileName, format);

            if (options.CopyToBook)
            {
                CopyImageToBook(chartImage, options.CopyToBookScale, options.CopyToBookScale, options);
            }
        }
    }
Example #2
0
 public SCSpreadsheet SaveChart(string sheetName, string fileName, SaveChartOptions options = null)
 {
     ExecuteSynchronized(options, () => DoSaveChart(sheetName, fileName, options));
     return(this);
 }