ComponentOne FlexGrid for WinForms
Loading and Saving Open XML Files
FlexGrid for WinForms Task-Based Help > Loading and Saving Open XML Files

You can load and save Microsoft Excel 2007 Open XML files within a C1FlexGrid control. Open XML is an open, standards-based format introduced by Microsoft in Office 2007. Open XML files contain a number of XML files compressed using Zip compression. Because they are compressed, Open XML files are usually much smaller than traditional document files (such as XLS files).

By default, the Load and Save methods in C1FlexGrid select the appropriate file format automatically based on the file extension. Any files with a "XLSX" or "ZIP" extensions are treated as Open XML files.

The LoadGrid and SaveGrid methods also have overloads that take the file type as a parameter. This allows you to take control over the file format and not rely on the file extension. For example, your application may use the Open XML format for its data files, but with an extension other than "XLSX". You can specify the file type as "Excel" and use the OpenXml option of the FileFlags enumeration.

To save and load a grid as an OpenXml file, complete the following steps:

  1. Add three buttons to the form containing the C1FlexGrid control and enter Save, Clear, and Load in the Text property for each, respectively, in the Visual Basic Properties window.


  2. From the Designer, double-click the Save button, Button_1, and enter code in the Button1_Click event, so it looks like the following:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.C1FlexGrid1.SaveGrid("C:\test\myfile.xlsx", FileFormatEnum.Excel, FileFlags.OpenXml)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void button1_Click(object sender, EventArgs e)
            {
                this.c1FlexGrid1.SaveGrid(@"C:\test\myfile.xlsx", FileFormatEnum.Excel, FileFlags.OpenXml);
     
            }
    
    Note: You must have the Imports or using statement at the top of your form in order for this code to work correctly. If coding in Visual Basic, use Imports C1.Win.C1FlexGrid. If coding in C#, use using C1.Win.C1FlexGrid;. For more information, see the Namespaces topic.
  3. Enter the following code for the Button2_Click event to be used for the Clear button:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.C1FlexGrid1.Clear(ClearFlags.All)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void button2_Click(object sender, EventArgs e)
            {
                this.c1FlexGrid1.Clear(ClearFlags.All);
            }
    
  4. Enter the following code for the Button3_Click event to be used for the Load button:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.C1FlexGrid1.LoadGrid("C:\test\myfile.xlsx", FileFormatEnum.Excel, FileFlags.OpenXml)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void button3_Click(object sender, EventArgs e)
            {
                this.c1FlexGrid1.LoadGrid(@"C:\test\myfile.xlsx", FileFormatEnum.Excel, FileFlags.OpenXml);
     
            }
    

Run the project:

  1. Enter content in the grid, as desired.
  2. Click the Save button. The grid is saved as an Open XML file with an .xlsx extension to the "C:\test" folder. You must create this folder if it does not already exist.
  3. Click the Clear button to clear the grid.
  4. Click the Load button, and the grid you previously saved is loaded.
See Also