附加到excel,但两次发布相同的行。 C#

时间:2017-10-21 11:22:30

标签: c# excel duplicates

我正在追加excel,但是当我发帖时,我发现同一行已经发布了两次。所以我有2行。当我再次发布时,它会离开我的第一行,覆盖第二行,但然后再次给我一个副本作为第3行。

总而言之,我可以发布我需要的所有内容,我只剩下1个重复的行。 我似乎无法纠正它

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


namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
        private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
        private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
        private static Microsoft.Office.Interop.Excel.Application oXL;

        private void button1_Click(object sender, EventArgs e)
        {
            string Combo1 = comboBox1.SelectedItem.ToString();
            string path = @"C:\Users\Staff\Desktop\RIT\worksheet.xls";
            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
            oXL.DisplayAlerts = false;
            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false,             
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0,       
   true, false, false);

            mWorkSheets = mWorkBook.Worksheets;

            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
            Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

            //I thought issue maybe here.. 

            int colCount = range.Columns.Count;
            int rowCount = range.Rows.Count;
            for (int index = 1; index < 3; index++) 
            {
                mWSheet1.Cells[rowCount + index, 1] = rowCount + index;
                mWSheet1.Cells[rowCount + index, 1] = Combo1 + index;
            }

            mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
               Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value);
            mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
            mWSheet1 = null;
            mWorkBook = null;
            oXL.Quit();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}      

2 个答案:

答案 0 :(得分:0)

可以立即分配所有值:

object[,] values = { { rowCount + 1, Combo1 + 1 }, { rowCount + 2, Combo1 + 2 } };

mWSheet1.Cells.Resize[2, 2].Offset[rowCount].Value2 = values;

答案 1 :(得分:0)

你必须使用

for (int index = 2; index > 0; --index)
{
    //...
}

而不是for循环,因为您将在循环的下一次迭代中处理新的插入/附加行。因此,该行被插入两次,并且在下次循环开始时将被覆盖(如您所述)。

当从数组中删除某些东西时,你也会使用这种循环(我称之为从高到低的循环):你会发生以下情况:

  • 你有一个包含item0,item1和item2
  • 的数组
  • 您开始从索引0
  • 处理数组
  • 如果条件通过(让它在索引0处执行),则从数组中删除item0
  • 然后你增加了计数器。

发生了什么事?

  • 你刚刚删除了item0

一切都很好,直到这里:

  • 您现在将index1设为索引0
  • 您现在在索引1处有item2
  • 你的柜台已经是索引1

所以你跳过了处理中的item1。

在反之亦然的情况下,你也发生了同样的事情:你已经添加了一行并将计数器增加了一个,这使得你在新插入/添加的行中成为现实。