Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views2 pages

Data Grid

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Data Grid

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

...GridView\GUI­DataGridView\GUI­DataGridView\ExcelRead.

cs 1
1 using System;
2
3
4 using System.Data;
5 using System.Drawing;
6
7
8
9 using System.Windows.Forms;
10 using System.IO;
11 using Excel;
12 using app = Microsoft.Office.Interop.Excel.Application;
13
14 namespace GUI_DataGridView
15 {
16 public partial class ExcelRead : Form
17 {
18 public ExcelRead()
19 {
20 InitializeComponent();
21 }
22
23 DataSet result;
24 private void btnImport_Click(object sender, EventArgs e)
25 {
26 OpenFileDialog Myopen = new OpenFileDialog();
27 Myopen.Title = "Excel Open File";
28 Myopen.InitialDirectory = @"C:\Users\Cuong Nguyen\Desktop\";
29 Myopen.Filter = "Excel | *.xlsx";
30 if (Myopen.ShowDialog() == DialogResult.OK)
31 {
32 txtPath.Text = Myopen.FileName;
33 FileStream fs = File.Open(Myopen.FileName, FileMode.Open,
FileAccess.Read);
34 IExcelDataReader reader =
ExcelReaderFactory.CreateOpenXmlReader(fs);
35 reader.IsFirstRowAsColumnNames = true;
36 result = reader.AsDataSet();
37 cbSheet.Items.Clear();
38 foreach (DataTable dt in result.Tables)
39 {
40 cbSheet.Items.Add(dt.TableName);
41 }
42 reader.Close();
43 MessageBox.Show("Successful",
"Information",MessageBoxButtons.OK,MessageBoxIcon.Information
);
44 }
45 }
46
47 private void cbSheet_SelectedIndexChanged(object sender, EventArgs e)
48 {
49 dataGridView.DataSource = result.Tables[cbSheet.SelectedIndex];
50 }
51
52 private void ExcelRead_Load(object sender, EventArgs e)
...GridView\GUI­DataGridView\GUI­DataGridView\ExcelRead.cs 2
53 {
54 dataGridView.EnableHeadersVisualStyles = false;
55 dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font
(dataGridView.Font.Name, dataGridView.Font.Size, FontStyle.Bold);
56 }
57
58 public void MEexport (DataGridView dg)
59 {
60 SaveFileDialog Mysave = new SaveFileDialog();
61 Mysave.Title = "Export Open File";
62 Mysave.InitialDirectory = @"C:\Users\Cuong Nguyen\Desktop";
63 Mysave.Filter = "Excel | *.xlsx";
64 if (Mysave.ShowDialog() == DialogResult.OK)
65 {
66 app obj = new app();
67 obj.Application.Workbooks.Add(Type.Missing);
68 obj.Columns.ColumnWidth = 10;
69 for (int i = 0; i < dg.Columns.Count; i++)
70 {
71 obj.Cells[1, i + 1] = dg.Columns[i].HeaderText;
72 }
73
74 for (int i = 0; i < dg.Rows.Count ­ 1; i++)
75 {
76 for (int j = 0; j < dg.Columns.Count; j++)
77 {
78 if (dg.Rows[i].Cells[j].Value != null)
79 {
80 obj.Cells[i + 2, j + 1] = dg.Rows[i].Cells
[j].Value;
81 }
82 }
83 }
84 obj.ActiveWorkbook.SaveAs(Mysave.FileName);
85 obj.ActiveWorkbook.Saved = true;
86 MessageBox.Show("Complete", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
87 }
88
89 }
90 private void btnExport_Click(object sender, EventArgs e)
91 {
92 MEexport(dataGridView);
93 }
94
95 }
96 }
97

You might also like