Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void ToCsV(DataGridView dGV, string filename)
- {
- string stOutput = "";
- // Export titles:
- string sHeaders = "";
- for (int j = 0; j < dGV.Columns.Count; j++)
- sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
- stOutput += sHeaders + "\r\n";
- // Export data.
- StringBuilder sbOutput = new StringBuilder();
- for (int j = 0; j < dGV.Columns.Count; j++)
- sbOutput.AppendLine(Convert.ToString(dGV.Columns[j].HeaderText) + "\t");
- for (int i = 0; i < dGV.RowCount - 1; i++)
- {
- for (int j = 1; j < dGV.Rows[i].Cells.Count; j++)
- sbOutput.Append(Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t");
- sbOutput.AppendLine();
- }
- Encoding utf16 = Encoding.GetEncoding(1254);
- byte[] output = utf16.GetBytes(sbOutput.ToString());
- FileStream fs = new FileStream(filename, FileMode.Create);
- BinaryWriter bw = new BinaryWriter(fs);
- bw.Write(output, 0, output.Length); //write the encoded file
- bw.Flush();
- bw.Close();
- fs.Close();
- }
Advertisement
Add Comment
Please, Sign In to add comment