# Export to Excel/PDF

## Content

## Export to Excel

`FlexGrid` can export data to <span data-highlighted="true" data-vc="highlighted-text">CSV</span>, Text, <span data-highlighted="true" data-vc="highlighted-text">XLSX</span>/<span data-highlighted="true" data-vc="highlighted-text">XLSM</span>, and HTML using the `C1.Maui.Grid.Excel `assembly. That assembly exposes an Extensions class with an asynchronous `SaveAsync` method that serializes the `FlexGrid` into the chosen format and exports the result.

## Supported File Formats

These are the save file formats that this extension supports.

| @cols=1:**Member** | @cols=1:**Description** |
| ------ | ----------- |
| @cols=1:@rows=1:`.``<span data-highlighted="true" data-vc="highlighted-text" style="box-sizing: border-box;">xlsx</span>` | @cols=1:@rows=1:Exports to an Excel Workbook |
| @cols=1:@rows=1:`.``<span data-highlighted="true" data-vc="highlighted-text" style="box-sizing: border-box;">csv</span>` | @cols=1:@rows=1:Exports to a Comma Separated Values file |
| @cols=1:@rows=1:`.text` | @cols=1:@rows=1:Exports to a Text file |
| @cols=1:@rows=1:`.html` | @cols=1:@rows=1:Exports to an HTML file |

## Options

### Row and Column Options

This specifies options available for customizing the excel output when grid is exported. These are the row(s) and column(s) export options that this extension supports.

| @cols=1:**Option** | @cols=1:**Description** |
| ------ | ----------- |
| @cols=1:@rows=1:VisibleOnly | @cols=1:@rows=1:Renders only visible row(s) and/or column(s) |
| @cols=1:@rows=1:RenderFrozen | @cols=1:@rows=1:Renders the frozen row(s) and/or column(s) |
| @cols=1:@rows=1:SelectedOnly | @cols=1:@rows=1:Renders only the selected row(s) and/or column(s) |
| @cols=1:@rows=1:ExludeRange | @cols=1:@rows=1:Excludes explicitly specified ranges |
| @cols=1:@rows=1:ExcludeEmpty (Row only) | @cols=1:@rows=1:Excludes empty rows |
| @cols=1:@rows=1:RenderGroups (Row only) | @cols=1:@rows=1:Includes grouped rows |

### Header Options

This specifies constants that defines which header cells are displayed in the exported file. These are the row(s) and column(s) grid header visibility options that this extension supports

| @cols=1:**Option** | @cols=1:**Description** |
| ------ | ----------- |
| @cols=1:@rows=1:None | @cols=1:@rows=1:No header cells are displayed in the output file. |
| @cols=1:@rows=1:All | @cols=1:@rows=1:Both column and row header cells are displayed in the output file. |
| @cols=1:@rows=1:Column | @cols=1:@rows=1:Only column header cells are displayed in the output file. |
| @cols=1:@rows=1:Row | @cols=1:@rows=1:Only row header cells are displayed in the output file. |

### Rendering Options

This specifies additional options available for rendering merged ranges, formatted values, and images.

| @cols=1:**Option** | @cols=1:**Description** |
| ------ | ----------- |
| @cols=1:@rows=1:RenderMergedRanges | @cols=1:@rows=1:Render merged ranges in the output |
| @cols=1:@rows=1:RenderFormattedValues | @cols=1:@rows=1:Render cell values as formatted in the output |
| @cols=1:@rows=1:RenderImages | @cols=1:@rows=1:Includes images; otherwise exports image URIs |

## Sample Usage

### Export to <span data-highlighted="true" data-vc="highlighted-text">Xlsx</span>

The following example shows how to use the `SaveAsync` method to export `FlexGrid` to Excel files. In this example, we used the **Name** property to name the `FlexGrid` control as "grid".
`XML`

