Różnice między wybraną wersją a wersją aktualną.
Ostatnia wersja | |||
— | c-sharp-datagridtocsv [2013/03/26 13:45] – utworzono kamil | ||
---|---|---|---|
Linia 1: | Linia 1: | ||
+ | ===== C Sharp - DataGrid to CSV ===== | ||
+ | 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, | ||
+ | StreamWriter fsZapis = new StreamWriter(fs, | ||
+ | for (int x = 0; x < tabela.ColumnCount; | ||
+ | temp = temp + tabela.Columns[x].HeaderText + ";"; | ||
+ | fsZapis.WriteLine(temp); | ||
+ | for (int y = 0; y < tabela.RowCount; | ||
+ | { | ||
+ | temp = ""; | ||
+ | for (int x = 0; x < tabela.ColumnCount; | ||
+ | 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); | ||
+ | </ |