using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Baza_Reklam
{
    class ExcelHandler
    {
        public void exportToExcel(DataGridView source)
        {
            Excel.ApplicationClass excel = new Excel.ApplicationClass();
            excel.Application.Workbooks.Add(true);
            DataGridView table = source;
            int cIndex = 0;
            foreach (DataGridViewColumn col in table.Columns)
            {
                if (col.Visible)
                {
                    cIndex++;
                    excel.Cells[1, cIndex] = col.HeaderText;
                }
            }
            int rIndex = 0;

            foreach (DataGridViewRow row in table.Rows)
            {
                rIndex++;
                cIndex = 0;
                foreach (DataGridViewColumn col in table.Columns)
                {
                    if (col.Visible)
                    {
                        cIndex++;
                        excel.Cells[rIndex + 1, cIndex] = row.Cells[col.Name].Value == null ? "" : row.Cells[col.Name].Value.ToString();
                    }
                }
            }
            //  excel.Save("New.xls");
            excel.Visible = true;
        }
    }
}
