Hi freinds
I have a DataGridView in WindowsForm and i want to print this grid but exept of print i want import it to excell file(but not read only file)
how is it possible?
Thank you
Grid To Excell
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Maharaje71 pointsPosted Aug 25, 2009, 10:46 PM EDT
Your answer....
this is all of codes that i needed:
class ExportToExcell { public static void ExportDataGridViewToExcell(DataGridView grid,Form frm) { SaveFileDialog save = new SaveFileDialog(); save.Filter = "Excel files(*.xls)|*.xls"; save.FilterIndex = 0; save.RestoreDirectory = true; save.CreatePrompt = true; save.Title = frm.Text; save.FileName = frm.Text; save.ShowDialog(); Stream myStream; myStream = save.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("windows-1256")); string str = ""; try { for (int i = 0; i < grid.ColumnCount; i++) { if (i > 0) { str += "\t"; } str += grid.Columns[i].HeaderText; } sw.WriteLine(str); for (int j = 0; j < grid.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < grid.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } if (grid.Rows[j].Cells[k].Value != null) tempStr += grid.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); MessageBox.Show("**êë¢çé ¬§**"); } catch (Exception e) { MessageBox.Show(e.ToString()); } finally { sw.Close(); myStream.Close(); } } }