C#查找列是否包含“N”。如果是,则删除行 [英] C# find if column contains a "N" if so then delete row

查看:110
本文介绍了C#查找列是否包含“N”。如果是,则删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个excel文件,并且每列都有一个Y或N的列,如果它包含N但是我想删除该行,但是要对条件进行检查以查看它。



我的尝试:



<预> Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 
Microsoft.Office.Interop.Excel.Workbook sheet = excel.Workbooks.Open(C:\\Data\\Enrolled.xlsx);

Microsoft.Office.Interop.Excel.Worksheet x = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
int i = 0;
for(i = 1; i< = lastRow; i ++)
{
//条件需要检查列行是否有N
if(excel.WorksheetFunction )
(x.Rows [i] as Microsoft.Office.Interop.Excel.Range).Delete();

}


sheet.Close(true,Type.Missing,Type.Missing);
excel.Quit();

解决方案

在使用工作表时可以使用范围指向某个单元格并调查< a href =https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.value.aspx> Range.Value属性(Microsoft.Office.Interop.Excel) [ ^ ]检查价值。



类似

 ... 
if (x.Range [ A + i] .Value == N){
...


Hello I have an excel file, and there is a column that has either "Y" or "N" for each row and I would like to delete the row if it contains "N" but struggling with the conditional to check it.

What I have tried:

<pre> Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook sheet = excel.Workbooks.Open("C:\\Data\\Enrolled.xlsx");

            Microsoft.Office.Interop.Excel.Worksheet x = excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
            int i = 0;
            for (i = 1; i <= lastRow; i++)
            {
               // Conditional Needed to check if column row has a "N"
               if (excel.WorksheetFunction)
                    (x.Rows[i] as Microsoft.Office.Interop.Excel.Range).Delete();

            }


            sheet.Close(true, Type.Missing, Type.Missing);
            excel.Quit();

解决方案

You can use range to point a certain cell when loping the worksheet and investigate Range.Value property (Microsoft.Office.Interop.Excel)[^] to check the value.

Something like

...
if (x.Range["A" + i].Value == "N") {
...


这篇关于C#查找列是否包含“N”。如果是,则删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