How to access Active Workbook

Hello Team,

Can you please advice me how to access newly created workbook i.e. ActiveWorkbook?

Regards,
Manish

Hi,


Thanks for your posting and using Aspose.Cells.

When you open a workbook object, then assign it to some class scope variable as you have in your code e.g. curWrkbk. I have checked your code but you are not loading excel file inside a workbook object anywhere.

So suppose you do it in some functions then immediately assign it to curWrkbk as shown below.

C#
void f1()
{
Workbook wb = new Workbook(“a.xlsx”);
curWrkbk = wb; //here you have assigned the local workbook reference to class scope workbook reference.
}

Thanks for Reply Shakeel.

But I am talking about how to access a newly created workbook, that is not saved yet.

C#
<div class=“csharpcode” style=“font-size: small; color: black; font-family: “Courier New”, Consolas, Courier, monospace; background-color: rgb(255, 255, 255); white-space: pre;”>void f1()
{
curWrkbk = ActiveWorkbook; </font><font color="#006400">//here you have assigned the local workbook reference to class scope workbook reference.</font><font color="#800080">

<div class=“csharpcode” style=“font-size: small; color: black; font-family: “Courier New”, Consolas, Courier, monospace; background-color: rgb(255, 255, 255); white-space: pre;”>}

Regards,
Manish

Hi,


Thanks for your posting and considering Aspose.Cells.

Please see the following sample code. It creates 10 workbook objects and adds them in List collection object. Then it adds some data in 3rd, 5th and 8th workbook. Later, it retrieves the Workbook object from List collection object according to the given index.

And if you give index 2, it will retrieve 3rd Workbook object and save it on disk.
And if you give index 4, it will retrieve 5th Workbook object and save it on disk.
And if you give index 7, it will retrieve 8th Workbook object and save it on disk.
And if you give index n-1 where (n<10), it will retrieve nth Workbook object and save it on disk.

I have attached 3 such Workbooks and their screenshots for your understanding. I have also attached the sample project for your testing.

In short, you can get your active workbook from List collection object by providing it the active workbook index.

C#
class Program
{
List lst = new List();
static void Main(string[] args)
{
    Program pg = new Program();
    pg.CreateWorkbookObjects();
        
    //Add worksheet in 3rd workbook and cell data in A5
    pg.AddWorksheetInWorkbook(2, "MySheet3");
    pg.AddCellDataInWorkbook(2, "MySheet3", "A5", "This is 3rd Workbook.");

    //Add worksheet in 5th workbook and cell data in A5
    pg.AddWorksheetInWorkbook(4, "MySheet5");
    pg.AddCellDataInWorkbook(4, "MySheet5", "C10", "This is 5th Workbook.");

    //Add worksheet in 8th workbook and cell data in A5
    pg.AddWorksheetInWorkbook(7, "MySheet8");
    pg.AddCellDataInWorkbook(7, "MySheet8", "J9", "This is 8th Workbook.");

    //Select which of the workbook you want to save
    int idx;
    idx = 2;
    //idx = 4;
    //idx = 7; 

    //Save the selected workbook
    pg.SaveWorkbook(idx, "TestOutput.xlsx");
}

//Create 10 workbook objects
void CreateWorkbookObjects()
{
    //Create 10 workbook objects
    for(int i=0; i<10; i++)
    {
        Workbook wb = new Workbook();
        lst.Add(wb);
    }
}

//Add worksheet in your given workbook
void AddWorksheetInWorkbook(int wbIdx, string sheetName)
{
    Workbook wb = lst[wbIdx];
    wb.Worksheets.Add(sheetName);
}

void AddCellDataInWorkbook(int wbIdx, string sheetName, string cellName, string cellData)
{
    Workbook wb = lst[wbIdx];
    wb.Worksheets[sheetName].Cells[cellName].PutValue(cellData);
}

void SaveWorkbook(int wbIdx, string wbName)
{
    Workbook wb = lst[wbIdx];
    wb.Save(wbName);
}
}


Hi Expert,

Actually i am trying to make one separate com-addins which will only work for create presentation using aspose. which will call on a menu button click of Active workbook.

I do not need to create a new workbook using Aspose.

Regards,
Manish

Hi,


Thanks for your posting and using Aspose.Cells.

Please share your application screenshot so that we could get a clear idea.

i am trying to make one separate com-addins

What do you mean by com-addins? Please explain it with screenshots.

I have also attached the screenshot that shows my understanding of your requirements. Please check it, if it is what you want and if you want something else, you should give screenshots.