FlexGrid for WinForms | ComponentOne
Save, Load and Print / Save
In This Topic
    Save
    In This Topic

    FlexGrid provides various methods to save the grid in the desired format such as text, excel, xml and image formats.

    Save as Text File

    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.

    private void btnSaveTxt_Click(object sender, EventArgs e)
    {
        c1FlexGrid1.SaveGrid("../../ExportedGrid.txt", FileFormatEnum.TextComma, FileFlags.AsDisplayed | FileFlags.IncludeFixedCells);
    }
    
    Private Sub btnSaveTxt_Click(ByVal sender As Object, ByVal e As EventArgs)
        c1FlexGrid1.SaveGrid("../../ExportedGrid.txt", FileFormatEnum.TextComma, FileFlags.AsDisplayed Or FileFlags.IncludeFixedCells)
    End Sub
    
    Note: To use the SaveGrid method, add C1.Win.C1FlexGrid.ImportExport for .NET Framework, and C1.Win.FlexGrid.ImportExport for .NET

    Save as Excel 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.

    private void btnSaveExcl_Click(object sender, EventArgs e)
    {
        c1FlexGrid1.SaveExcel("../../ExportedGrid.xlsx",  FileFlags.AsDisplayed | FileFlags.IncludeFixedCells);
    }
    
    Private Sub btnSaveExcl_Click(ByVal sender As Object, ByVal e As EventArgs)
        c1FlexGrid1.SaveExcel("../../ExportedGrid.xlsx", FileFlags.AsDisplayed Or FileFlags.IncludeFixedCells)
    End Sub
    
    Note: To use the SaveExcel method, add C1.Win.C1FlexGrid.ImportExport for .NET Framework, and C1.Win.FlexGrid.ImportExport for .NET.

    Save as XML

    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.

    private void btnWriteXML_Click(object sender, EventArgs e)
    {
        c1FlexGrid1.WriteXml("ExportedGrid.xml", XmlOptions.All);
    }
    
    Private Sub btnSaveXML_Click(ByVal sender As Object, ByVal e As EventArgs)
        c1FlexGrid1.WriteXml("../../ExportedGrid.xml", XmlOptions.All)
    End Sub
    

    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.

    Save as Image

    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.

    private void btnSaveImg_Click(object sender, EventArgs e)
    {
        Image gridImage = c1FlexGrid1.CreateImage();
        gridImage.Save("../../ExportedGrid.png");
    }
    
    Private Sub btnSaveImg_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim gridImaage As Image = c1FlexGrid1.CreateImage()
        gridImaage.Save("../../ExportedGrid.png")
    End Sub
    
    See Also

    Documentation