private void Excel_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count != 0) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Microsoft Excel 97/2000/XP(*.xls)|*.xls"; sfd.FileName = "Report.xls"; if (sfd.ShowDialog() == DialogResult.OK) { StreamWriter wr = new StreamWriter(sfd.FileName); int cols = dataGridView1.Columns.Count; for (int i = 0; i < cols; i++) { wr.Write(dataGridView1.Columns[i].Name.ToUpper() + "\t"); } wr.WriteLine(); for (int i = 0; i < (dataGridView1.Rows.Count -1); i++) { for (int j = 0; j < cols; j++) { wr.Write(dataGridView1.Rows[i].Cells[j].Value + "\t"); } wr.WriteLine(); } wr.Close(); label9.Text = "Your file has been successfully saved !"; } } else { MessageBox.Show("Error"); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)