EXcel VSTO - Transferring a list object onto a worksheet - c#

I have a VSTO add in I am looking to implement.
I would like to click a button and a list of products, names, etc would be placed onto the worksheet.
I understand that I could go through each individual item in the list and then write this cell by cell, but is there a way of literally just 'dumping' the data onto the worksheet?
Apologies if this is a really thick question.

Nope, there is no easy way to just 'dump' the data. You're going to have to do it the hard way. Just google for some examples, it's easy enough.
http://www.google.nl/search?q=c%23+export+data+to+excel

That depends on where your "list of products, names, etc" are coming from. If those items are fixed, you can create a template document with a prepared worksheet containing these items. Put this document into an embedded resource of your program. When you want to create a new worksheet from this template, extract your resource to a temporary file and use the Excel API to copy the worksheet from your template to your working document.
Sounds perhaps more complicated than it is. Here
Read a file from a resource and write it to disk in C#
you find an example how to accomplish the "extract file from resource to temporary file" part.

Related

How to display a specific Excel Range in c#

For a little windows application, I need to display a specific range of a worksheet into a container of my WPF window. This range contains shapes and also notes that have to de displayed too.
This app takes a folder of many excel files with the same format. After choosing the folder, the user selects the file and the part of the file (Part One, Part two ...) The parts don't correspond to rows or columns, it is like "D28:AA39". The selection works well, selection of range too but once my Range is selected I don't know how to display it. Datagrid doesn't seem to display shapes and notes.
The app is supposed to be used on a small screen and with a device with not awesome specs so displaying in a new Excel file isn't a viable solution. The final result is to display the range in my C# window with all the data. Those data doesn't need to be alterable, they just need to be readable. Thanks a lot for helping me.
So I figured out myself how to do it the "simplest" way.
To make this opration i just used the [Range].CopyPicture() which allow you to copy the range as a picture in the clipboard.
Then you save this into as a temporary file and once it is done you just have to display an Image in a container.
Hope it will help other people.

How do I use a relative path to point to a file from which I wish to import data to my Data Model?

What I'm really trying to achieve
We have an Excel dashboard which is built to be used in tandem with an Export from our in-house application. The generation of the data Excel file is done with EPPlus and we were convinced that we could use the Excel Dashboard as the "base" file for the generation, so that the exported file contained the dashboard in one worksheet and the dataset in another worksheet.
So the user would receive everything they needed in one single file.
We sort of got going with this thanks to named ranges which dynamically adapt to the size of the dataset, covered here
However, we've found, that since the DataModel in the Excel dashboard file is an OLAP Cube, we can't update the underlying dataset and save it with EPPlus and receive an error about "the cache source is not a worksheet" when trying to save the sheet.
So while we worked on that, we sought out a temporary solution... where we've also hit a snag.
The new problem
The temporary solution we thought of is to distribute the Excel dashboard and the data as two separate Excel files. The Dashboard is distributed to those who need it and the data exports can then be generated from our application.
We thought that the only downside of this would be that it would require the user to rename and place the data file side by side with the Excel Dashboard manually.
However, we are running into an issue where Excel is insisting on using the absolute path to the data file instead of a relative path.
This results in requiring the user to point the source to the data export manually. Which is, apparently, done this way:
Now, I'm showing the entire process to also give some insight into the way we've set it up, since I'm not sure I'm using the right words about the technical details - and perhaps I'm grossly mistaken in the approach.
Overall, the way it works
File A contains the dashboard, some sheets with pivot tables used by the dashboard. The pivot tables are all working off of the aforementioned Data Model in the sheet which is a Cube (we need it to be a cube for some of the functions we use in the pivot tables). The Data Model is based on a named range including all the data in one of the worksheets.
File B is the one generated by our application. The two files were placed side by side when designing the dashboard and mapping the data since I was of the impression that Excel attempted to keep relative file paths when possible. The file consists of a single worksheet containing the exported data.
At this point, the challenge was to automatically pull in data from File B into the worksheet in File A. I did this by going to the Data tab and using the Get Data function to point it to File B and telling Excel to load the data from the specified sheet.
This worked like a charm although the "solution" seems overly complicated.
All was well until we tried to use sheet from another machine/directory. Then we found that the path to File B seemed to be absolute and that the data file could no longer be found.
So, the short question after this long-winded explanation is: "How can I possibly have Excel use a relative path to another file, when importing data from an external file with the "Get Data" function?"
By getting more familiar with Power Query I've been able to come up with a solution to this.
My setup is still as described above.
First, I have a sheet with some "system" values I use various places in the Workbook. I added a field there with the following Excel formula:
=LEFT(CELL("filename");FIND("[";CELL("filename");1)-1)
This provides me with the absolute path to the folder where my File A resides.
I use this value, and concatenate the expected filename of File B which holds the data. The result is an absolute path, pointing to where I expect the data file to be located.
I then added a name range pointing to the exact cell where this value is contained.
Next, I added a new Power Query function like this:
= (rangeName) => Excel.CurrentWorkbook(){[Name=rangeName]}[Content]{0}[Column1]
The function takes the name of a named range as a parameter and spits back the value. I my case I called the function GetValue. This function can now be used in other Power Query scripts.
Finally, I loaded up the Power Query scripts which is responsible for loading the data from the other Excel sheet. In that script, I changed the path of the file to this:
Source = Excel.Workbook(File.Contents(GetValue("FilePath")), null, true),
The single thing to notice here is the call of GetValue("FilePath") for the path parameter of File.Contents. FilePath is the name I gave the range pointing to the cell. All it does, is load the path from my sheet and use that as the path for the Excel sheet holding the data.
A quite convoluted solution, but it works.

