Change datasource for chart in Word c# - c#

I'm trying to create a word-document on the fly and in there I'm suppose to have a chart. For that, I have
doc.InlineShapes.AddChart(Microsoft.Office.Core.XlChartType.xlCylinderCol, ref oRange);
However that opens Excel, reads data from some default data source of some kind and closes again.
How do I control this chart and choose the data source, and labels on axis?

This helped me a lot when I had the exact same question -
How to add graph in word
The example shows adding a graph as an OLE object, but the AddChart method works in a very similar manner. To add a graph to a Range, you would essentially do
InlineShape objShape = doc.InlineShapes.AddChart(XlChartType.xlCylinderCol, ref oRange);
To get access to the relevant objects
Chart objChart = objShape.Chart;
Workbook book = objChart.ChartData.Workbook;
Worksheet dataSheet = book.Worksheets["Sheet1"];
Now you can manipulate all the properties on the Chart and Datasheet like Axes, Data, Colors etc.
Another helpful tip, if you are not sure how to find something in the API, fire up Excel and start Record Macro to capture the changes you want, and then look at the Macro code. Looking at the Recorded Macros usually sets me on the right path when I know how to do something using the UI but not in the API.

Related

Get Array Formula value for cell in Excel-DNA

I have an Excel DNA Ribbon and a set of formulae that return arrays.
I'd like to add a button on the ribbon which expands an array formula out to the size of the data - much like this: http://excel-dna.net/2011/01/30/resizing-excel-udf-result-arrays/ but on the click of a button rather than when the UDF is run.
I have a reference to the cell:
ExcelAsyncUtil.QueueAsMacro(
() =>
{
ExcelReference current = XlCall.Excel(XlCall.xlfActiveCell) as ExcelReference;
});
but don't know what to do from here. If I try to use object value = current.GetValue(); I just get the display value of the individual cell.
I tried to access the Formula but I don't want to have to evaluate every parameter individually.
Any ideas would be gratefully received.
From the ribbon handler, it's probably easier to do this using the COM object model. Perhaps this post has some VBA that would give you a start: https://newtonexcelbach.wordpress.com/2015/04/14/re-sizing-array-functions/
In your Excel-DNA add-in, you get the root Application object with a call to ExcelDnaUtil.Application.

Exporting a ListView to Excel

I have a ListView that I need to export to an Excel spread sheet. There a bunch of documentation to export GridViews and to a lesser extent ListViews to Excel but not so much in terms of doing some customization before export.
Before exporting I need to prep the data, as a) only a subset of the columns need to be exported b) some of these columns are editable textbox controls.
Doing a simple export without preping the table means that the control gets exported as well which obviously looks really ugly.
At the moment I can iterate the controls and remove the formatting from both the Grid and ListViews thus solving b.
In the GridView and I can just set some the columns I want to hide using Visible = false; and then set back to true when I have finished.
The thing I can't figure is how to do this with the listview.
I am wondering what is the best way to do this as I am kind of stuck.
Thanks,
Michael
I actually think this is a dumb question. If I want to export the list view to excel then i'll export all columns removing the unnecessary formatting. Then users can do any customization of the columns in Excel.
To do any customization of the data prior to export then I should use the underlying data types.
The above approach (admittedly my own) is simply bad.

Excel Interop XSD and XPath

