using Microsoft.Office.Interop.Excel;
using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;


namespace HSUCO_Cargo_Garage_Operation_Program
{
    internal class ExcelWrapper
    {

        public bool WriteDataTableToExcel
        (System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string title, string writer, DateTime dateCreated)
        {
            Microsoft.Office.Interop.Excel.Application excel;
            Microsoft.Office.Interop.Excel.Workbook excelworkBook;
            Microsoft.Office.Interop.Excel.Worksheet excelSheet;
            Microsoft.Office.Interop.Excel.Range excelCellrange;

            try
            {
                //  get Application object.
                excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Visible = false;
                excel.DisplayAlerts = false;

                // Creation a new Workbook
                excelworkBook = excel.Workbooks.Add(Type.Missing);

                // Workk sheet
                excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
                excelSheet.Name = worksheetName;

                if (dataTable == null)
                {
                    return false;
                }
                var rowCount = 1;
                // 타이틀 
                excelSheet.Cells[rowCount, 1] = title;
                Range range = excelSheet.Range[excelSheet.Cells[rowCount, 1], excelSheet.Cells[rowCount, dataTable.Columns.Count]];
                range.Merge();
                range.Font.Size = 20;
                range.Font.Bold = true;
                range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                range.Borders.LineStyle = XlLineStyle.xlDouble;

                rowCount += 2;
                // 작성자
                range = excelSheet.Range[excelSheet.Cells[rowCount, dataTable.Columns.Count - 2], excelSheet.Cells[rowCount, dataTable.Columns.Count - 2]];
                range.Merge();
                range.Value = "작성자 : ";
                range.HorizontalAlignment = 4;
                range.Borders.LineStyle = XlLineStyle.xlContinuous;

                range = excelSheet.Range[excelSheet.Cells[rowCount, dataTable.Columns.Count - 1], excelSheet.Cells[rowCount, dataTable.Columns.Count]];
                range.Merge();
                range.Value = writer;
                range.HorizontalAlignment = 1;
                range.Borders.LineStyle = XlLineStyle.xlContinuous;

                // 데이트
                rowCount++;
                
                range = excelSheet.Range[excelSheet.Cells[rowCount, dataTable.Columns.Count - 2], excelSheet.Cells[rowCount, dataTable.Columns.Count - 2]];
                range.Merge();
                range.Value = "작성일 : ";
                range.HorizontalAlignment = 4;
                range.Borders.LineStyle = XlLineStyle.xlContinuous;
                range = excelSheet.Range[excelSheet.Cells[rowCount, dataTable.Columns.Count - 1], excelSheet.Cells[rowCount, dataTable.Columns.Count]];
                range.Merge();
                range.Value = dateCreated.ToString("yyyy-MM-dd HH:mm:ss");
                range.HorizontalAlignment = 1;
                range.Borders.LineStyle = XlLineStyle.xlContinuous;
                
                
                rowCount += 2;
                int startRow = rowCount;

                // 실제 데이터 삽입 
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    excelSheet.Cells[rowCount, i + 1] = dataTable.Columns[i].ColumnName;
                }

                foreach (DataRow datarow in dataTable.Rows)
                {
                    rowCount += 1;

                    for (int i = 1; i <= dataTable.Columns.Count; i++)
                    {
                        excelSheet.Cells[rowCount, i] = datarow[i - 1].ToString();
                    }
                }
                excelCellrange = excelSheet.Range[excelSheet.Cells[startRow, 1], excelSheet.Cells[rowCount, dataTable.Columns.Count]];
                excelCellrange.Columns.AutoFit();
                excelCellrange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                excelCellrange.Borders.LineStyle = XlLineStyle.xlContinuous;
                // now save the workbook and exit Excel
                excelworkBook.SaveAs(saveAsLocation);
                excelworkBook.Close();
                excel.Quit();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                excelSheet = null;
                excelCellrange = null;
                excelworkBook = null;
            }
        }

    }
}