Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Apr 04, 2024 2:38 pm

Hi,

We use Spire.XLS to generate screenshots of excel files and I want to make improvements to the screenshot process.
Is it possible to get the active selected range from the excel file?

I would then be able to calculate the firstRow, firstColumn, lastRow and lastColumn.

Thank you in advance.


I attached an excel file with an active selection.

Below you can find the code we use to generate the screenshot.


Code: Select all
using (Workbook workbook = new Workbook())
            {
                workbook.LoadFromFile(filePath);
               
                using (Worksheet sheet = workbook.Worksheets[workbook.ActiveSheetIndex]) { 
                   
                    sheet.PageSetup.IsPrintGridlines = true;
                    sheet.PageSetup.BottomMargin = 0;
                    sheet.PageSetup.LeftMargin = 0;
                    sheet.PageSetup.RightMargin = 0;
                    sheet.PageSetup.TopMargin = 0;
           
                    using (var image = sheet.ToImage(1, 1, 30, 15)) {
                        image.Save(stream, ImageFormat.Png);
                        getHeapSizeAfter();
                    }
                }
            }


Best,

Erick
Attachments
excel file with active selection.zip
Excel file with active selection
(11.86 KiB) Downloaded 387 times

elabnextdeveloper
 
Posts: 8
Joined: Wed Jan 25, 2023 12:22 pm

Fri Apr 05, 2024 4:23 am

Hi,

Thanks for your inquiry.
Do you want to convert all of the filled cells into image? If so, you just modifys the parameter of sheet.ToImage() method, as following code:

Code: Select all
sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn);


I put complete code below for your reference:
Code: Select all
using (Workbook workbook = new Workbook())
            {
                workbook.LoadFromFile(filePath);

                using (Worksheet sheet = workbook.Worksheets[workbook.ActiveSheetIndex])
                {

                    sheet.PageSetup.IsPrintGridlines = true;
                    sheet.PageSetup.BottomMargin = 0;
                    sheet.PageSetup.LeftMargin = 0;
                    sheet.PageSetup.RightMargin = 0;
                    sheet.PageSetup.TopMargin = 0;

                    using (var image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn))
                    {
                        image.Save(stream, ImageFormat.Png);
                        getHeapSizeAfter();
                    }
                }
            }


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Apr 05, 2024 7:25 am

Hi Abel,

Thank you for the reply. The thing I want to achieve is a bit different though. I want to use the active selection in the sheet to generate the screenshot. I've attached an image to highlight what i want to use as screenshot, so cells Z53:AG64. The selection is saved in the excel file, when i reopen the file the selection will still be active.

I've noticed there is an SelectionCount, but the Selections array is internal and it looks like i can't use that, is that the case? Do you have an alternative how I would be able to achieve this?

Best,

Erick
Attachments
excel active selection.jpg
Excel active selection
excel active selection.jpg (138.42 KiB) Viewed 6294 times

elabnextdeveloper
 
Posts: 8
Joined: Wed Jan 25, 2023 12:22 pm

Fri Apr 05, 2024 10:29 am

Hi,

Sorry for I misunderstand your mean, I reopen the excel you provided, I do find there are a shadow area, as following image, could you tell me how to add this shadow to excel file using Office-Excel software? Since according to the way of adding shadow with office-excel, I will do further investigation to check if the location of shadow area can be obtained by Spire.Xls.
1.png
1.png (79.65 KiB) Viewed 6287 times


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Mon Apr 08, 2024 8:00 am

Hi,

The "shadow" (selection) can be created by dragging the mouse from one cell to another. Below I've included an example how this could be achieved using Microsoft Interop. Can you provide me an example how I can do this with Spire.XLS?

Code: Select all
using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelSelectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Excel application instance
            Excel.Application excelApp = new Excel.Application();
           
            // Open the workbook
            Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\Path\To\Your\Excel\File.xlsx");
           
            // Get the active worksheet
            Excel.Worksheet worksheet = (Excel.Worksheet)excelApp.ActiveSheet;
           
            // Get the active selection
            Excel.Range selection = excelApp.Selection;

            // Output the address of the selection
            Console.WriteLine("Active Selection: " + selection.Address);

            // Close the workbook and Excel application
            workbook.Close();
            excelApp.Quit();

            // Release COM objects
            ReleaseObject(worksheet);
            ReleaseObject(workbook);
            ReleaseObject(excelApp);
        }

        // Method to release COM objects to avoid memory leaks
        static void ReleaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine("Exception occurred while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}


Thank you in advance.

Best,

Erick

elabnextdeveloper
 
Posts: 8
Joined: Wed Jan 25, 2023 12:22 pm

Mon Apr 08, 2024 9:31 am

Hello,

Thanks for your feedback.
Now, Spire.Xls don’t support to get the active selection, but I have logged it as a new feature to our updating system with the number SPIREXLS-5195, and our Dev team will achieve it. There are any information about this, I’ll inform you in time.

Beside, I recommend you to use the following alternative solution before this feature is achieved. You can right click the active selection and define it and save the excel file, as shown in the following screenshot.
123.png
123.png (346.4 KiB) Viewed 6235 times


Than, you can get this cell range through the following code
Code: Select all
  using (Workbook workbook = new Workbook())
            {
                workbook.LoadFromFile(@"../../data/Map1.xlsx");

                using (Worksheet sheet = workbook.Worksheets[workbook.ActiveSheetIndex])
                {

                   CellRange ce = sheet.Range["test"];


               
                    Console.WriteLine("Active Selection:"+ce.RangeAddress);

  }
            }



Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Thu May 23, 2024 10:05 am

Hi,

Thanks for your patiently waiting.
I’m glad to inform you that the latest version of Spire.Xls 14.5.3 include your fix with the ticket number SPIREXLS-5195. You can use the following code to get the active range:
Code: Select all
Worksheet worksheet = workbook.Worksheets[0];
 string Information = null;
 foreach (CellRange range in worksheet.ActiveSelectionRange)
 {
 Console.WriteLine( "RangeAddressLocal:"+ range.RangeAddressLocal);
}

You can download the latest version from the following link:
website: https://www.e-iceblue.com/Download/down ... t-now.html
Nugut: https://www.nuget.org/packages/Spire.XLS/14.5.3

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.XLS