create a report from template using C# excel OpenXML API - c#

I've been using Microsoft COM model to prepare reports but it is slow and fails if server does not have excel installed. So I am moving to using OpenXML for creating reports from a server process that does a ton of other things as well.
I've a template that contains named ranges for data that I need to change or extend, named charts, named chart series, named worksheets, etc. All my formatting is contained in excel file and all I do in my server side process is populate the file with data.
These are the steps that I need to replicate using Excel OpenXML:
Open an existing sheet and create a new sheet based on it.
Get the worksheet by worksheet name
Get the Named Ranges in that worksheet.
Populate the Named Ranges from data and extend the ranges if required based on data set.
Get Chart by Chart Name
Get Chart Series by Series Title
Set Chart series to the new range
Export Chart to jpeg to be used in html.
Save new File.
Is it possible to do these steps in OpenXML cleanly? Any examples will help me a lot. Or a light weight library that does this. Some of these 3rd party libraries are too big and are more useful in creating new sheets from scratch.
Any help appreciated.

AFAIK, with OpenXml you can do anything.
I recommend a ClosedXml lib, it wraps OpenXML SDK and makes it easier to do your job. Link contains samples and stuff. Hope that helps.

Related

How to generate excel like report in WPF?

I have this Excel Sheet That I want to generate from my WPF Application, What is the easiest way to do so, bear in mind that the table in the sheet may have many containers and each container may have one or more sizes and so on as shown.
I'v looked at the ReportViewer provided in WPF but I don't know how to design such a report using the ReportViewer.
Another thing came to my mind is to fill the excel sheet from the WPF and I found a way to do so by specifying tags (Ex: Date: ) in which I can fill these tags from my WPF application, but I don't know who to generate tables and set their columns and rows according to the data in the database.
Thank you.
Did you try adding Microsoft.Office.Interop.Excel as a dll, and try the code given here.
Or have a look at this sample:
WPF Spreadsheet
I have made good experience with the EPPlus library. The nice thing is, you don't need any MS Office components on your OS to generate the excel files.

How to save excel chart as an image using c#?

I have excel files which contains few charts, and I must display these charts on the web page. Additionally I can't install excel on server machine.
I've checked the following free libraries:
epplus, closedxml, spreadsheetlight.
But they are not support that kind of operation.
Do you know any free library which can help me with that?
I'm just going to put this here in case there are no other responses...
Without Excel and therefore interop -- you could have it extract the chart XML files from the XLXS (as if it were a ZIP) to parse/recreate the charts (possibly using System.Windows.Forms.DataVisualization.Charting?) and then save.

Can we export Excel spreadsheet into XML and apply stylesheet to transform to another format?

I have an excel spreadsheet which has one worksheet. The work sheet has 50 columns and 1000+ rows. And I want to transform the data inside the excel spreadsheet into another custom format.
Is it possible to use xml+xslt to transform the data inside the excel worksheet into another format - as I've recently read that excel data is xml under the hood?
My programming language of choice is C# (incase that is required)
Thank you
in theory yes but I think this could be rather complex...
Another approach is to use OpenXML SDK from MS - see http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5124
For some starting points
http://msdn.microsoft.com/en-us/library/bb456488.aspx
http://www.microsoft.com/download/en/details.aspx?id=17985
http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/06/02/4730.aspx
http://www.codeproject.com/KB/office/ExcelOpenXMLSDK.aspx
IF you need more (like rendering to high-quality PDF etc.) then there are several 3rd-party libraries available (like Flexcel, Aspose.Cells, SpreadsheetGear...).

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