I am succesfully binding XSD to Excel using Interop (C#) and XPath.
However, there is one thing I am unable to succeed at.
When using the Developer toolbar in Excel it is possible to bind an entire XSD to a cell. This creates some sort of merged row in the Excel sheet for all the attributes in the node. When expanding this row one can receive multiple values for each attribute from XML.
I would like to have this behaviour as well in the code I wrote, but so far I have only been able to map attributes sepperatly to a single cell.
So the question is:
Can I bind an entire Node to a cell using interop? Instead of binding the attributes sepperatly.
Thanks!
Edit:
This is a screenshot of what I have now:
http://imageshack.us/photo/my-images/193/badl.png/
And this is a screenshot of what I want (Manually done in Excel):
http://imageshack.us/photo/my-images/406/goodu.png/
I'm unsure on your particular case, but if you're using Excel interop then you can use a little trick.
Create a new macro (in Excel 2007 its View->Macro).
Perform the manual action.
Stop the macro.
Then Step-into the macro and see what the VBA code looks like, will give you a starting point
on how to do it with Interop.
It seems that I have figured out what to do.
I was creating a List object for each cell and then binding it using xpath.
What I had to do was span the List object over multiple columns (using a range object), and then bind each cell in the list using xpath.

How do I make a Excel cell contents invisible

I have an excel file(VSTO), which will be uploaded with data.
However I want to make one cell invisible, as it contains one of the ID's which should not be revealed to the user.
I use Excel.Range.Value2 property to set the value.
Now I want to make the value inside it invisible
I am using C#, how do I accomplish the above.
This will make cell content invisible, untill you click on them then you can change the data. Here
Microsoft.Office.Interop.Excel.Range descriptionRange31 = xlApp.get_Range("A18,A25,B18,B25");
descriptionRange31.NumberFormat = ";;;";
I am not sure how to make it invisible. However, you can just copy and save the value in a variable in memory and replace it by string.Empty.
Declare the variable in your plug-ins application object in such a way that it stays alive as long as Excel is open.
If you want to persist it even when Excel application is closed, then either write it to disk or save it in the properties of the Excel workbook (the same place that is visible in Properties page of the workbook) after encoding it.
I have worked on the VSTO Excel applications.
In my project i have used specials Ids to identify each column uniquely..
So, i would suggest rather than getting into coding you can do following 2 actions to make it invisible.
Go to ur visual studio, open design mode of the workbook.
Enter ur ids in columns or rows , wherever u want to (lets assume in column C and Row 5,6)
1.You can see the horizontal / vertical strips at the top and left side of excel which we use to resize the columns n rows ..just adjust it in a such a way that that particular column will be hidden
as u can see the image, i adjusted columns to Hide Column "C" and Rows to hide row no 5 and 6.
2.Now open excel (not from Visual stodio, from windows explorer) pres Alt+T+P+P
and protect the sheet using a perticular password and your are done.
Now user can not make any changes in columns or rows and date remains invisible.
Regards,
Sangram Nandkhile
I'm not sure you can make the contents of a cell "invisible." However, you could go a long way toward that goal by:
Placing the hidden information within a hidden column in a separate worksheet
Password protecting the worksheet structure to prevent unhiding of that hidden column
Hiding the protected worksheet
Password protecting the workbook's VBA project from editing
The security of this approach is not 100% because Excel protection is not exceedingly difficult to break. However, the combination of obscurity (since the worksheet tab is not even visible to the user) and protection of the VBA Project from editing (so even if someone knows the hidden sheet is there, it won't be as easy to code the unhide method) and password protection of the worksheet structure (so it won't be easy to unhide the protected column) should prevent all but the most determined peekers from seeing what you're hiding.
I have not worked with VSTO, so I can't code this for you. The approach is valid though, and all API hooks to achieve it are probably available.
I'm not sure if this answer match your level of question, but this if I would into your problem description. I would just make the font color same as the background and lock the cell from editing (which you of course handle from code, while write/change the value) and go for locking the excel sheet except the cells/regions that explicitly marked as opened.

How do you programmatically break up one Excel document into several while still maintaining each cell's style and format?

m trying to break one large Excel spreadsheet into several. I've made good progress, but I'm running into some problems. Specifically, the values that get copied over don't retain their format (for instance, 40322 instead of 5/24/2010 and -101 instead of (101.00) ). I've tried using the style (see below) but that doesn't even get me the font, let alone the number format. Any help or a poke in the right direction would be appreciated.
There are 2 loops, one for row, one for column.
destinationSheet.Cells[i, j].Style = sourceSheet.Cells[i, j].Style;
Instead of looping for each cell, you can copy/paste the entire range of cells using the pastespecial method.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial(VS.80).aspx

Categories

Resources