C# 日期计算错误

C# 日期计算错误,c#,excel,winforms,date,C#,Excel,Winforms,Date,我有一个简单的代码,它将一个月的日期设置为Excel中的列中的(DD/MM/YYYY): DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form. for (int j = 7; j < daysInMonth + 7; j++

我有一个简单的代码,它将一个月的日期设置为Excel中的
列中的
(DD/MM/YYYY)

DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form.
for (int j = 7; j < daysInMonth + 7; j++)
{
        sheet.get_Range("A" + j).Value = date.ToShortDateString();
        date.AddDays(1.0);
}

问题在于,在for循环的每次迭代之后,都不会更新日期。我的意思是你这样做是错误的:

您应该使用如下内容:

date = date.AddDays(1.0);
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1);
for (int j = 0; j < daysInMonth; j++)
{
   // Skip increasing the first date
   if (j > 0) {
      date = date.AddDays(1.0);
   }

   // Set the correct date to the sheet
   sheet.get_Range("A" + (j + 7)).Value = date.ToString(@"dd/MM/yyyy");
}
更新:以上部分是您的代码中的一个小问题。主要问题在于Excel本身。当您将日期传递给它时,它会混淆它是月还是日,而不管您为它提供的格式如何

解决方法:Excel将日期存储为1900年1月1日之后的天数()。因此,您可以根据该格式设置单元格的值,然后将单元格的数字格式更改为通常设置为“DD/MM/YYYY”格式的格式

从日期获取整数值的函数:

private int convertDateTime(DateTime input)
{
    DateTime start = new DateTime(1900, 1, 1);
    return (int)(input - start).TotalDays + 2; //This is some calibration with the above mentioned Office article
}
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form.
for (int j = 7; j < daysInMonth + 7; j++)
{
    Range cell = sheet.get_Range("A" + j);
    cell.Value = convertDateTime(date);
    cell.NumberFormat = "DD/MM/YYYY";
    date = date.AddDays(1.0);
}
现在,您可以像这样使用代码:

private int convertDateTime(DateTime input)
{
    DateTime start = new DateTime(1900, 1, 1);
    return (int)(input - start).TotalDays + 2; //This is some calibration with the above mentioned Office article
}
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form.
for (int j = 7; j < daysInMonth + 7; j++)
{
    Range cell = sheet.get_Range("A" + j);
    cell.Value = convertDateTime(date);
    cell.NumberFormat = "DD/MM/YYYY";
    date = date.AddDays(1.0);
}
DateTime date=newdatetime(int.Parse(year.Text),int.Parse(month.Text),1)//year.Text和month.Text只是表单中控件的月份和年份。
对于(int j=7;j
问题在于,在for循环的每次迭代之后,都没有更新日期。我的意思是你这样做是错误的:

您应该使用如下内容:

date = date.AddDays(1.0);
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1);
for (int j = 0; j < daysInMonth; j++)
{
   // Skip increasing the first date
   if (j > 0) {
      date = date.AddDays(1.0);
   }

   // Set the correct date to the sheet
   sheet.get_Range("A" + (j + 7)).Value = date.ToString(@"dd/MM/yyyy");
}
更新:以上部分是您的代码中的一个小问题。主要问题在于Excel本身。当您将日期传递给它时,它会混淆它是月还是日,而不管您为它提供的格式如何

解决方法:Excel将日期存储为1900年1月1日之后的天数()。因此,您可以根据该格式设置单元格的值,然后将单元格的数字格式更改为通常设置为“DD/MM/YYYY”格式的格式

从日期获取整数值的函数:

private int convertDateTime(DateTime input)
{
    DateTime start = new DateTime(1900, 1, 1);
    return (int)(input - start).TotalDays + 2; //This is some calibration with the above mentioned Office article
}
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form.
for (int j = 7; j < daysInMonth + 7; j++)
{
    Range cell = sheet.get_Range("A" + j);
    cell.Value = convertDateTime(date);
    cell.NumberFormat = "DD/MM/YYYY";
    date = date.AddDays(1.0);
}
现在,您可以像这样使用代码:

