在一次迭代后打破一个while循环

时间:2018-01-06 01:26:32

标签: c# excel

我是初学程序员,我正在努力提高我的编程技巧。我的最终目标是为医疗账单公司建立一个数据管理系统。

我在学习编程语言的最初阶段,我的第一个小小的任务是用C#创建一个基本的Windows应用程序,打开一个excel文件,检查最后一行有数据的文件,从它下来一行,并添加文本框中的数据。添加一行数据后,我希望它停止并保存文件(如只添加一行数据)

到目前为止,我已经能够完美地完成所有事情......除了一件事。我使用while循环检查第一个单元格是否为空:

while(oSheet.Cells[inc,1] !=null)

使用inc++;oSheet.Cells[inc,X] = "text";行,我已经让我的程序自动下拉到下一行并添加数据但它并没有停止!它一直在无限进行!我最好的想法告诉我要包含一个if语句,声明如果递增的单元格不是空的,那么要打破while循环。出于某种原因,我无法理解,当我包含if if break语句时,代码永远不会执行。我认为如果if语句低于inc ++;和单元格填充语句,while循环将检查第一个单元格,如果它不是null它将下降到下一个单元格,如果单元格为null然后它将添加文本然后运行if语句看到它只是单元格添加的文本实际上不是null,它会破坏while循环。这似乎不会发生。当我包含if break语句时,没有任何反应。以下是代码的两个版本:

没有if break语句:

    private void btnCreateClaim_Click(object sender, EventArgs e)
    {
        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;

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

        oWB = oXL.Application.Workbooks.Open(@"C:\CSAIO4D\testsheet1.xlsx");
        oSheet = oWB.ActiveSheet;




        int inc = 1;
        while(oSheet.Cells[inc,1] != null)
        {
            inc++;

                oSheet.Cells[inc, 1] = txtClientName.Text.ToString();
                oSheet.Cells[inc, 2] = txtState.Text.ToString();
                oSheet.Cells[inc, 3] = txtAlphaPrefix.Text.ToString();
                oSheet.Cells[inc, 4] = txtInsurance.Text.ToString();
                oSheet.Cells[inc, 5] = txtStartDate.Text.ToString();
                oSheet.Cells[inc, 6] = txtEndDate.Text.ToString();
                oSheet.Cells[inc, 7] = txtUnits.Text.ToString();
                oSheet.Cells[inc, 8] = txtLOC.Text.ToString();
                oSheet.Cells[inc, 9] = txtRate.Text.ToString();
                oSheet.Cells[inc, 10] = txtAmount.Text.ToString();
                oSheet.Cells[inc, 11] = txtAuth.Text.ToString();
                oSheet.Cells[inc, 12] = txtBilledDate.Text.ToString();
                oSheet.Cells[inc, 13] = txtPrimaryDiagnosis.Text.ToString();
                oSheet.Cells[inc, 14] = txtBillType.Text.ToString();
                oSheet.Cells[inc, 15] = txtRevenueCode.Text.ToString();
                oSheet.Cells[inc, 16] = txtHCPCS.Text.ToString();
                oSheet.Cells[inc, 17] = txtCPT_Code.Text.ToString();

        }

        oWB.Save();
    }

使用if break语句:

    private void btnCreateClaim_Click(object sender, EventArgs e)
    {
        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;

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

        oWB = oXL.Application.Workbooks.Open(@"C:\CSAIO4D\testsheet1.xlsx");
        oSheet = oWB.ActiveSheet;




        int inc = 1;
        while(oSheet.Cells[inc,1] != null)
        {
            inc++;

                oSheet.Cells[inc, 1] = txtClientName.Text.ToString();
                oSheet.Cells[inc, 2] = txtState.Text.ToString();
                oSheet.Cells[inc, 3] = txtAlphaPrefix.Text.ToString();
                oSheet.Cells[inc, 4] = txtInsurance.Text.ToString();
                oSheet.Cells[inc, 5] = txtStartDate.Text.ToString();
                oSheet.Cells[inc, 6] = txtEndDate.Text.ToString();
                oSheet.Cells[inc, 7] = txtUnits.Text.ToString();
                oSheet.Cells[inc, 8] = txtLOC.Text.ToString();
                oSheet.Cells[inc, 9] = txtRate.Text.ToString();
                oSheet.Cells[inc, 10] = txtAmount.Text.ToString();
                oSheet.Cells[inc, 11] = txtAuth.Text.ToString();
                oSheet.Cells[inc, 12] = txtBilledDate.Text.ToString();
                oSheet.Cells[inc, 13] = txtPrimaryDiagnosis.Text.ToString();
                oSheet.Cells[inc, 14] = txtBillType.Text.ToString();
                oSheet.Cells[inc, 15] = txtRevenueCode.Text.ToString();
                oSheet.Cells[inc, 16] = txtHCPCS.Text.ToString();
                oSheet.Cells[inc, 17] = txtCPT_Code.Text.ToString();

            if(oSheet.Cells[inc,1] != null)
            {
                break;
            }

        }

        oWB.Save();
    }

