tags:

views:

407

answers:

5

Hi.

I have an Excel file with one column which is filled with numbers. I would like to be read the numbers from this column into an array in C#. How can I do that?

Thank you all.

+1  A: 

The easiest way is probably to use the Excel ODBC driver. This allows you to use OdbcConnection to read the worksheet into a DataTable. You can then iterate over the table's Rows collection, copying the values into a list or array.

itowlson
I was thinking about working with files. and read the data in file's rows. I worked with .txt file befor but I forget how I used it.
LIX
@LIX: Its relatively easy to read plain text files, but the Excel file formats are not plain text and require a special reader to read them.
Zach Johnson
@itowlson : In view of the question and now the Text remark, could it be an illustration of the fact that: a) Yes! the SO community needs to be culturally aware and work at smoothing out communication hurdles b) Even when paused by colleagues with the least command of the English language, good or decent question don't look that bad c) Not everyone can [should?] be helped. What do you think ?
mjv
thanks for your kindly comments. can I do something in my Ecxel file? for example convert a column of data to 1 cell that data are seprated with ","?
LIX
@LIX: I'm not sure what you are exactly trying to do. If you want to put all the values from a column of data into one cell, each separated by a comma, then you will want to first read the values from the column, then create a string containing the formatted values, and then put the data in the cell. Have you chosen a particular method of Excel Interop? (ODBC, Excel Interop Libraries, etc)
Zach Johnson
@mjv: "What do you think?" I think: too many people failed to get to (a); (b) is over-optimistic (idioms and tone differ greatly between cultures); and (c) may be true but is irrelevant. Until Jason fixed the question, nobody even *tried* to elicit the technical issue: they just piled onto the phrasing. At least now we are offering technical solutions on the merits: whether the OP can make use of them remains to be seen, but that is an educational matter, not a matter for personal abuse.
itowlson
@itowlson: fair enough. I generally think, and act, alike; although I sometimes wish we had a better bozo filter (maybe a hint that's I need to take a vacation from SO ;-) ). Thank you for your kindness.
mjv
@LIX: Well you could convert your excel file into a CSV file format which would you would be parse as a normal file.
Albinoswordfish
A: 

This link might help you. You can adapt the answers to convert the values to integers.

Zach Johnson
A: 

You can use Excel Interop and do something along the lines of:

Excel.Range firstCell = excelWorksheet.get_Range("A1", Type.Missing);
Excel.Range lastCell = excelWorksheet.get_Range("A10", Type.Missing);
Excel.Range worksheetCells = excelWorksheet.get_Range(firstCell, lastCell);
var cellValues = worksheetCells.Value2;

You should get an array of objects (1-based index), and can cast the contents using (for instance) Convert.ToDouble().

Mathias
A: 

SpreadsheetGear for .NET can do it. Below is some C# source. Note that the SpreadsheetGear API is similar to the Excel API, so the code below could be adapted to Excel.

using System;
using SpreadsheetGear;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            // Load a workbook from disk and get the first worksheet.
            var workbook = SpreadsheetGear.Factory.GetWorkbook(@"C:\tmp\Numbers.xlsx");
            // Allocate a list of doubles to store the number.
            var numbers = new System.Collections.Generic.List<double>();
            var worksheet = workbook.Worksheets[0];
            // Assuming that column A is the column with the numbers...
            var columnA = worksheet.Cells["A:A"];
            var usedRange = worksheet.UsedRange;
            // Limit the cells we look at to the used range of the sheet.
            var numberCells = usedRange.Intersect(columnA);
            // Get the numbers into the list.
            foreach (IRange cell in numberCells)
            {
                object val = cell.Value;
                if (val is double)
                    numbers.Add((double)val);
            }
            // Write the numbers to the console.
            foreach (double number in numbers)
                Console.WriteLine("number={0}", number);
        }
    }
}

You can download the free SpreadsheetGear trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
A: 

Take a look at this: Create Excel (.XLS and .XLSX) file from C#

The open source projects listed on the link above allow you to read/write Excel files.

Leniel Macaferi