Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Open the workbook.
            var workbook = SpreadsheetGear.Factory.GetWorkbook(@"t:\tmp\Chart.xlsx");
            // Get a chart named "Chart 1" on the sheet named "Sheet1".
            var chart = workbook.Worksheets["Sheet1"].Shapes["Chart 1"].Chart;
            // Get a System.Drawing.Bitmap image of the chart.
            var bitmap = new SpreadsheetGear.Drawing.Image(chart).GetBitmap();

            // Save it to a file and launch just to see that it worked.
            bitmap.Save(@"t:\tmp\Chart.png", System.Drawing.Imaging.ImageFormat.Png);
            System.Diagnostics.Process.Start(@"t:\tmp\Chart.png");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the chart image
        /// </summary>
        /// <returns></returns>
        private Image GenerateImage()
        {
            Image Retval = null;
            try
            {
                this.moSpreadsheet.GetLock();

                //-- Set the aspect ratio of the image
                this.SetAspectRatio();

                SpreadsheetGear.Shapes.IShapes ExcelShapes = this.moSpreadsheet.ActiveWorksheet.Shapes;
                SpreadsheetGear.Drawing.Image SpreadsheetImage = new SpreadsheetGear.Drawing.Image(ExcelShapes[0]);

                Bitmap ExcelBitmap = SpreadsheetImage.GetBitmap();
                Retval = (Image)ExcelBitmap;
            }
            catch (Exception)
            {
            }
            finally
            {
                this.moSpreadsheet.ReleaseLock();
            }
            return Retval;
        }
Ejemplo n.º 3
0
 public SpreadsheetGear.Drawing.Image GetImage()
 {
     SpreadsheetGear.Drawing.Image image = new SpreadsheetGear.Drawing.Image(cells[allRange]);
     return(image);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// It replaces vertor Map image (.emf), present in xls presentation, with bitmap image like .png, .jpeg.
        /// </summary>
        /// <param name="mapPresentationFilePath"></param>
        public static bool ReplaceVectorMapImageWithBitmap(string mapPresentationFilePath, ImageFormat desiredBitmapFormat, string resultantOutputFilePath)
        {
            bool RetVal = false;
            SpreadsheetGear.IWorkbook workbook = null;
            SpreadsheetGear.IWorksheet worksheet = null;
            SpreadsheetGear.Shapes.IShapes ImagesShapes = null;
            SpreadsheetGear.Shapes.IShape OriginalMapShape = null;
            byte[] MapImageBytes = null;

            try
            {
                if (string.IsNullOrEmpty(mapPresentationFilePath) == false)
                {
                    if (File.Exists(mapPresentationFilePath))
                    {
                        //- Load xls.
                        workbook = SpreadsheetGear.Factory.GetWorkbook(mapPresentationFilePath);
                        worksheet = workbook.Worksheets[0];

                        //- Get all shapes present in WorkSheet 1
                        ImagesShapes = worksheet.Shapes;

                        if (ImagesShapes != null)
                        {
                            //- Load Map Image (Generally 2nd Shape in Sheet 1 represents Map image.)
                            foreach (SpreadsheetGear.Shapes.IShape imgShape in ImagesShapes)
                            {
                                if (imgShape.Type == SpreadsheetGear.Shapes.ShapeType.Picture && imgShape.Name == "Picture 2")
                                {
                                    OriginalMapShape = imgShape;

                                    //- Load Image into memory
                                    SpreadsheetGear.Drawing.Image MapImage = new SpreadsheetGear.Drawing.Image(imgShape);
                                    System.Drawing.Bitmap MapBitmap = MapImage.GetBitmap();
                                    MemoryStream ms = new MemoryStream();
                                    ms.Position = 0;

                                    //- Convert Map to png format
                                    MapBitmap.Save(ms, desiredBitmapFormat);
                                    MapImageBytes = ms.ToArray();
                                    break;
                                }
                            }

                            //- Save and close Workbook.
                            if (MapImageBytes != null)
                            {
                                // overwrite on previous Map shape.
                                worksheet.Shapes.AddPicture(MapImageBytes, OriginalMapShape.Left, OriginalMapShape.Top, OriginalMapShape.Width, OriginalMapShape.Height);

                                //- Delete Previous Image
                                OriginalMapShape.Delete();

                                if (File.Exists(resultantOutputFilePath))
                                {
                                    try
                                    {
                                        File.Delete(resultantOutputFilePath);
                                    }
                                    catch
                                    { }
                                }

                                workbook.SaveAs(resultantOutputFilePath, SpreadsheetGear.FileFormat.XLS97);

                                RetVal = true;
                                workbook.Close();
                                workbook = null;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                if (workbook != null)
                {
                    workbook.Close();
                }
            }

            return RetVal;
        }