private int convertDateTime(DateTime input)
{
    DateTime start = new DateTime(1900, 1, 1);
    return (int)(input - start).TotalDays + 2; //This is some calibration with the above mentioned Office article
}
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1); //year.Text & month.Text are just the month and year from controls in the form.
for (int j = 7; j < daysInMonth + 7; j++)
{
    Range cell = sheet.get_Range("A" + j);
    cell.Value = convertDateTime(date);
    cell.NumberFormat = "DD/MM/YYYY";
    date = date.AddDays(1.0);
}
DateTime date=newdatetime(int.Parse(year.Text),int.Parse(month.Text),1)//year.Text和month.Text只是表单中控件的月份和年份。
对于(int j=7;j
尝试计算循环中的单元格位置,并在将其设置为工作表之前增加日期,如下所示:

date = date.AddDays(1.0);
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1);
for (int j = 0; j < daysInMonth; j++)
{
   // Skip increasing the first date
   if (j > 0) {
      date = date.AddDays(1.0);
   }

   // Set the correct date to the sheet
   sheet.get_Range("A" + (j + 7)).Value = date.ToString(@"dd/MM/yyyy");
}
DateTime date=newdatetime(int.Parse(year.Text),int.Parse(month.Text),1);
对于(int j=0;j0){
日期=日期加天数(1.0);
}
//为工作表设置正确的日期
sheet.get_范围(“A”+(j+7)).Value=date.ToString(@“dd/MM/yyyy”);
}

尝试计算循环中的单元格位置,并在将其设置为工作表之前增加日期,如下所示:

date = date.AddDays(1.0);
DateTime date = new DateTime(int.Parse(year.Text), int.Parse(month.Text), 1);
for (int j = 0; j < daysInMonth; j++)
{
   // Skip increasing the first date
   if (j > 0) {
      date = date.AddDays(1.0);
   }

   // Set the correct date to the sheet
   sheet.get_Range("A" + (j + 7)).Value = date.ToString(@"dd/MM/yyyy");
}
DateTime date=newdatetime(int.Parse(year.Text),int.Parse(month.Text),1);
对于(int j=0;j0){
日期=日期加天数(1.0);
}
//为工作表设置正确的日期
sheet.get_范围(“A”+(j+7)).Value=date.ToString(@“dd/MM/yyyy”);
}


你能试试date.ToOADate()吗?@DasKrümelmonster你想用.ToOADate()替换.toSortDateString()?你能提供更多的代码吗?从您创建工作表的位置开始,输入daysInMonth变量,并提供文本框输入。@Farhanam“文本框输入”是什么意思?您在年和月文本框中输入的内容会产生不正确的结果吗?您是否可以尝试date.ToOADate()?@DasKrümelmonster您的意思是用.ToOADate()替换.ToSortDateString()?你能提供更多的代码吗?从您创建工作表的位置开始,输入daysInMonth变量,并提供文本框输入。@Farhanam“文本框输入”是什么意思?您在年和月文本框中输入了什么,从而产生了不正确的结果?好的,请稍候。我正在用一个消息框测试这个。让我用excel interop重现错误。@AnDrOiD我想问一个问题:当你运行应用程序时,excel应用程序是否也打开了?否,它只创建了文件。请参阅更新的答案。现在保证它可以工作。:-)我真的希望它能起作用!(我几分钟后再试)。你在解释中写的是1990年,但在代码中写的是1900年。是哪一个?好的,等一下。我正在用一个消息框测试这个。让我用excel interop重现错误。@AnDrOiD我想问一个问题:当你运行应用程序时,excel应用程序是否也打开了?否,它只创建了文件。请参阅更新的答案。现在保证它可以工作。:-)我真的希望它能起作用!(我几分钟后再试)。你在解释中写的是1990年,但在代码中写的是1900年。是哪一个?增量没有问题。出于某种原因,从第一天到第十二天的日期格式很奇怪,然后就可以了。你是说在.Text中添加.ToString()?不。。。还是一样:\n增量没有问题。出于某种原因,从第一天到第十二天的日期格式很奇怪,然后就可以了。你是说在.Text中添加.ToString()?不。。。还是一样:\