怎么用C#读取指定文件夹下的Excel文件中指定的列的内容,而且还需要遍历这个excel文件下的所以sheet? 40
展开全部
List<string> list1=new List<string>();
string[] str=Directory.Getfiles(@"文件夹路径");
if(str.length==0)
return ;
foreach(int i in str)
{
if(i.SubString(i.LastIndexof('.')-1)=="xlsx"||i.SubString(i.LastIndexof('.')-1)=="xls")
{
//new一个Excel进程
Excel.Application app = new Excel.Application();
//打开Excel文件
Excel.Workbook book1 = app.Workbooks.Open(i, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
for(int j=1;j<=book1.Worksheets.Count;j++)
{
Excel.Worksheet sheet1 = (Excel.Worksheet)book1.Worksheets[j];
for(int k=1;k>0;k++)
{
//m为指定列列号
//如果读取结束,就跳出循环
if(sheet1.Cells[k,m].ToString()==string.Empty)
break;
list1.add(sheet1.Cells[k,m]);
}
}
}
}
string[] str=Directory.Getfiles(@"文件夹路径");
if(str.length==0)
return ;
foreach(int i in str)
{
if(i.SubString(i.LastIndexof('.')-1)=="xlsx"||i.SubString(i.LastIndexof('.')-1)=="xls")
{
//new一个Excel进程
Excel.Application app = new Excel.Application();
//打开Excel文件
Excel.Workbook book1 = app.Workbooks.Open(i, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
for(int j=1;j<=book1.Worksheets.Count;j++)
{
Excel.Worksheet sheet1 = (Excel.Worksheet)book1.Worksheets[j];
for(int k=1;k>0;k++)
{
//m为指定列列号
//如果读取结束,就跳出循环
if(sheet1.Cells[k,m].ToString()==string.Empty)
break;
list1.add(sheet1.Cells[k,m]);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询