FlexGrid provides various methods to save the grid in the desired format such as text, excel, xml and image formats.
To save grid content as a text file, you can use the SaveGrid method of the Extensions class. This method has parameters that let you choose delimiter, encoding, file location and the portion of the grid to be saved. It allows you to specify the file format for saving the grid content using the FileFormatEnum parameter and provides various options for saving the different ranges or selections from the grid to a text file through the FileFlags parameter.
The following table describes all the options provided by FileFlags parameter.
Options | Description |
---|---|
AsDisplayed | Saves values as displayed. |
IncludeFixedCells | Includes fixed cells when loading or saving the grid. |
SelectedRangesOnly | Saves only selected ranges. The selected range must lie within a common row or column range. |
SelectedRowsOnly | Saves only selected rows. |
VisibleOnly | Saves visible rows and columns only. |
The resulting text files can later be loaded back into the control, or into other applications that support comma or tab-delimited files such as Microsoft Excel.
Following code shows how to save content of the WinForms FlexGrid as a text file.
To save grid as an excel file, you can use the above mentioned SaveGrid method and simply set the format parameter to FileFormatEnum.Excel. You don't need to have Microsoft Excel installed on your computer. However, SaveGrid method can only save the data in a workbook with single worksheet.
To get an additional control over how you save your data to an excel file, you can use the SaveExcel method instead. Just like the SaveGrid method, the SaveExcel method also has similar parameters that allow you to specify the file location and which portion of the grid to be saved. The FileFlags parameter lets you save different ranges and selections from the grid to an Excel file by providing the following options.
Options | Description |
---|---|
AsDisplayed | Saves values as displayed. |
ExcludeEmptyRows | Excludes empty rows when exporting. |
IncludeFixedCells | Includes fixed cells when loading or saving the grid. |
IncludeMergedRanges | Saves merged ranges when exporting to Excel. |
NoFreezing | Does not freeze rows and columns when exporting to excel. |
Outline | Saves nodes as Excel groups when exporting to Excel. |
SaveMergedRanges | Saves merged ranges when exporting to Excel. |
SelectedRangesOnly | Saves only selected ranges. The selected range must lie within a common row or column range. |
SelectedRowsOnly | Saves only selected rows. |
VisibleOnly | Saves visible rows and columns only. |
In this case, the process of saving excel files converts most data types and formatting information, including row and column dimensions, fonts, colors, formats, and cell alignment. However, some other features such as frozen and merged cells, images, data maps, and cell borders are not translated while converting to an excel file.
Use the code below to save the WinForms FlexGrid as an Excel file.
To serialize grid contents into an XML document, you can simply call WriteXml method of the C1FlexGrid class and parse path of the XML document as a parameter along with the choice of the FlexGrid element to be saved using XmlOptions enumeration. Using XmlOptions parameter, you can save ColumnInfo, RowInfo, Ranges, Control, Styles, Maps, Tree, Glyphs, and Images as XML.
Objects of custom types stored in the grid are also serialized as long as they have an associated System.ComponentModel.TypeConverter that provides conversions to and from string.
Below code demonstrates how to save contents of the WinForms FlexGrid to an XML document.
Further, sorted, filtered, merged and/or grouped elements of FlexGrid can be saved to an XML file using the WriteXml method. The XML file stores the output elements as they are on FlexGrid.
To save a grid as an image, you can create the grid image using CreateImage method of the C1FlexGrid class and save that image object at the specified path. You can also specify a cell range or pass a cell range object as its parameter to save a specific portion of the grid as an image.
Use the code below to save the WinForms FlexGrid as an image.