We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Exporting chart to open Excel Workbook

Thread ID:

Created:

Updated:

Platform:

Replies:

94805 Jun 2,2010 10:42 AM Jun 4,2010 08:18 AM Windows Forms 3
loading
Tags: XlsIO
Farman Samee
Asked On June 2, 2010 10:42 AM

Hi,

I want to add a worksheet/chartsheet to a workbook which is already open and active. I try to do this using (C#):

ExcelEngine excelEngine = new ExcelEngine();
IWorksheet sheet = excelEngine.Excel.ActiveWorkbook.Worksheets.Create();

but ActiveWorkbook is always null so I get an exception. How can I access the open/active workbook? I saw a similar post below where you suggest

IWorksheet sheet = application.ActiveWorkbook.Worksheets.Create();

But application always refers to a System object when I try this. Any solutions?

Thanks




Geetha M [Syncfusion]
Replied On June 3, 2010 12:44 AM

Hi,

Thank you for your interest in Syncfusion products.

The code snippet you have used does not have any workbook initialized and thus causes exception. If you are to create a new workbook, you can make use of the following code.

ExcelEngine excelEngine = new ExcelEngine();
IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];

// user code goes here

IWorksheet newSheet = excelEngine.Excel.ActiveWorkbook.Worksheets.Create();

Please try this and let me know if you have any questions.

Regards,
Geetha

Farman Samee
Replied On June 3, 2010 05:55 AM

Ok thanks- does this mean that I can't access the workbook which is currently active in Excel, unless I have just created it myself using the Excel Engine object? I don't want to create the (currently open) workbook myself, the user will have opened this manually. I just want to access it, in order to add some sheets. Can I do this? (ps. please bear in mind I can't seem to access the Excel 'Application' object, if I need to do this can you let me know the full namespace).
Thanks


Geetha M [Syncfusion]
Replied On June 4, 2010 08:18 AM

Hi,

Thank you for the details.

I was able to reproduce the NullReference exception when accessing ActiveWorkbook after loading a workbook. Could you please workaround this using the following code snippet?

ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open(@"..\..\Input.xlsx", ExcelOpenType.Automatic);

IWorkbook book = application.ActiveSheet.Workbook;

IWorksheet sheet = book.Worksheets.Create("New");

workbook.SaveAs("Sample.xlsx");
workbook.Close();
excelEngine.Dispose();

The workaround is to load the Workbook of the ActiveSheet.

Also, IApplication interface can be accessed using Syncfusion.XlsIO namespace.

Please try this and let me know if you have any questions.

Regards,
Geetha

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;