简体   繁体   中英

How to read all contents of a column of excel file into an array C#? Without using ADO.net approach

How to read all contents of a column from excel file in 1-d array and in C#? Not using ADO.net approach or excel reader. Also there can be any number of rows in the excel file(how to determine number of rows in the column)..some cells may have blank value in the column...need the most basic approach using Microsoft.Office.Interop.Excel

If you are using Office 2007 or better, check out OpenXML. Useful for reading and constructing Word, Excel and PowerPoint documents

http://msdn.microsoft.com/en-us/library/bb448854.aspx

A number of 3rd party component vendors have Excel solutions that allow you to load and create Excel files without having to have Excel installed on the system -- I know for a fact Infragistics has such a solution since I have used it myself and it works pretty nicely

Download Excel DataReader This approach is without using ADO.net approach

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

//5. Data Reader methods
while (excelReader.Read())
{
    //excelReader.GetInt32(0);
}

//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM