Csharp:datagridview Convert CSV file

Source: Internet
Author: User
Tags rowcount

 <summary>////2014-08-29///////////Geovin Du//</summary> <param name= "DGV" ></param>///<param name= "filename" ></param> public St            atic void Datagridviewtocsv (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.column S[J].            HeaderText) + ",";            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]. CELLS[J].                Value) + ",";            Stoutput + = Stline + "\ r \ n"; } utf8encoding UTF8 = new UTF8Encoding ();            string file = "1.csv";            SaveFileDialog SaveFileDialog1 = new SaveFileDialog (); Savefiledialog1.initialdirectory = Environment.getfolderpath (Environment.SpecialFolder.Desktop);//            Environment.getfolderpath (Environment.SpecialFolder.MyDocuments);            Savefiledialog1.createprompt = true;            Savefiledialog1.overwriteprompt = true;            Savefiledialog1.title = "Save text Files";            Savefiledialog1.checkfileexists = true;            Savefiledialog1.checkpathexists = true;            Savefiledialog1.defaultext = "CSV"; Savefiledialog1.filter = "CSV files (*.csv) |*.csv|            All Files (*. *) |*.* ";            Savefiledialog1.filterindex = 1;            Savefiledialog1.restoredirectory = true;            System.IO.Stream FileStream; if (savefiledialog1.showdialog () = = DialogResult.OK) {//filestream fs = new FileStream (filename                , FileMode.Create); File = SAvefiledialog1.filename;                FileStream = Savefiledialog1.openfile ();                MemoryStream userinput = new MemoryStream ();                byte[] Output = Encoding.Default.GetBytes (Stoutput); FileStream.Read (output, 0, (int) output.                Length); FileStream.Write (output, 0, output.                Length);            Filestream.close (); }}///<summary>///2014-08-28///Geovin Du// /</summary>//<param name= "DGV" ></param>//<param name= "filename" ></param&gt        ;            public static void Datagridviewtocsvdu (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.column S[J].            HeaderText) + ","; 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]. CELLS[J].                Value) + ",";            Stoutput + = Stline + "\ r \ n";            } utf8encoding UTF8 = new UTF8Encoding (); Encoding utf16 = encoding.getencoding (;//unicode Encoding byte[] output = Encoding.Default.GetBytes (stOu            Tput);            FileStream fs = new FileStream (filename, filemode.create);            BinaryWriter bw = new BinaryWriter (FS, Encoding.default); Bw. Write (output, 0, output. Length); Write the encoded file BW.            Flush (); Bw.            Close (); Fs.                    Close (); }

Csharp:datagridview Convert CSV file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.