Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel中的行循环_C#_Excel - Fatal编程技术网

C# Excel中的行循环

C# Excel中的行循环,c#,excel,C#,Excel,如何使用c#在excel中循环一行 像使用datatable一样循环 foreach(DataRow _dr in datatable.row) { //data } 我正在试着得到这个。但它每列循环一次 foreach (Excel.Range r in usedRange) { // check condition: try { if (Convert.ToInt32(r.Value2.ToString()) == 0) { /

如何使用c#在excel中循环一行

像使用datatable一样循环

foreach(DataRow _dr in datatable.row)
{
  //data
}
我正在试着得到这个。但它每列循环一次

foreach (Excel.Range r in usedRange)
{
   // check condition:
   try
   {
      if (Convert.ToInt32(r.Value2.ToString()) == 0)
      {
        // if match, delete and shift remaining cells up:
         r.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
        break;
      }
    }
    catch { }
}

我的错,我把你的代码块搞混了

假设您有一个名为
sheet
的工作表,请尝试以下操作:

foreach (var row in sheet.UsedRange.Rows)
{
    // either put your logic here, 
    // or look at columns if you prefer 
    /*
    foreach (var cell in row.Columns)
    {
       // do something with cells 
    } 
    /*
}

我的错,我把你的代码块搞混了

假设您有一个名为
sheet
的工作表,请尝试以下操作:

foreach (var row in sheet.UsedRange.Rows)
{
    // either put your logic here, 
    // or look at columns if you prefer 
    /*
    foreach (var cell in row.Columns)
    {
       // do something with cells 
    } 
    /*
}
继我的评论之后


在范围中循环时,它总是从左到右循环,而不是从上到下循环(除非该范围只有一列)

假设您的excel工作表如下所示

经过尝试和测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

Namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            Microsoft.Office.Interop.Excel.Range xlRange;

            object misValue = System.Reflection.Missing.Value;

            xlexcel = new Excel.Application();
            xlexcel.Visible = true;

            // Open a File
            xlWorkBook = xlexcel.Workbooks.Open("C:\\Book1.xlsx", 0, true, 5, "", "", true,
            Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            // Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlRange = xlWorkSheet.UsedRange;

            for (int i = 1; i <= xlRange.Rows.Count; i++)
            {
                for (int j = 1; j <=  xlRange.Columns.Count; j++)
                {
                    if (xlexcel.WorksheetFunction.CountIf(xlRange.Cells[i, j], "0") > 0)
                    {
                        MessageBox.Show("Row " + i + " has 0");
                        break;
                    }
                }
            }

            //Once done close and quit Excel
            xlWorkBook.Close(false, misValue, misValue);
            xlexcel.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlexcel);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Excel=Microsoft.Office.Interop.Excel;
命名空间Windows窗体应用程序4
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.工作簿;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel.Range xlRange;
对象错误值=System.Reflection.Missing.Value;
xlexcel=新的Excel.Application();
xlexcel.Visible=true;
//打开一个文件
xlWorkBook=xlexcel.Workbooks.Open(“C:\\Book1.xlsx”,0,true,5,“,”,true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);
//将图纸1设置为要使用的图纸
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlRange=xlWorkSheet.UsedRange;
对于(inti=1;i,请参阅我的评论


在范围中循环时,它总是从左到右循环,而不是从上到下循环(除非该范围只有一列)

假设您的excel工作表如下所示

经过尝试和测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

Namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            Microsoft.Office.Interop.Excel.Range xlRange;

            object misValue = System.Reflection.Missing.Value;

            xlexcel = new Excel.Application();
            xlexcel.Visible = true;

            // Open a File
            xlWorkBook = xlexcel.Workbooks.Open("C:\\Book1.xlsx", 0, true, 5, "", "", true,
            Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            // Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlRange = xlWorkSheet.UsedRange;

            for (int i = 1; i <= xlRange.Rows.Count; i++)
            {
                for (int j = 1; j <=  xlRange.Columns.Count; j++)
                {
                    if (xlexcel.WorksheetFunction.CountIf(xlRange.Cells[i, j], "0") > 0)
                    {
                        MessageBox.Show("Row " + i + " has 0");
                        break;
                    }
                }
            }

            //Once done close and quit Excel
            xlWorkBook.Close(false, misValue, misValue);
            xlexcel.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlexcel);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用Excel=Microsoft.Office.Interop.Excel;
命名空间Windows窗体应用程序4
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.工作簿;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel.Range xlRange;
对象错误值=System.Reflection.Missing.Value;
xlexcel=新的Excel.Application();
xlexcel.Visible=true;
//打开一个文件
xlWorkBook=xlexcel.Workbooks.Open(“C:\\Book1.xlsx”,0,true,5,“,”,true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);
//将图纸1设置为要使用的图纸
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlRange=xlWorkSheet.UsedRange;

for(int i=1;i当您在一个范围内循环时,它总是从左到右循环,而不是从上到下循环(除非该范围只有一列).你到底想做什么?我正试图删除一个包含zeroGimme 10 m in的行…更新C#code OK我现在正在测试它当你在一个范围内循环时,它总是从左到右循环,而不是从上到下循环(除非该范围只有一列).你到底想做什么?我正试图删除一个包含zeroGimme 10 m in的行…更新C#code OK我只是在测试它now@user2729205:要删除行,必须从下到上删除行,否则代码将跳过行。对于(int i=1;i=1;i+=-1)的更改
并使用
xlsheet.rows[i].Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
删除行。如果范围位置未知,该怎么办?
foreach
可能对我有用,但我需要知道信息来自范围中的哪个列索引。因此,与第二列一样,可能在G列中,但我仍然知道它是第二列,因此有些value@user2729205:去删除行,您必须从下到上删除行,否则代码将跳过行。对于(int i=1;i=1;i+=-1)的
更改,请使用
xlsheet.rows[i].delete(Excel.XlDeleteShiftDirection.xlShiftUp)删除行。范围位置未知的情况如何?
foreach
可能对我有用,但我需要知道信息来自范围中的哪个列索引。因此,与第二列一样,可能在G列中,但我仍然知道它是第二列,因此是某个值