在Excel中循环循环

时间:2013-11-08 10:55:47

标签: c# excel

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

像使用数据表一样循环

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 { }
}

2 个答案:

答案 0 :(得分:3)

我的不好,我把你的代码块混淆了。

假设您有一个名为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 
    } 
    /*
}

答案 1 :(得分:1)

继续我的评论

  

当您遍历某个范围时,它始终从左向右循环而不是向上循环(除非该范围只有一列)

假设你的excel表看起来像这样

enter image description here

已经过测试

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

这就是你得到的

enter image description here

相关问题