C#读取excel某列数据

时间:2020-07-01 11:05:33   收藏:0   阅读:19

public List ReadeCFGNameFromExcel(string ExcelName)
{
List ColumnDB = new List();
//创建 Excel对象
Microsoft.Office.Interop.Excel.Application App = new Microsoft.Office.Interop.Excel.Application();
//获取缺少的object类型值
object missing = Missing.Value;
//打开指定的Excel文件
Microsoft.Office.Interop.Excel.Workbook openwb = App.Workbooks.Open(ExcelName, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//获取选选择的工作表
Microsoft.Office.Interop.Excel.Worksheet ws = ((Microsoft.Office.Interop.Excel.Worksheet)openwb.Worksheets["Sheet1"]);//方法一:指定工作表名称读取
//Worksheet ws = (Worksheet)openwb.Worksheets.get_Item(1);
//获取工作表中的行数
int rows = ws.UsedRange.Rows.Count;
//获取工作表中的列数
int columns = ws.UsedRange.Columns.Count;
Console.WriteLine("请输入你要获取哪列数据");
int column = Convert.ToInt16(1);
//提取对应行列的数据并将其存入数组中
for (int i = 2; i < rows; i++)
{
string aName = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, column]).Text.ToString();
Console.WriteLine("读取的数据:" + aName);//测试是否获得数据
string[] fileStr = aName.Split(‘.‘);
if (fileStr.Length>1)
{
if (fileStr[1]== "xml")
{
ColumnDB.Add(aName);
}
}
}
return ColumnDB;
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!