root/Baza Reklam 2 - Faktury/Classes/ExcelHandler.cs @ 2

Wersja 2, 1.3 KB (wprowadzona przez dorota, 17 years temu)
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Windows.Forms;
5
6namespace Baza_Reklam
7{
8    class ExcelHandler
9    {
10        public void exportToExcel(DataGridView source)
11        {
12            Excel.ApplicationClass excel = new Excel.ApplicationClass();
13            excel.Application.Workbooks.Add(true);
14            DataGridView table = source;
15            int cIndex = 0;
16            foreach (DataGridViewColumn col in table.Columns)
17            {
18                if (col.Visible)
19                {
20                    cIndex++;
21                    excel.Cells[1, cIndex] = col.HeaderText;
22                }
23            }
24            int rIndex = 0;
25
26            foreach (DataGridViewRow row in table.Rows)
27            {
28                rIndex++;
29                cIndex = 0;
30                foreach (DataGridViewColumn col in table.Columns)
31                {
32                    if (col.Visible)
33                    {
34                        cIndex++;
35                        excel.Cells[rIndex + 1, cIndex] = row.Cells[col.Name].Value == null ? "" : row.Cells[col.Name].Value.ToString();
36                    }
37                }
38            }
39            //  excel.Save("New.xls");
40            excel.Visible = true;
41        }
42    }
43}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.