SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Excel File"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName == "") return; Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try { for (int i = 0; i < dataGridView5.ColumnCount; i++) { if (i > 0) { str += " "; } str += dataGridView5.Columns[i].HeaderText; } sw.WriteLine(str); for (int j = 0; j < dataGridView5.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dataGridView5.Columns.Count; k++) { if (k > 0) { tempStr += " "; } tempStr += dataGridView5.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } finally { sw.Close(); myStream.Close(); } }