Created an Excel workbook in c# but charting is disabled? - c#

I have a c# application that generates an Excel workbook. The problem is that when you open the workbook and click on the "Insert" menu, "Chart" is disabled. Any idea on how to enable this option when creating the workbook in c#?

I write this part taken from here.
Hope this helps...
Sounds like you have the setting to
not display objects on the sheet (a
chart is an object).
You probably
can't add a rectangle, either!
Find
the option in Office/Excel Options.
Or easier, press ctrl/6 (not F6, 6).
Bob Umlas
Excel MVP
EDITED:
Another solution taken from here.
Yes, the Shared workbook option will
grey out the chart wizard.
You would
have to remove the sharing to get the
wizard back in that workbook.

Related

Visual Studio 2013 Excel VSTO Add WPF Or Windows Form To Document

I want to know how to add a WPF Form or a Regular Windows Form to an excel Spreadsheet to collect data.
For example, I want a button, when clicked to open a WPF/WF which has a couple of checkboxes, and then I want to pass those values back to the worksheet. How would I do that?
As far as I know, there is no way to actually embed forms in a worksheet unless you were to wrap it up as an OLE object (yuck). You can however, show a dialog form to collect user input and have your VSTO AddIn populate or gather data from the excel sheet. I have used this approach with ease for cryptography scenarios where sharing sensitive data requires a runtime password.
In case you are not limited to an Excel AddIn, you could also push/pull data from an external instance of Excel. Let me know if any of these scenarios suit you and if you would like help with code.

opening sheet and selecting cells excel/c#

Hello I am looking for a way to open an Excel sheet programmatically and allow the user to be able to select a range of cells. My reasoning behind this is I am currently developing a lot of individual excel upload forms for pulling data in to a database I would like the ability to select the column headers as simply as highlighting the cells through excel and presenting a textbox for each to enter the equivalent DB table column name and store these. So effectively a form to create a form.
Many thanks
Charlie
Please check this article: Opening and Navigating Excel with C#.
There is also a section in MSDN library that completely covers Excel PIA.

Show Excel workbook without save in C#

I am creating an Excel file in an WinForm app (VS2008) in runtime. I am exporting some data into it from datagridview. After export, i would like to show this file in excel, but dont want to save anywhere. My goal is to use it as an alternative of creating many reports in crystal. Is it possible ?
If you are using Interop Service, you only can make workbook application visible equals to true.
[workbookobject].Application.Visible = true;

Using templates in excel with C#

As part of the c# program that I am producing I need to generate 1 workbook containing; 2 different worksheets and a 3rd that could be produced any number of times, what is the most effective way of doing this? I have looked into using templates although I am unsure how to repeat certain worksheets whilst only displaying others once. Any help or advice would be appreciated.
A simple way is to make a hands off template example workbook with the three worksheets. Then make a copy of it. Open both and re-copy worksheet number 3 on to the working workbook as a new worksheet as needed.
In response to the comment:
There are a couple of excel engines in a .net component products our there like spreadsheet gear or aspose cells. But if your application is a windows form based and where the application is guaranteed to run has office you can use office automation. You can't legally use office automation on a web server, but it is just as possible on a web server as on a client desktop. I've used the aspose cells and it's very easy to work with and very capable and a little less expensive than spreadsheet gear, but spreadsheet gear does also have a good reputation. Both of those components have very good documentation on how to do anything with excel. But if you have excel and want to use office automation, be sure to look for example code on the web on how to properly close excel from c# or vb.net. There are some tricks to getting it to close properly.
SpreadsheetGear for .NET has ISheet.CopyAfter / CopyBefore methods which enable you to copy an entire worksheet within a workbook or between workbooks.
You can see an example of duplicating a single worksheet multiple times in the Worksheet with Chart to Multiple Worksheets with Charts sample on the SpreadsheetGear / Excel Reporting Samples page here.
Disclaimer: I own SpreadsheetGear LLC
I have done this before with templates. I would create a template xls with the first two worksheets that you don’t want changed, then add a 3rd worksheet that you could copy to the end of the workbook (as you need more worksheets).
If you know ahead of time how many of the 3rd worksheet you need, then you can copy them to the end and delete the template 3rd slot.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.CopyWorksheetToEnd(3);
t.RemoveAtIndexWorksheet(3);
t.SetSomeValue(3);
t.SetSomeValue(4);
t.Close();
If you don’t know, then keep the template around to copy it to the end as needed, then when you are done, just remove the 3rd worksheet template.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(4);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(5);
t.RemoveAtIndexWorksheet(3);
t.Close();
I used the Microsoft.Office.Interop.Excel dll to create my ExcelTemplateManger class. The basic idea is to create a copy of the template excel file, and work off the copy. Let me know if you need help setting that part up, but it should be too bad.

Programmatically generating Excel workbook with pivots and charts

I need to create a workbook which has a raw data on a sheet and a pivot table on another sheet. The pivot is created from the raw data. Then I need to create a chart with this pivot on the same or new sheet.
How can I do this in C# - and is this possible using VSTO?
Easiest way to do it?
Use Open XML SDK 2.0
Build a pre-generated version of the workbook with raw data, pivottable and pivotchart already created.
Install the OpenXML SDK 2.0 setup package
Use the OpenXML SDK 2.0 productivity tool from this install to open the workbook
Select the root element on the left hand pane, and then right click and select Reflect Code
Done - on the left pane is a complete C# class with the necessary code to generate the file.
Well not quite done as you still need to refactor the code to take into account dynamically adding the required data content, however this will get you 97% of the way if you already know C# fairly well.
I've done something similar to this before, but I didn't do it using C#. I used VBA since the language is already built in to Excel.
My approach was this:
Create a worksheet called "RAW DATA." This worksheet has a QueryTable object in it that can be updated via code in VBA.
I manually created a pivot table based on the QueryTable in the RAW DATA worksheet.
I then added code in VBA so that after RAW DATA was updated, Pivot Table was refreshed with the new data.
This method works really well if the layout of your raw data and your pivot table stays the same. I have a workbook that I made for a cowork that updates multiple sheets with pivot tables based on one set of data. She really likes it because just by clicking one button, she has a refreshed view of all of her data.
If this approach works for you and you'd like more details as to how to implement some of those methods, let me know more details of your situation and I can try to help you out.
One option is to connect to database from Excel and refresh the "Raw data" sheet, via VBA or defining an SQL query in Pivot data source. This is not so great as the user who opens the file must be able to connect to the database.
The other option is to fill the "Raw data" sheet programatically via C#. There are numerous libraries that can help you with that, even some free ones, but you can also do it yourself by using the Excel XML format (SpreadsheetML). You can use the Excel 2003 XML format or the new Open XML Excel format. The latter is far more complicated, but with it you can also take advantage of the OpenXML SDK and the Excel Package API.

Categories

Resources