```auto
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="btnSave" Clicked="OnSave" />
    </ContentPage.ToolbarItems>
    <c1:FlexGrid x:Name="grid" FontFamily="Algerian" />
</ContentPage>
```

`CSharp`

```auto
using C1.Maui.Grid;
using GrapeCity.Documents.Excel;
private string FILENAME = "ExportedGrid";
async void OnSave(object sender, EventArgs e)
{
    var type = await DisplayActionSheet("Save As", "Cancel", null, "Excel");
    string filePath = "";
    //Windows   
    filePath = Path.Combine(
      Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments),
      FILENAME) + ".xlsx";
    //Android, iOS, Mac
    filePath = Path.Combine(
      Environment.GetFolderPath(
        Environment.SpecialFolder.LocalApplicationData),
      FILENAME) + ".xlsx";
    try
    {
      await grid.SaveAsync(
        filePath, "FlexGrid Sheet", //Sheet Name
        SaveFileFormat.Xlsx, //GrapeCity.Documents.Excel.SaveFileFormat: Represents the format in which the workbook is saved
        headers: GridHeadersVisibility.Column); //C1.Maui.Grid.GridHeaderVisibility: Specifies constants that defines which header cells are displayed
        //Windows
        System.Diagnostics.Process.Start(
          new System.Diagnostics.ProcessStartInfo
          { 
            FileName = filePath,
            UseShellExecute = true 
          });
        //Android, iOS, Mac
        await DisplayAlert("Saved", "File has been saved to: " + filePath, "OK");
    }
    catch
    {
        await DisplayAlert("Error", "File could not be saved", "OK");
    }
    break;
}
```

### Export to <span data-highlighted="true" data-vc="highlighted-text">CSV</span>, Text, HTML

The following example shows how to use the `Save` method to export `FlexGrid` to <span data-highlighted="true" data-vc="highlighted-text">CSV</span>, Text, and HTML files. In this example, we used the **Name** property to name the `FlexGrid` control as "grid".
`XML`

```auto
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml">
    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="btnSave" Clicked="OnSave" />
    </ContentPage.ToolbarItems>
    <c1:FlexGrid x:Name="grid" FontFamily="Algerian" />
</ContentPage>
```

`CSharp`

```auto
using C1.Maui.Grid;
private string FILENAME = "ExportedGrid";
async void OnSave(object sender, EventArgs e)
{
    var type = await DisplayActionSheet("Save As", "Cancel", null, "CSV", "Text", "HTML");
    string PathAndName = Path.Combine(
      Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
      FILENAME) + "." + type;
    switch (type)
    {
        case "CSV":
            grid.Save(
              PathAndName,
              GridFileFormat.Csv, //C1.Maui.Grid.GridFileFormat: Specifies file formats available for saving the grid
              System.Text.Encoding.UTF8,
              GridSaveOptions.SaveColumnHeaders);//C1.Maui.Grid.GrisSaveOptions: Specifies options available for customizing the files created by FlexGrid.Save Method
            break;
        case "Text":
            grid.Save(
              PathAndName,
              GridFileFormat.Text,
              System.Text.Encoding.UTF8,
              GridSaveOptions.SaveColumnHeaders);
            break;
        case "HTML":
            grid.Save(
              PathAndName,
              GridFileFormat.Html,
              System.Text.Encoding.UTF8,
              GridSaveOptions.SaveColumnHeaders);
            break;
    }
    if(type != "Cancel")
    {
        await DisplayAlert("Saved", "File has been saved to: " + PathAndName, "OK");
    }
}
```

>type=info
> * Use `SaveAsync` for Excel and PDF export.
> * Use `Save` for <span data-highlighted="true" data-vc="highlighted-text">CSV</span>, Text, and HTML export.
> * File paths must be handled using platform-specific storage APIs (e.g., `LocalApplicationData`).
> * File access behavior varies across platforms (Windows, Android, iOS, macOS).


<br>
<br>
