Nevron Open Vision Documentation
Framework / Document Formats / Excel Files
In This Topic
    Excel Files
    In This Topic
     Reading Excel Files

    Nevron Open Vision makes it easy to read data from Excel Workbooks (XLSX files) and Excel 2003 Workbooks (XLS files) into an NDataSet. To do this, you should use the NExcelReader static class placed in the Nevron.Nov.Formats.Excel namespace of the Nevron.Nov.Presentation.dll.

    Use any of the Read methods to read all or some of the data from the Excel workbook. All of the Read methods have a Boolean parameter headerRow, which specifies whether to use the first row of each sheet as column headers. If the headerRow parameter is set to false,

    Reading an Excel file
    Copy Code
    NFile file = NFileSystem.GetFile(@"C:\Documents\ExcelWorkbook.xlsx");
    file.OpenRead(
        delegate (Stream stream)
        {
            using (stream)
            {
                NDataSet dataSet = NExcelReader.ReadAll(stream);
            }
        },
        delegate (Exception ex)
        {
            // Reading of the file failed
        }
    );
    
     Writing Excel Files

    To create and write Excel Workbooks (XLSX files) create and instance of the NExcelWriter class placed in the Nevron.Nov.Formats.Excel namespace of the Nevron.Nov.Presentation.dll. Set the Author property of the created Excel writer instance to value of your choice. It is by default set to "Nevron Software".

    Writing an Excel file
    Copy Code
    NFile file = NFileSystem.GetFile(@"C:\Documents\ExcelWorkbook.xlsx");
    file.Create(
        delegate (Stream outputStream)
        {
            using (outputStream)
            {
                NExcelWriter writer = new NExcelWriter();
                writer.Write(outputStream, dataSet);
            }
        },
        delegate (Exception ex)
        {
            // Reading of the file failed
        }
    );
    

    If you pass an NDataTable to the Excel writer, the data table will be exported as the single sheet in the created Excel Workbook. The name of the data table determines the title of the sheet.

    If you pass an NDataSet to the Excel writer, each data table of the data set will be exported to a different sheet in the created Excel Workbook. The names of the data tables determine the titles of the sheets.   

    You can also pass a 2D object array to the Excel writer's Write method. In that case you should also pass a string that specifies the name of the Excel sheet for the object array's data. The first row of the 2D array should contain the column headers. Values in the 2D array are addressed as dataArray[rowIndex + 1, columnIndex], because of the first column header row, where rowIndex and columnIndex are zero-based. For example, the value of the cell on the second row and fifth column should be specified by the element dataArray[2, 4].

    Writing a 2D object array to Excel
    Copy Code
    object[,] data = new object[,]
    {
        { "Name", "Country", "Birth Date", "Weight" },
        { "John", "USA", new DateTime(1973, 8, 21), 85 },
        { "Elizabeth", "United Kingdom", new DateTime(1991, 3, 7), 58 },
        { "Olga", "Russia", new DateTime(1995, 11, 16), 52 },
        { "Ronaldo", "Brazil", new DateTime(1982, 3, 30), 72 },
    };
    NFile.Create(filePath,
        delegate (Stream outputStream)
        {
            using (outputStream)
            {
                NExcelWriter writer = new NExcelWriter();
                writer.Write(outputStream, "Persons", data);
            }
        }
    );
    
    See Also