如何在EPPlus中更改其月份时,如何创建新工作表并将其保存在那里

时间:2017-10-04 02:56:32

标签: c# excel winforms epplus

enter image description here我有一个名为datein的列,其中包含日期。问题是我怎样才能创建一个新的工作表,并在日期改变月份时将其保存在那里。新月新工作表。

我现在所拥有的是使用epplus保存到Excel中的代码。

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        using (MySqlConnection con = new MySqlConnection(connectionString))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM statusrouted.routed", con))
            {
                cmd.CommandType = CommandType.Text;
                using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {

                        using (ExcelPackage pck = new ExcelPackage())
                        {
                            sda.Fill(dt);

                            ExcelWorksheet ws = pck.Workbook.Worksheets.Add(DateTime.Today.ToString("MMMM-yyyy"));

                            ws.Cells.Style.Font.Size = 11;
                            ws.Cells["B1:K1"].Merge = true;
                            ws.Cells["B1"].Value = "INCOMING AND OUTGOING ROUTED COMMUNICATIONS";
                            ws.Cells["B1"].Style.Font.Size = 12;
                            ws.Cells["B1"].Style.Font.Bold = true;
                            ws.Cells["B1"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
                            ws.Cells["B1"].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
                            ws.Cells["A2"].LoadFromDataTable((this.maindgv.DataSource as DataTable).DefaultView.ToTable(), true);


                            ws.DeleteColumn(1);

                            saveFileDialog1.Title = "Save as Excel";
                            saveFileDialog1.FileName = "";

                            saveFileDialog1.Filter = "Excel files(2007)|*.xlsx";

                            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                            {
                                try
                                {
                                    pck.SaveAs(new FileInfo(@""+ saveFileDialog1.FileName));
                                    recentsToolStripMenuItem1.AddRecentItem(@"" + saveFileDialog1.FileName);
                                }
                                catch (Exception)
                                {
                                    DialogResult reminder = MessageBox.Show("Cannot save file, file opened in another program.\nClose it first! ", "Save Failed", MessageBoxButtons.OK);
                                }
                            }
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

逻辑如何工作如下所示 enter image description here

请按照以下步骤操作

var dateGroups  = dt.AsEnumerable().GroupBy(row => row["ColumnName of Date Time"]);
foreach (var group in dateGroups)
{
 // the group.Key will give the Date on the basis of which you can create sheets. 
// and then your logic of excel
  foreach (var rows in group)
  {
   // excel filling logic
  }
}