Change Document template path in multiple Word Templates

I have about 500 word documents that I need to change the server name in the Document template path. I am not an expert with VBA but I have tried several solutions that have not worked for me. Is there a way to do this (perhaps with C#, with a foreach loop on the directory?) that I can do a very simple find and replace on this field ?
i.e.
\\ASDCFS\NtierFiles\...
becomes
\\NewServer\NtierFiles\...
You can't write to the field in the dialog box directly. In the object model the equivalent is Document.AttachedTemplate and yes, you can work with that. Using in the object model (whether using VBA or C#) you'd loop the documents in a folder, open each in Word, assign the correct path, save and close.
More efficient and less prone to "hiccups" if the original template path is already invalid, would be to edit the documents' Word Open XML directly, without using the Word application. The Open XML SDK would be a good tool for this. It provides the AttachedTemplate class (https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.attachedtemplate(v=office.14).aspx).
You can use WTC to correct the template path in a bulk of documents. You find the source code and binary on Github: https://github.com/NeosIT/wtc

How to read a range of cells (e.g.A1:G30) from Excel file to GridView

I have been looking for a solution all over the last days and I found that this library EPPlus allows retrieving in the same time formatting besides the actual data, plus charts, if needed, from Excel files which is what I am aiming at the moment.
Could you please explain to me step by step how to read a Range of cells from an Excel (like A1:P34) file that resides at a certain path, via ASP.NET/C#?
PATH would be something like //ServerName/Folder1/Folder2/Folder3/ExcelFileName.xlsx
I looked over the web, but there is not explicit documentation for my level of C# expertise on this. I tried several examples but none displayed the Excel Range into the webpage. (e.g this one.)
Note: the three examples I have tried all included an File Upload Control, I do not need such. I want to read the Excel file from a specified location over the local network.
EPPlus library is available here.
If you can recommend me any simpler resources to understand EPPlus on:
-reading from Excel
-writing from Excel
-reading charts from Excel
This EPPlus does seem wonderful in its functionality.
To read a file off the server take a look at this:
Open ExcelPackage Object with Excel application without saving it on local file path
Just need to set the var path part for your file.
To actually put the excel data on a web page, that is not so easy. See this:
Generating a HTML table from an Excel file using EPPlus?
Response to Comment:
Hosting an actual excel sheet in web page is temperamental at best but there are ways to do it (I haven't tried it personally). SharePoint is probably your best option if you have it available. If not, you would have to use an iFrame or some kind of office web component. Check this out:
how to display excel sheet in html page

How do I create a structure that can be pasted into Excel in C#

I want to create a data structure that I can copy to the clipboard in such a way that the user can paste it into an Excel worksheet and it inserts correctly into the columns and rows of the sheet.
Is there any way to create such a data structure? Or does it already exist?
I would like to avoid having to open up Excel and pasting the values myself because I can't determine if the worksheet will look the same in the future, so I'd rather have the user himself copy the rows and columns where he wants them.
When copying the data to clipboard, format it as Tab separated for columns, and Enter separated for rows. When pasting in Excel it will automatically put the values in rows and columns.
You can copy your data to clipboard in a tab-delimited textual format.
A tab or comma delimited string is the easiest and least technical solution.
Assuming you want something a little more complicated there are some superb libraries around (search CodePlex) which can offer creating Excel documents in managed code.
Or you could use the interop libraries that come ad part of the Visual Studio office integration.
Or you could use the XLSX format based on XML.

Categories

Resources