Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在excel中将值从标签放到列中的简短或简单解决方案_C#_Excel_Winforms_Excel Interop - Fatal编程技术网

C# 在excel中将值从标签放到列中的简短或简单解决方案

C# 在excel中将值从标签放到列中的简短或简单解决方案,c#,excel,winforms,excel-interop,C#,Excel,Winforms,Excel Interop,您好,任何人都可以帮我解决这个问题,我这里有一个代码,可以计算从B列到AF列的水果数量(31天) 我使用的开关将是情况1到情况31。我希望我的代码是简单的或循环的。31号病例太长了。顺便说一下,我的代码工作,我只是希望它是简短的 这是我的密码 private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application OfficeExcel;

您好,任何人都可以帮我解决这个问题,我这里有一个代码,可以计算从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 ;

将日值放入变量,并将其用作索引器。

我认为您可以再次使用开关,对吗?如果我错了,你能告诉我怎么做吗。我很感谢你的帮助。你不再需要开关了。