开发者

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 va开发者_开发问答lue in the column...need the most basic approach using Microsoft.Office.Interop.Excel


Is using the Excel Objects Library an option?

http://social.msdn.microsoft.com/Forums/en/vsto/thread/b6e8a28c-6760-4e86-a1aa-e2ce9ec36380


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