C1FlexGrid has always been quite easy to use, be it manipulation of Grid Styles, or exporting data to different file formats. Since ComponentOne Olap For Winforms uses the same inherent controls, like I said before, anything that can be done with C1FlexGrid can be done with OlapGrid as well, to some extent. In this post, I am going to shed some more light on the export features. Using OlapGrid, we can export all our data, obtained by using different operations via the OlapEngine to any of the file formats, supported by C1FlexGrid. So if you want the filtered data thus obtained, in a CSV file, which you can use directly in another application, say for example an MS Excel sheet, you could do an export to exactly that format. And if one wants to export the data directly to excel, vis-a-vis C1FlexGrid, that can be done too, for, at the code behind, the same methods are used to obtain similar results. I would like to explain with the help of this post, one implementation each of both the export methods that can be used, 1) SaveExcel 2) SaveGrid Lets start with the assumption that we already have an OlapApplication created, and running, maybe even take the same application we left with in the last post. So now once I have the data populated, I want the same to take around in an excel file. What do I do?
As simple as it sounds, somethings require a few tweaking here and there, after which we can savor what we were looking for in the first place. Lets place another button on the form, which says "Clickme to Export in Excel Sheet". Within the button click event, place the following code,
//Export Data to Excel file format, _fileName is a string that specifies name and path of file c1OlapPage1.OlapGrid.SaveExcel(_fileName, FileFlags.AsDisplayed | FileFlags.IncludeFixedCells);
The only thing that needs to be kept in mind is choosing the appropriate FileFlags required to export data as per our needs. For more details on choosing the FileFlags, and using them in different combinations, refer to my other blog post, http://our.componentone.com/2011/11/15/file-flags-using-combinations-while-exporting/
Another method that can be used for exporting to formats other than MS Excel is the SaveGrid method. To check its implementation, place another button on the form, giving it the text, "Clickme to Export not in Excel sheet", or any other relevant text of your choice:). Within the button click event, place the following code,
//Export Data to CSV file format, _fileName is a string that specifies name and path of file c1OlapPage1.OlapGrid.SaveGrid(_fileName, FileFormatEnum.TextComma);
Using this method gives the flexibility of choosing other file formats as well, that too by simply setting another enumeration value. The FileFlags enum, though can be used but with some restricted values, as some of its options are explicitly for the Excel File Format export. For more details about the implementation of everything mentioned above, refer to the attached sample, Download Sample