将标签中的数据连续保存到Excel中的列

时间:2015-10-30 15:15:07

标签: c# excel winforms excel-interop

我在标签中保存数据时遇到了麻烦。我保存时总是会覆盖它。将数据保存到B列是可以的,但是当它转到C列时,它会覆盖它。 B列中的数据将被删除。只保存C列。

private void button1_Click(object sender, EventArgs e)
    {

        Microsoft.Office.Interop.Excel.Application OfficeExcel;
        Microsoft.Office.Interop.Excel._Workbook OfficeWorkBook;
        Microsoft.Office.Interop.Excel._Worksheet OfficeSheet;

        try
        {
            //Start Excel and get Application object.
            OfficeExcel = new Microsoft.Office.Interop.Excel.Application();
            OfficeExcel.Visible = true;

            //Get a new workbook.
            OfficeWorkBook = (Microsoft.Office.Interop.Excel._Workbook)(OfficeExcel.Workbooks.Add(""));
            OfficeSheet = (Microsoft.Office.Interop.Excel._Worksheet)OfficeWorkBook.ActiveSheet;


            int apple = 10, banana = 11, grapes = 12, orange = 5;
            int apple2 = 20, banana2 = 21, grapes2 = 22, orange2 = 15;


            switch (cmbDays.SelectedItem.ToString())
            {
                case "1":
                    lbltotalapple.Text = apple.ToString();
                    lbltotalbanana.Text = banana.ToString();
                    lbltotalgrapes.Text = grapes.ToString();
                    lbltotalorange.Text = orange.ToString();
                    //Add table headers going cell by cell.

                    OfficeSheet.Cells[3, 2] = lbltotalapple.Text;
                    OfficeSheet.Cells[4, 2] = lbltotalbanana.Text;
                    OfficeSheet.Cells[5, 2] = lbltotalgrapes.Text;
                    OfficeSheet.Cells[6, 2] = lbltotalorange.Text;

                    OfficeExcel.Visible = false;
                    OfficeExcel.UserControl = false;

                    // I think is the problem. How do I make it continously save. Not overwriting it.
                    OfficeWorkBook.SaveAs("D:\\test\\test1.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing,
                                false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    OfficeWorkBook.Close();

                    break;

                case "2":
                    lbltotalapple.Text = apple2.ToString();
                    lbltotalbanana.Text = banana2.ToString();
                    lbltotalgrapes.Text = grapes2.ToString();
                    lbltotalorange.Text = orange2.ToString();

                    OfficeSheet.Cells[3, 3] = lbltotalapple.Text;
                    OfficeSheet.Cells[4, 3] = lbltotalbanana.Text;
                    OfficeSheet.Cells[5, 3] = lbltotalgrapes.Text;
                    OfficeSheet.Cells[6, 3] = lbltotalorange.Text;

                    OfficeExcel.Visible = false;
                    OfficeExcel.UserControl = false;

                    // I think is the problem. How do I make it continously save. Not overwriting it.
                    OfficeWorkBook.SaveAs("D:\\test\\test1.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Type.Missing, Type.Missing,
                                false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    OfficeWorkBook.Close();

                    break;
            }

        }
        catch (Exception ex)
        {
                   // do something
        }


    }

如何连续保存从B列到C列等等?

0 个答案:

没有答案