With that in mind, I'll review now a tool to do just that: export data to various formats. The tool is Spire.DataExport for .NET. Some of the features of this tool are:
Support for popular formats: MS Excel, MS Word, HTML, MS clipboard, XML, PDF, DBF, SQL Script, SYLK, DIF, CSV.
Exports data from Command, ListView and DataTable components.
Independent from data handling applications such as Microsoft Excel
This article will be both a walk through and a review. For that, I'll create a simple example that shows the real usage of the tool. Since XML is the format I worked with in many projects, I'll show the feature to export to XML.
- Visual Studio (you can download Visual Studio Community Edition here)
- Spire.DataExport installed. For this, download Spire.DataExport .msi file and execute it.
- SQL Server installed. You can download the express edition here.
Create a new project
1. I created a simple Windows Form Application. I named it "SpireDataExportTest":
2. Add a DataGridView component and a button. It should look like this:
AS you can see, I modified the text of the button. You can do other UI modifications as well if you think you need to.
3. Add the Spire.DataExport reference.
3.1 Right click References > Add Reference...
3.2 Click Browse and go to the path where you installed Spire.DataExport. The default path is C:\Program Files (x86)\e-iceblue\Spire.DataExport\Bin\NET4.0, in the case you have .NET Framework 4 (you might have to check your .NET Framework version. You can consult this if you have doubts).
Now, click OK.
4. Connect the grid to the database
There are several steps you need to take for this. Although it's not too complicated, I won't list all the steps here because the goal of the article is to do a review of the Spire.DataExport component. You might need a more detailed explanation, so I highly recommend to read this article.
However, this is the code you should have in Form1.cs up to this point:
And this is the database I used:
I called "testdb". Take into account that the Connection String in the code depends on this database and its configuration. Let's move on.
Code the action of exporting to XML
1. Double click the "Export to XML" button
2. Inside the method "button1_Click", enter the the code to export to xml. It should look like this:
As you can see, the method names are very straightforward. The only detail that you have to take care of is that xmlExport.DataTable only receives DataTable. So, what I did was declaring DataSet ds = new DataSet(); at the class level and then I converted the first table of the data set to DataTable.
The last thing left is to test the code. So, click Start and then click "Export to XML" once the program is running. You should see the data in XML:
The most important feature of this particular Spire product is that it's very simple to understand, learn and use. In fact, when writing this article, it was far harder to connect the database to the data grid than adding the functionality to export to XML. Another point is that the methods have clear names. This is very important, and many developers tend to forget that detail.
I really enjoy using these products, and I look forward to try the next one. More information about this tool and other similar products here.