0% found this document useful (0 votes)
72 views2 pages

Export C# & Upload File

The document contains code for exporting the contents of a DataGridView control to a CSV file. It includes a method that loops through the columns and rows, concatenates the cell values with tabs, and writes the output to a file stream. There is also code to open a SaveFileDialog for the user to select the export file name and location. A recursive Copy method is defined to duplicate the contents of a source folder to a target folder.

Uploaded by

Evan Abie Eza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views2 pages

Export C# & Upload File

The document contains code for exporting the contents of a DataGridView control to a CSV file. It includes a method that loops through the columns and rows, concatenates the cell values with tabs, and writes the output to a file stream. There is also code to open a SaveFileDialog for the user to select the export file name and location. A recursive Copy method is defined to duplicate the contents of a source folder to a target folder.

Uploaded by

Evan Abie Eza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using using using using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; MySql.Data.MySqlClient; System.Configuration; System.IO;

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. for (int i = 0; i < dGV.RowCount - 1; i++) { string stLine = ""; for (int j = 0; j < dGV.Rows[i].Cells.Count; j++) stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Ce lls[j].Value) + "\t"; stOutput += stLine + "\r\n"; } Encoding utf16 = Encoding.GetEncoding(1254); byte[] output = utf16.GetBytes(stOutput); 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(); } //button SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel Documents (*.xls)|*.xls"; sfd.FileName = "export.xls"; if (sfd.ShowDialog() == DialogResult.OK) { //ToCsV(dataGridView1, @"c:\export.xls"); ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is you r grid view name } //upload // Recursive function to copy a folder's content into another folder void Copy(string sourceDir, string targetDir) { Directory.CreateDirectory(targetDir); foreach(var file in Directory.GetFiles(sourceDir)) File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

foreach(var directory in Directory.GetDirectories(sourceDir)) Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory))); }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy