Klasa:
using System; using System.Text; using System.IO; using System.Windows.Forms; namespace adm { class exportCsv { protected System.Windows.Forms.DataGridView tabela; public exportCsv(System.Windows.Forms.DataGridView tabelap) { tabela = tabelap; } public void exportToCSV(string plik) { string temp = ""; Encoding e = Encoding.GetEncoding(1250); FileStream fs = new FileStream(plik, FileMode.Create); StreamWriter fsZapis = new StreamWriter(fs, e); for (int x = 0; x < tabela.ColumnCount; x++) temp = temp + tabela.Columns[x].HeaderText + ";"; fsZapis.WriteLine(temp); for (int y = 0; y < tabela.RowCount; y++) { temp = ""; for (int x = 0; x < tabela.ColumnCount; x++) temp = temp + tabela.Rows[y].Cells[x].Value.ToString() + ";"; fsZapis.WriteLine(temp); } fsZapis.Close(); } } }
Zastosowanie:
exportCsv eksportuj = new exportCsv(tabSprzetu); saveFileDialog1.ShowDialog(); eksportuj.exportToCSV(saveFileDialog1.FileName);