martedì 26 febbraio 2013

Export DataGridView to Excel




public void ExportExcell(DataGridView dgw, int[] columnsToExport, int columnsWidth, string path, bool visible, bool quit)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);


            ExcelApp.Columns.ColumnWidth = columnsWidth;

            int columnExcel = 1;
            List<int> columns = new List<int>();

            for (int a = 0; a < (columnsToExport.Length); a++)
                columns.Add(columnsToExport[a]);


            for (int i = 0; i < dgw.Columns.Count; i++)
            {
                if (columns.Contains(i))
                {
                    ExcelApp.Cells[1, columnExcel] = dgw.Columns[i].HeaderText;
                    columnExcel++;
                }
            }

            columnExcel = 1;
            for (int i = 0; i < dgw.Rows.Count; i++)
            {
                for (int j = 0; j < dgw.Columns.Count; j++)
                {
                    if (columns.Contains(j))
                    {
                        ExcelApp.Cells[i + 2, columnExcel] = dgw.Rows[i].Cells[j].Value.ToString();
                        columnExcel++;
                    }
                    //+2 because must write from second row of the excel sheet
                }
                columnExcel = 1;
            }

            ExcelApp.Visible = visible;

            if (!String.IsNullOrEmpty(path))
            {
                ExcelApp.ActiveWorkbook.SaveCopyAs(path);
                ExcelApp.ActiveWorkbook.Saved = true;
            }
           
            if(quit)
                ExcelApp.Quit();
        }




public void ExportExcell(DataGridView dgw, string[] columnsToExport, int columnsWidth, string path, bool visible, bool quit)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new              Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);


            ExcelApp.Columns.ColumnWidth = columnsWidth;

            int columnExcel = 1;
            List<string> columns = columnsToExport.OfType<string>().ToList();

            for (int i = 0; i < columnsToExport.Length; i++)
            {
                if (dgw.Columns.Contains(columnsToExport[i]))
                {
                    ExcelApp.Cells[1, columnExcel] = dgw.Columns[columnsToExport[i]].HeaderText;
                    columnExcel++;
                }
            }

            columnExcel = 1;
            foreach (string item in columns)
            {
                for (int i = 0; i < dgw.Rows.Count; i++)
                {
                    ExcelApp.Cells[i + 2, columnExcel] = dgw.Rows[i].Cells[item].Value.ToString();
                }
                columnExcel++;
            }

            ExcelApp.Visible = visible;

            if (!String.IsNullOrEmpty(path))
            {
                ExcelApp.ActiveWorkbook.SaveCopyAs(path);
                ExcelApp.ActiveWorkbook.Saved = true;
            }

            if (quit)
                ExcelApp.Quit();
        }






Nessun commento:

Posta un commento