有人可以向我解释我需要添加/做什么不同让它继续下去,直到它找到第一列中的第一个空单元格,一旦它发生,在文本框中发布信息然后在发布后停止它一次?

3 个答案:

答案 0 :(得分:2)

如果是这样的话:

while(oSheet.Cells[inc,1] != null)
        ^^^^^^^^^^^^^^^^^^^^^^

这意味着循环应该继续。但是加上这个:

        if(oSheet.Cells[inc,1] != null)
        {
            break;
        }
如果相同的条件成立,你会告诉它退出循环。所以它会在检查完第一行后退出,正如您所见。

另外,检查 - oSheet.Cells[inc,1] != null是否正确确定单元格是否为空?我不这么认为。我认为oSheet.Cells[inc, 1]总是会返回一个Range对象。单元格可能是空的,也可能不是空的,但它永远不会为空。

试试这个:

while(oSheet.Cells[inc,1].Text != string.Empty)

您也可以使用

oSheet.UsedRange.Rows.Count

获取不为空的行数。像这样 - 不需要循环。

var emptyRow = oSheet.UsedRange.Rows.Count + 1;
oSheet.Cells[emptyRow, 1] = txtClientName.Text.ToString();
oSheet.Cells[emptyRow, 2] = txtState.Text.ToString();

答案 1 :(得分:1)

您是否只需要在循环外移动代码设置值? e.g。

private void btnCreateClaim_Click(object sender, EventArgs e)
{
    Excel.Application oXL;
    Excel._Workbook oWB;
    Excel._Worksheet oSheet;

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

    oWB = oXL.Application.Workbooks.Open(@"C:\CSAIO4D\testsheet1.xlsx");
    oSheet = oWB.ActiveSheet;

    int inc = 1;
    while(oSheet.Cells[inc,1].Text != String.Empty)
    {
        inc++;
    }

    oSheet.Cells[inc, 1] = txtClientName.Text.ToString();
    oSheet.Cells[inc, 2] = txtState.Text.ToString();
    // etc...

    oWB.Save();
}

答案 2 :(得分:0)

当逻辑被分解为可管理的部分时,程序总是更容易管理。在这种情况下,我建议你编写一个单独的函数来查找下一个空白行。它可能看起来像这样:

    private int FindFirstBlankRow(Excel._Worksheet oSheet)
    {
        int y = 1;
        while (oSheet.Cells[y, 1] != null) y++;
        return y;
    }

然后在你的主要功能中它应该很简单:

    private void btnCreateClaim_Click(object sender, EventArgs e)
    {
        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;

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

        oWB = oXL.Application.Workbooks.Open(@"C:\CSAIO4D\testsheet1.xlsx");
        oSheet = oWB.ActiveSheet;

        int inc = FindFirstBlankRow(oSheet);

        oSheet.Cells[inc, 1] = txtClientName.Text.ToString();
        oSheet.Cells[inc, 2] = txtState.Text.ToString();
        oSheet.Cells[inc, 3] = txtAlphaPrefix.Text.ToString();
        oSheet.Cells[inc, 4] = txtInsurance.Text.ToString();
        oSheet.Cells[inc, 5] = txtStartDate.Text.ToString();
        oSheet.Cells[inc, 6] = txtEndDate.Text.ToString();
        oSheet.Cells[inc, 7] = txtUnits.Text.ToString();
        oSheet.Cells[inc, 8] = txtLOC.Text.ToString();
        oSheet.Cells[inc, 9] = txtRate.Text.ToString();
        oSheet.Cells[inc, 10] = txtAmount.Text.ToString();
        oSheet.Cells[inc, 11] = txtAuth.Text.ToString();
        oSheet.Cells[inc, 12] = txtBilledDate.Text.ToString();
        oSheet.Cells[inc, 13] = txtPrimaryDiagnosis.Text.ToString();
        oSheet.Cells[inc, 14] = txtBillType.Text.ToString();
        oSheet.Cells[inc, 15] = txtRevenueCode.Text.ToString();
        oSheet.Cells[inc, 16] = txtHCPCS.Text.ToString();
        oSheet.Cells[inc, 17] = txtCPT_Code.Text.ToString();

        oWB.Save();
    }