Creating Pivot Table in Excel using Open XML and C# - c#

I used store a data into the DataTable and wants to create a Pivot Table using this data.
I have gone through many posts and also read the Microsoft's documentation on creating Pivot table through Open XML but couldn't get the solution. Can anyone please post some code to create Pivot table.
Thanks in advance.

Install the Open XML SDK, it contains a tool called Open XML SDK Productivity Tool.
How to use it:
Open Excel and create a file containing a Pivot table. Save it and open it with the Productivity Tool. In this tool is an option to 'Reflect the code', that way you can see how to create the same Excel by using C# code.

Related

Reading particular columns of Excel sheet

I need to read particular columns from an Excel sheet (say Columns A,P,Q,B) and also some particular cells (say C3 or D10). I do not need it to be displayed in a DataGrid view or anything (all examples I have seen use DataGrid).
How do I do that and write them into a new CSV file?
I have no sample code as I do not know how to proceed.
I sugest you use the ExcelReaderInterop library.
using Microsoft.Office.Interop.Excel;
Detailed example can be found here:
http://www.dotnetperls.com/excel
I had to deal with a similar issue a few weeks ago and could not find a simpler way to deal with this. The post suggest this overkill approach may be due to lots of legacy code in the library.
We have successfully used the Microsoft Access Database engine to open and read Excel files. The "2010 Access Redistributable" can also be installed on a server free of charge. What you asking for is a multi-step process:
Open a connection to the file using the Access OleDbConnection. In the connection string the "Data Source" is the file name.
Select the appropriate worksheet, which will return a DataTable object.
Grab a row from the data table or iterate over top of all of them myDataTable.Rows.
Access the column in question.
This post shows some of the process:
SSIS Excel Source Connection. What does it use to read Excel?
Hopefully this gets you pointed in the right direction.
I copied the columns that I needed to another excel sheet and saved it as CSV. Then read this csv to perform the task.
This was the easiest option as the machine I was suppose to run the program didn't have Microsoft office.

How can I unzip Office Open XML file in C# and modify its data and re-zip again?

From a console application written in C#, how can I :
extract an Office Open XML file,
Obtain the data part of it modify
the data and re-zip it again
My motivation is to save an excel file with the formats and use it to populate cells via a console application.
Is this possible to achieve, do I need a specific library to that provides Excel files manipulation (unzipping it, modifying it etc.)
For the zip-unzip part i think you can find easily many examples here.
To edit the excel file, I'd suggest you to have a look at Open XML SDK. With it, you can easily edit office files programmatically.
Hope it helps

Importing data into Excel using XML and Excel Templates

I am generating CSV data from a C# application. This can be imported into Excel easily but I need formatting applied to the file.
One option is interop but the machine running this application will not have Office products installed so that is out.
I've been told that XML can work with Excel templates and am looking for a starter example on how to achieve this.
I have generated excel spread sheets using the excel 2003 xml format several times but you will have to consider the following features that cannot be supported using this format:
This XML Spreadsheet 2003 file format (.xml) does not retain the following features:
Auditing tracer arrows
Chart and other graphic objects
Chart sheets, macro sheets, dialog sheets
Custom views
Data consolidation references
Drawing object layers
Outlining and grouping features
Password-protected worksheet data
Scenarios
User-defined function categories
VBA projects
If that is acceptable you can use as someone suggest an open source library that allows you to generate the spreadsheet in code or as I have done you can generate the xml using either an xml transform or a using the spark template engine. Both have worked for me in the past but using the spark view engine was probably the nicest.
The best way to achieve either of these is to create a template the way you want it to look and save it as a Excel 2003 Xml format and look at the raw xml. This should make it easy for you to generate your output. You can also download the xml definition for reference.
You can use excellent OpenXML wrapper ClosedXML to generate xlsx files with formatting. Or if you want, you can use pure OpenXML. OpenXML installation is required for ClosedXML to work.

Display a table from Excel as html table - c# asp.net

I have an Excel file with a formatted table. Right now I have a database in sql server, I'm pulling the data into my c# asp.net program. I want to know if anyone has an idea of what I should try to use that Excel format to display data from a query at a webpage without having to go in and construct the table from scratch. I tried looking for a good tutorial on how to import an excel file and use it as a template for crystal reports, had no luck. Any advice would be greatly appreciated!
I have used a third party component called ASPOSE.NET to acheive this. ASPOSE provides extensive formatting options and support a wide variety of file formats.
However it is not a free product.

Dataset to excel

I'm developing an application that as to export a dataset to excel, this dataset have 3 tables and the relation between them, I would like to export this to a single excel sheet where for each parent table there is a '+' sign that when expanded shows the child values related to it. To export a single table to excel I know how to do it, but to create this type of relation I have no idea how to do it, do I have to create a macro on the excel file?
I use a DevExpress Reports or a ActiveReports for this. I create a normal report and next i save it in xls, and Done. Its very complicated to this using us code. Its more easy to use a third-party components
There's a free tool: https://closedxml.codeplex.com/ to create XLSX outputs from code.

Categories