using System.Windows.Forms; namespace excel2 internal class ExportHelper { public bool Export(DataGridView dgv) { bool exported = false; List<string> lines = new List<string>(); DataGridViewColumnCollection column = dgv.Columns; bool firstDone = false; StringBuilder columnLine = new StringBuilder(); foreach(DataGridViewColumn col in column) { if(!firstDone) { columnLine.Append(col.DataPropertyName); firstDone = true; } else { columnLine.Append("," + col.DataPropertyName); } } lines.Add(columnLine.ToString()); //data lines foreach(DataGridViewRow row in dgv.Rows) { StringBuilder dataLine = new StringBuilder(); firstDone = false; foreach(DataGridViewCell cell in row.Cells) { if (firstDone) { dataLine.Append(cell.Value); firstDone= true; } else { dataLine.Append(","+cell.Value); } lines.Add(dataLine.ToString()); } } string file = Path.Combine(Application.StartupPath, "Excel"); File.WriteAllLines(file,lines); //this line giving error System.Diagnostics.Process.Start(file); return exported; } }
private void button8_Click(object sender, EventArgs e) { new ExportHelper().Export(dataGridView1); }
string file = Path.Combine(Application.StartupPath, "Excel");
string file = Path.Combine(Application.StartupPath, "Excel.csv");
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)