Create ClosedXML Excel

using ClosedXML;
using ClosedXML.Excel;
using System.IO;
        private void button1_Click(object sender, EventArgs e)
        {
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("Merge Cells");

            DateTime fromDate = new DateTime(2015, 03, 20);
            DateTime toDate = new DateTime(2015, 03, 30);
            Int32 columnPosition = 2;
            for (DateTime date = fromDate; date <= toDate; date = date.AddDays(1))
            {
                var range = ws.Range(ws.Cell(1, columnPosition).Address, ws.Cell(1, columnPosition+1).Address);
                range.Cell(1, 1).Value = date.ToString("yyyy-MM-dd");
                range.Merge();
                range.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                range.Style.Fill.BackgroundColor = XLColor.Almond;

                var firstCell = ws.Cell(2, columnPosition);
                firstCell.Value = "1st"; firstCell.Style.Fill.BackgroundColor = XLColor.Amethyst;
                var secondCell = ws.Cell(2, columnPosition + 1);
                secondCell.Value = "2nd"; secondCell.Style.Fill.BackgroundColor = XLColor.Amethyst;

                columnPosition += 2;
              
            }

          
            workbook.SaveAs("MergeCells.xlsx");
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s