[]
        
(Showing Draft Content)

Importing FlexSheet

C1FlexSheet allows import of Excel files (.xls, .xlsx) and text file (.txt). This functionality is attained by using ImportFileFormat enum. The following code sample illustrates importing Excel files in C1FlexSheet:

vbnet

Dim dlg = New Microsoft.Win32.OpenFileDialog()
dlg.Filter = "Excel 97-2003 Workbook (*.xls)|*.xls|" +
             "Excel Workbook (*.xlsx)|*.xlsx" +
             "Text File (*.txt)|*.txt|"
If dlg.ShowDialog().Value Then
    Try
        Using s = dlg.OpenFile()
            Dim ext = System.IO.Path.GetExtension(dlg.SafeFileName).ToLower()
            Select Case ext
                Case ".txt"
                    flex.Load(s, ImportFileFormat.TXT)
                    Exit Select
                Case ".xlsx"
                    flex.Load(s, ImportFileFormat.XLSX)
                    ' ImportFileFormat uses namespace FlexGrid
                    Exit Select
                Case ".xls"
                    flex.Load(s, ImportFileFormat.XLS)
                    Exit Select
            End Select
        End Using
    Catch x As Exception
        Dim msg = "Error opening file: " & vbCr & vbLf & vbCr & vbLf + x.Message
        MessageBox.Show(msg, "Error", MessageBoxButton.OK)
    End Try
End If

csharp

var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Excel 97-2003 Workbook (*.xls)|*.xls|"
    + "Excel Workbook (*.xlsx)|*.xlsx" +
    "Text File (*.txt)|*.txt|";
if (dlg.ShowDialog().Value)
{
    try
    {
        using (var s = dlg.OpenFile())
        {
            var ext = System.IO.Path.GetExtension(dlg.SafeFileName).ToLower();
            switch (ext)
            {
                case ".txt":
                    flex.Load(s, ImportFileFormat.TXT);
                    break;
                case ".xlsx":
                    // ImportFileFormat uses namespace FlexGrid
                    flex.Load(s, ImportFileFormat.XLSX);
                    break;
                case ".xls":
                    flex.Load(s, ImportFileFormat.XLS);
                    break;
            }
        }
    }
    catch (Exception x)
    {
        var msg = "Error opening file: \r\n\r\n" + x.Message;
        MessageBox.Show(msg, "Error", MessageBoxButton.OK);
    }
}