C# 根据日期c打开文件#
我想知道是否有人能帮忙 我创建了一个基于4周轮班表的电子表格,它计算了4周内的所有工作时间,并计算出月末的预期工资 我现在正在用C#编写一个桌面小工具来显示“今天”和“明天”的开始和结束时间。我正在使用C# 根据日期c打开文件#,c#,excel,C#,Excel,我想知道是否有人能帮忙 我创建了一个基于4周轮班表的电子表格,它计算了4周内的所有工作时间,并计算出月末的预期工资 我现在正在用C#编写一个桌面小工具来显示“今天”和“明天”的开始和结束时间。我正在使用GemBox.Spreadsheets解析xls文件中的信息 因为它基于4周一次的工资结构,所以每个工资周期最终会有13个不同的xls文件 我需要的是确定C#应用程序相对于该支付期自动打开哪个文件 i、 e: 如果今天的日期大于2017年4月22日,但小于2017年5月21日,请打开文件6。 如果
GemBox.Spreadsheets
解析xls文件中的信息
因为它基于4周一次的工资结构,所以每个工资周期最终会有13个不同的xls文件
我需要的是确定C#应用程序相对于该支付期自动打开哪个文件
i、 e:
如果今天的日期大于2017年4月22日,但小于2017年5月21日,请打开文件6。如果今天的日期大于2017年5月21日但小于2017年6月18日,请打开文件7。
如果今天的日期大于2017年6月18日但小于2017年7月15日,请打开文件8。
等等等 我只是没办法想清楚 以下是我当前的代码:
ExcelFile ef = ExcelFile.Load("wages.xls");
var ws = ef.Worksheets.ActiveWorksheet;
DateTime dateNow = DateTime.Now;
var date = dateNow.Date;
double d = date.ToOADate();
string dateConv = d.ToString();
int objectRow, objectColumn;
ws.Cells.FindText(dateConv, false, false, out objectRow, out objectColumn);
if (objectRow == -1 || objectColumn == -1)
{
todaysShift.Text = "No Data Found!";
tomorrowsShift.Text = "No Data Found";
}
else
{
string todayCellRow = ws.Cells[objectRow, 1].Row.ToString();
string tomorrowCellRow = ws.Cells[objectRow + 1, 1].Row.ToString();
DateTime todayStartTime = Convert.ToDateTime(ws.Cells["E" + todayCellRow].Value);
DateTime todayFinishTime = Convert.ToDateTime(ws.Cells["F" + todayCellRow].Value);
DateTime tomorrowStartTime = Convert.ToDateTime(ws.Cells["E" + tomorrowCellRow].Value);
DateTime tomorrowFinishTime = Convert.ToDateTime(ws.Cells["F" + tomorrowCellRow].Value);
string tdayShiftS = todayStartTime.ToString("HH:mm");
string tdayShiftF = todayFinishTime.ToString("HH:mm");
string tmorShiftS = tomorrowStartTime.ToString("HH:mm");
string tmorShiftF = tomorrowFinishTime.ToString("HH:mm");
if (tdayShiftS == "00:00" && tdayShiftF == "00:00")
{
todaysShift.Text = "Day Off!";
}
else
{
todaysShift.Text = tdayShiftS + " - " + tdayShiftF;
}
if (tmorShiftS == "00:00" && tmorShiftF == "00:00")
{
tomorrowsShift.Text = "Day Off!";
}
else
{
tomorrowsShift.Text = tmorShiftS + " - " + tmorShiftF;
}
}
从您描述的情况来看,您的第一个4周周期似乎始于2016年12月3日。看起来很奇怪,但你更清楚 我们称之为基准日期。如果您只需从“今天”中减去基准日期,您将得到一个时间跨度,从中可以计算天数、周数和4周周期
const DateTime baseDate = new DateTime(2016,12,3);
...
//get rid of time part and calculate days from base date.
TimeSpan diff = DateTime.Today - baseDate;
int weeksDiff = diff.Days / 7;
int periodNum = weeksDiff / 4;
我将建立一个类与所有信息的付款期和建设者建立从期间索引和日期
class PayPeriod
{
const int DAYS_PER_WEEK = 7;
const int WEEKS_PER_PERIOD = 4;
static readonly DateTime _startPeriod = new DateTime( 2017, 01, 01 );
public PayPeriod( int index )
{
Index = index;
StartDate = _startPeriod.Date.AddDays( ( index - 1 ) * DAYS_PER_WEEK * WEEKS_PER_PERIOD );
EndDate = StartDate.AddDays( DAYS_PER_WEEK * WEEKS_PER_PERIOD - 1 );
}
public static PayPeriod FromDate( DateTime date )
{
var index = ( date.Date - _startPeriod ).Days / DAYS_PER_WEEK / WEEKS_PER_PERIOD + 1;
return new PayPeriod( index );
}
public int Index { get; }
public DateTime StartDate { get; }
public DateTime EndDate { get; }
}
您计算这4周时间的起始日期是什么?这一年从2017年1月4日开始,这是一个周日。电子表格开始于周日,结束于28天后的周六。正如我所说,每张表代表了28天(4周)的轮班模式。我们是说1。2017年6月6日或。2017年1月(在我的日历上都不是星期日)?对不起,2017年1月1日可以吗,期待2020年,也就是2020年第一个4周的开始日期?(我只是想从你那里得到计算规则)我想知道整数除法是否舍入错误。试着让我的头绕着它。@Flater-整数除法取整。没问题。当weeksDiff为0,1,2,3时,periodNum将为0。我想你可能想加1。@SirRufo-我不理解你的评论。我的帖子中没有2017年1月6日或周日。OP说4月22日是第6期,我从那里开始工作。OP在评论中发布的开始日期是2017年1月6日(星期日),现在编辑为2017年1月4日(星期日),在2017年1月1日(星期日)的附加评论中,这与第6期的22/4不太相符。在任何情况下,我确信OP都能够在我的代码片段中更改baseDate。