export to excel from sharepoint - complex - c#

There is some code/werbparts on our site that display a lot of data in a grid on our site. In addition, they have another page with a few charts.
I need a way to create functionality to export this to excel so the user can click a button and save everything.
The chart will actually have to be built in excel, I can't just grab the picture from the website and display it.
Also, the data from the grid will have to be put into a spreadhseet. I am not as worried about this part.
I have exported to excel before, but it was just basically printing some data which was tab delimited with a certain doc type .
This actually will need to use different work sheets and use some excel functionality.
(I need a better answer than use excel services)
Thanks!

Use Epplus is a third party library for creating and manipulating excel files(xlsx)

Dunno about sharepoint, but the OpenXML SDK might be what you're after:-
http://msdn.microsoft.com/en-us/library/bb448854.aspx

Related

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

Is there any way to render Excel files in MVC4 C#?

i have a problem, i need to show a group of excel files in some views of an mvc4 project. The real problem is that the files are Excel PowerPivot and they can not be shown as images or pdf, it has to be Excel files.
Any ideas?
Thanks for the help...
If you can open the workbook in the Browser-based version of excel and see what you need to display, you can then use the Share->Embed feature of the Excel online app to generate HTML or JavaScript embed code.
If you need to have the end user manipulate the workbook to retrieve the necessary data from the PowerPivot table, then they'd most likely be better off downloading the workbook and going from that.
If they don't need to manipulate anything, look into if you can export the workbook to HTML in some fashion and embed that.
Hopefully that might put you down the right track.

Silverlight Datagrid to Excel export with cell formatting

Is there a way to do this in browser? (no AutomationFactory)
I know that I can export as CSV fine but I want to export my Silverlight datagrid as a Excel (.xls) document with the background colors of some rows set.
If you want to interact with MS Excel using Silverlight CLR - it is the only way to use AutomationFactory (becouse of silverlight pesmission restrictions). Ms Excel should be installed on client machines.
But you can also pass data to web-service, process it, and return excel sheet as byte array.
I suggest you use the second way.
I found this guide which is pretty much what I needed, although it is not elegant. Basically, loop through and print all the rows into XML format. I find it very strange that Microsoft would not provide an in-browser method to export to an Excel format and we need to do stuff like this
http://www.codeproject.com/Articles/45731/Export-Silverlight-DataGrid-to-Excel-XML-CSV
Export grid from silverlight to excel mht will work for any colors and formattings.

importing excel data while retaining formatting

Is it possible to import excel data to display using C# in any view(data grid etc ..) while retaining formatting like Bold, italic and strikethrough, throughout the document? I am making a Windows form application. My data is in an excel sheet, I want to display it in grid view or any other while retaining the formatting.
Well, there are some ways that I can think of but neither of them are very pretty at all and they would not be very effecient.
1) Parse the excel file cell by cell using Excel Interop. (Very nasty)
2) If you can convert the excel file to Excel XML you could then write say a XSLT to parse the file to pull styling data much more quickly.
Of course getting the styling data out is one thing. Applying it can be a whole other experience. Once you get the styling you have to make sure you apply the values to all the correct columns. Again very nasty and alot of parsing.
However since you did not say whether this was a web or desktop app you could always just embedd an excel control (on the web side) to display the data which may solve your problem. On a desktop app you are probably in for a world of pain trying to do what I describe above.
Maybe somebody will have a better answer but that's what I got.
SpreadsheetGear for .NET can load Excel workbooks and provides access to formatted text values as well as cell formatting such as fonts and colors.
SpreadsheetGear also comes with a Windows Forms spreadsheet control which will allow users to display, edit, format, calculate, print, etc... workbooks with formatting, charts, etc... intact.
If you are building an ASP.NET application, you can use SpreadsheetGear to open a workbook and generate an image of cells and / or charts as shown on our "Excel Chart and Range Imaging Samples" page here
You can download the free trial here if you want to try it out.
Disclaimer: I own SpreadsheetGear LLC

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