如何在不使用C#覆盖的情况下附加到现有Excel文件?

时间:2018-06-27 11:23:48

标签: c# .net visual-studio visual-studio-2010 visual-studio-2012

private void newsubmit_Click(object sender, EventArgs e)
{
    int y;
    string[] range;
    range = new string[999];

    if ( newid.Text != null)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Administrator\Desktop\coach ceazar\CLIENTS", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        object misValue = System.Reflection.Missing.Value;

        for (y = 1; y < xlWorkSheet.UsedRange.Columns.Count; y++)
        {
            xlWorkSheet.Cells[y][newid.Text].copy();
            range[y] = Clipboard.GetText();
        }

        xlApp.UserControl = false;
        xlWorkBook.Close(false);

        xlWorkBook =  xlApp.Workbooks.Open(@"C:\Users\Administrator\Desktop\coach ceazar\classes\" + classes[classnumber-1].Text+".xls", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        for (int w = 1; w < y; w++)
        {
            xlWorkSheet.Cells[w][xlWorkSheet.UsedRange.Rows.Count+1]=range[w];
        }

        xlApp.UserControl = false;
        xlWorkBook.Save();
        xlApp.Visible = true; 
    }
    else MessageBox.Show("please enter the id to be enrolled!");
}

我正在尝试使用C#将整个行从一个Excel文件复制到另一个Excel文件。这是我当前的代码,但是每次运行代码时,它都会覆盖Excel文件中已经存在的内容。这真的让我感到沮丧-有人可以帮忙吗?

0 个答案:

没有答案