private void OpenToData(string filePath) { int iMonth = DateTime.Now.Month; string strDate = ""; if (comboBox1.SelectedIndex == 5) { strDate = "天津" + System.DateTime.Now.ToString("yyyy年") + "考勤"; iMonth = -1; } else { iMonth = iMonth - 2 + comboBox1.SelectedIndex; strDate = "天津" + (iMonth < 0 ? (System.DateTime.Now.AddYears(-1).ToString("yyyy年") + (iMonth + 12).ToString()) : (System.DateTime.Now.ToString("yyyy年") + (iMonth).ToString())) + "月考勤"; } try { DataToExcel("保存转换后的考勤表", strDate + "表", strDate, NameNumInfo.ReadNameToData(filePath, iMonth)); MessageBox.Show("考勤表转换完毕,接着统计信息", "统计"); DataToExcel("保存统计信息表", strDate + "信息", strDate + "统计", GatherDuty.GetDutyDataTable()); DialogResult dr = MessageBox.Show("统计完毕~要打开吗?", "统计完毕", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { System.Diagnostics.Process.Start(saveFileDialog1.FileName); } } catch (Exception e) { MessageBox.Show("出现错误:" + e.Message + "在" + e.StackTrace + ",和你的亲爱的联系!", "出错了"); } }
private void RefreshList() { listBox1.Items.Clear(); string FilePath = ""; if (File.Exists(Directory.GetCurrentDirectory() + NameNumInfo.Path)) { FilePath = Directory.GetCurrentDirectory() + NameNumInfo.Path; } else { DialogResult dr = MessageBox.Show("没找到NameNumInfo.txt,要不要指定一个名字信息文件?", "没找到", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { openFileDialog1.Filter = "文本文件(*.txt)|*.txt"; openFileDialog1.Title = "打开名字信息文件"; openFileDialog1.ShowDialog(); FilePath = openFileDialog1.FileName; } } if (FilePath != "") { NameNumInfo.ReadName(); } foreach (KeyValuePair <string, string> item in NameNumInfo.dictInfo) { listBox1.Items.Add(new Item(" " + item.Key + (item.Key.Length > 1 ? "" : " ") + " " + item.Value, item.Key)); } listBox1.Refresh(); }
public static System.Data.DataTable ReadNameToData(string filePath, int iMonth) { System.Data.DataTable dt = new System.Data.DataTable(); int iyear = iMonth < 0 ? DateTime.Now.AddYears(-1).Year : DateTime.Now.Year; string strFilt = new DateTime(iyear, iMonth < 0 ? iMonth + 12 : iMonth, 1).ToString("yyyy-MM"); using (StreamReader s = File.OpenText(filePath)) { string strLine = ""; strLine = s.ReadLine(); foreach (string item in NameNumInfo.TrimEmpty(strLine)) { dt.Columns.Add(item); } dt.Columns["EnNo"].DataType = typeof(System.Int32); strLine = s.ReadLine(); while (strLine != null) { List <string> value = NameNumInfo.TrimEmpty(strLine); value[value.Count - 2] += " " + value[value.Count - 1]; value.RemoveAt(value.Count - 1); value[value.Count - 1] = Convert.ToDateTime(value[value.Count - 1]).ToString("yyyy-MM-dd HH:mm:ss"); string name = ""; if (NameNumInfo.dictInfo.ContainsKey(value[2])) { name = NameNumInfo.dictInfo[value[2]]; } else { name = "<<没有对应!>>"; } value.Insert(3, name); if (value[value.Count - 1].IndexOf(strFilt) >= 0) { dt.Rows.Add(value.ToArray()); } strLine = s.ReadLine(); } DataView dv = dt.DefaultView; dv.Sort = " EnNo ASC"; dt = dv.ToTable(); } return(dt); }