C# DataGridView의 데이터를 EXCEL로 내보내기

 Microsoft Office Excel: 1using Excel = Microsoft.Office.Interop.Excel; 
 1 DataGridView Excel#region  DataGridView Excel
 2        /**//// 
 3        ///  DataGridView Excel
 4        /// 
 5        /// DataGridView 
 6        ///  Excel 
 7        /// 
  
 8        public bool ExportDataGridview(DataGridView gridView,bool isShowExcle)
 9        {
10            if (gridView.Rows.Count == 0)
11                return false;
12            // Excel 
13            Excel.Application excel = new Excel.Application();
14            excel.Application.Workbooks.Add(true);
15            excel.Visible = isShowExcle;
16            // 
17            for (int i = 0; i < gridView.ColumnCount; i++)
18            {
19                excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
20            }
21            // 
22            for (int i = 0; i < gridView.RowCount-1; i++)
23            {
24                for (int j = 0; j < gridView.ColumnCount; j++)
25                {
26                    if (gridView[j, i].ValueType == typeof(string))
27                    {
28                        excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
29                    }
30                    else
31                    {
32                        excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
33                    }
34                }
35            }
36            return true;
37        }
38        #endregion  1if (!oper.ExportDataGridview(dgvEquiment, true))
2                MessageBox.Show(" , !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);

좋은 웹페이지 즐겨찾기