简体   繁体   English

如何将excel文件一列的所有内容读入数组C#? 不使用 ADO.net 方法

[英]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#?如何从 excel 文件中的一维数组和 C# 中读取列的所有内容? Not using ADO.net approach or excel reader.不使用 ADO.net 方法或 excel 读卡器。 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 excel 文件中也可以有任意数量的行(如何确定列中的行数)..某些单元格在列中可能有空白值...需要使用 Microsoft.Office.Interop.Excel 的最基本方法

If you are using Office 2007 or better, check out OpenXML.如果您使用的是 Office 2007 或更高版本,请查看 OpenXML。 Useful for reading and constructing Word, Excel and PowerPoint documents用于阅读和构建 Word、Excel 和 PowerPoint 文档

http://msdn.microsoft.com/en-us/library/bb448854.aspx 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许多第 3 方组件供应商拥有 Excel 解决方案,允许您加载和创建 Excel 文件,而无需安装 Excel,因为我自己使用了这样的解决方案很好

Download Excel DataReader This approach is without using ADO.net approach下载 Excel DataReader这种方法是没有使用 ADO.net 的方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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