简单或简单的解决scheme,用于在excel中将标签和列的值相加

美好的一天,任何人都可以帮助我解决这个问题,我有一个代码在这里,从列B到列AF(31天)的水果的数目我用开关,情况1,情况31.我希望我的代码是简单或者在一个循环中。 31案件太长了。 顺便说一句,我的代码工作,我只是希望它是短暂的。

这是我的代码

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; var dtpMonth = dateTimePicker1.Value.ToString("MMMM"); var dtpYear = dateTimePicker1.Value.Year; var MonthYear = dtpMonth + " - " + dtpYear; var dtpDay = dateTimePicker1.Value.Day; try { OfficeExcel = new Microsoft.Office.Interop.Excel.Application(); OfficeExcel.Visible = true; int appletotal = Convert.ToInt32(lblappletotal.Text); int bananatotal = Convert.ToInt32(lblbananatotal.Text); int orangetotal = Convert.ToInt32(lblorangetotal.Text); int grapestotal = Convert.ToInt32(lblgrapestotal.Text); switch (dateTimePicker1.Value.Day.ToString()) { case "1": OfficeWorkBook = (Microsoft.Office.Interop.Excel._Workbook)(OfficeExcel.Workbooks.Add("")); OfficeSheet = (Microsoft.Office.Interop.Excel._Worksheet)OfficeWorkBook.ActiveSheet; OfficeSheet.Cells[3,1] = "apple"; OfficeSheet.Cells[4,1] = "banana"; OfficeSheet.Cells[5,1] = "orange"; OfficeSheet.Cells[6,1] = "grapes"; OfficeSheet.Cells[2, 2] = dtpDay + dtpMonth ; OfficeSheet.Cells[3, 2] = appletotal; // variable OfficeSheet.Cells[4, 2] = bananatotal; OfficeSheet.Cells[5, 2] = orangetotal; OfficeSheet.Cells[6, 2] = grapestotal; OfficeExcel.Visible = true; OfficeWorkBook.SaveAs("D:\\fruits\\" + MonthYear + ".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); break; case "2": OfficeWorkBook = OfficeExcel.Workbooks.Open("D:\\fruits\\" + MonthYear + ".xls"); OfficeSheet = (Excel.Worksheet)OfficeWorkBook.Worksheets.get_Item(1); OfficeSheet.Cells[2, 3] = dtpDay + dtpMonth; OfficeSheet.Cells[3, 3] = appletotal; OfficeSheet.Cells[4, 3] = bananatotal; OfficeSheet.Cells[5, 3] = orangetotal; OfficeSheet.Cells[6, 3] = grapestotal; OfficeExcel.Visible = true; OfficeWorkBook.Save(); break; . . . so On..... } 

谢谢

 int DayCell = dateTimePicker1.Value.Day+1; OfficeSheet.Cells[2, Daycell] = dtpDay + dtpMonth ; 

将date值放入一个variables中,并将其用作索引器。