FlexSheet for WPF | ComponentOne
Working with C1FlexSheet / Importing and Exporting / Exporting FlexSheet
In This Topic
    Exporting FlexSheet
    In This Topic

    FlexSheet can be exported to .xls, .xlsx, .htm, .html, .csv, and .txt file formats using FileFormat and SaveOptions enums. It also exports .pdf file format using PdfExportOptions class, which provides options for pdf export and SavePDF method, which saves the grid to a PDF stream. The following steps illustrate exporting C1FlexSheet to these formats:

    Dim dlg = New Microsoft.Win32.SaveFileDialog()
    dlg.DefaultExt = "xlsx"
    dlg.Filter = "Excel Workbook (*.xlsx)|*.xlsx|" +
                 "Excel 97-2003 Workbook (*.xls)|*.xls|" +
                 "HTML File (*.htm;*.html)|*.htm;*.html|" +
                 "Comma Separated Values (*.csv)|*.csv|" +
                 "Text File (*.txt)|*.txt|" + "PDF (*.pdf)|*.pdf"
    
    If dlg.ShowDialog().Value Then
        Using s = dlg.OpenFile()
            Dim ext = System.IO.Path.GetExtension(dlg.SafeFileName).ToLower()
            Select Case ext
                Case ".htm", ".html"
                    flex.Save(s, FileFormat.Html, SaveOptions.Formatted)
                    Exit Select
                Case ".csv"
                    flex.Save(s, FileFormat.Csv, SaveOptions.Formatted)
                    Exit Select
                Case ".txt"
                    flex.Save(s, FileFormat.Text, SaveOptions.Formatted)
                    Exit Select
                Case ".pdf"
                    SavePdf(s, "ComponentOne ExcelBook")
                    Exit Select
                Case ".xlsx"
                    flex.SaveXlsx(s)
                    Exit Select
                Case Else
                    flex.SaveXls(s)
                    Exit Select
            End Select
        End Using
    End If
    
    var dlg = new Microsoft.Win32.SaveFileDialog();
    dlg.DefaultExt = "xlsx";
    dlg.Filter =
        "Excel Workbook (*.xlsx)|*.xlsx|" +
        "Excel 97-2003 Workbook (*.xls)|*.xls|" +
        "HTML File (*.htm;*.html)|*.htm;*.html|" +
        "Comma Separated Values (*.csv)|*.csv|" +
        "Text File (*.txt)|*.txt|" +
        "PDF (*.pdf)|*.pdf";
    
    if (dlg.ShowDialog().Value)
    {
        using (var s = dlg.OpenFile())
        {
            var ext = System.IO.Path.GetExtension(dlg.SafeFileName).ToLower();
            switch (ext)
            {
                case ".htm":
                case ".html":
                    flex.Save(s, FileFormat.Html, SaveOptions.Formatted);
                    break;
                case ".csv":
                    flex.Save(s, FileFormat.Csv, SaveOptions.Formatted);
                    break;
                case ".txt":
                    flex.Save(s, FileFormat.Text, SaveOptions.Formatted);
                    break;
                case ".pdf":
                    SavePdf(s, "ComponentOne ExcelBook");
                    break;
                case ".xlsx":
                    flex.SaveXlsx(s);
                    break;
                default:
                    flex.SaveXls(s);
                    break;
            }
        }
    }
    

    The implementation of the SavePDF method used in the above code is given in the following code:

    Private Sub SavePdf(s As Stream, documentName As String)
        Dim options As New PdfExportOptions()
        options.Margin = New Thickness(96, 96, 96 / 2, 96 / 2)
        options.ScaleMode = ScaleMode.ActualSize
        flex.SavePdf(s, options)
        s.Close()
    
    void SavePdf(Stream s, string documentName)
    {
        PdfExportOptions options = new PdfExportOptions();
        options.Margin = new Thickness(96, 96, 96 / 2, 96 / 2);
        options.ScaleMode = ScaleMode.ActualSize;
        flex.SavePdf(s, options);
        s.Close();
    }