topofsteel
topofsteel

Reputation: 1277

Interop.Excel - reading data types

I'm reading an excel file into c# and I only need the Rows that begin with a number. All of the cells are formatted as General. I tried testing to see if the value in the first column was a Double, and it worked. But some of the rows begin with '1' and some have a decimal place '1.1'. How can I sort the whole number from the decimals? Treat them as a string and test for '.'? My formatting options in excel seem to be either a the cell is a number, or not. I cant specify int, double or anything. How can I accomplish this?

Upvotes: 0

Views: 1179

Answers (2)

topofsteel
topofsteel

Reputation: 1277

This worked for my purpose. I am able to read only the rows that begin with a number and filter the whole numbers from the decimals (whole numbers shown). Thank you for your help.

        Worksheet sheet = (Worksheet)workbook.Sheets[1];
        Range excelRange = sheet.UsedRange; 
        object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

        for (int i = 1; i <= excelRange.Rows.Count; i++)
        {
            if (valueArray[i, 1] is Double)
            {                   
                if (!valueArray[i, 1].ToString().Contains("."))
                {
                    sb.AppendLine(valueArray[i, 1].ToString()); 
                }
            }
        }

Upvotes: 0

HuRN
HuRN

Reputation: 71

If you are using only Excel 2007/2010 (*.xlsx) files then you can use official Microsoft assembly (Open XML SDK) which have many powerfull futures (including row filtering by any condition): http://www.microsoft.com/en-us/download/details.aspx?id=5124

Upvotes: 1

Related Questions