C# - Creating FileStream start at certain row?

Posted on 2012-08-13
Last Modified: 2012-08-21
Hello all,

I am reading in an excel file into a FileStream as I am using an excel reader with that stream.  User will be inputting what row is the header row in a particular file.  Have files that come in where I need to eliminate some garbage header rows from the file.  So right now I have for example:

FileStream fileStream = new FileStream(dto.DataSourceFile.FullFilePath, FileMode.Open, FileAccess.Read);

How do I read in the file starting at a certain row for example Row 5 as an example?

Thanks all.
Question by:sbornstein2
    LVL 35

    Expert Comment

    you can't do that.

    What you can do is read data into a data structure like a datatable or an array and then access whatever row you want

    Author Comment

    Well essentially I want to use the DataTable.ColumnName as the end result to get the column name.  The excel reader code gets it into a dataset:

     IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fileStream);
                    excelReader.IsFirstRowAsColumnNames = true;
                    DataSet ds = excelReader.AsDataSet();
                    DataTable dt = ds.Tables[0];

    Then I was going to loop through the column count get the columnname.  The IsFirstRow flag sets the dataset column names.  Problem is the 1st row is not my header row in certain scenarios.  I do not want to use Microsoft Interop at all as it sucks.
    LVL 13

    Accepted Solution

    >>  I do not want to use Microsoft Interop at all as it sucks.
    TOTALLY AGREE with you!

    You can delete a number of rows from your DataTable, after reading your Excel.
    You should really know how many rows are garbage, then you can work with your DataTable.
    I think there is no further filtering in ExcelDataReader, other than "IsFirstRowAsColumnNames".


    Author Comment

    ya I may have to just delete from the datatable at that point.  just need to make sure I dont have too many columns etc. without the header.
    LVL 13

    Expert Comment

    Is the problem in the columns or in the rows?

    Author Closing Comment


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the reā€¦
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now