/// <summary>
/// 保存文件
/// 涂聚文
/// 2014-08-29
/// Geovin Du
/// </summary>
/// <param name="dGV"></param>
/// <param name="filename"></param>
public static 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.Columns[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>
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.Columns[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(1200);//unicode encoding
byte[] output = Encoding.Default.GetBytes(stOutput);
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的更多相关文章

  1. ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file

    Perhaps you’re looking for this? ogr2ogr -f “CSV” “E:\4_GIS\NorthArkCartoData\UnitedStates\MO_wkt” “ ...

  2. C# - CSV file reader

    // ------------------------------------------------------------------------------------------------- ...

  3. SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server

    CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. Create TestTabl ...

  4. [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

    Hello everyone, this is the third post of the series. .   Background =============== In my solution, ...

  5. Qt Read and Write Csv File

    This page discusses various available options for working with csv documents in your Qt application. ...

  6. Python: Write UTF-8 characters to csv file

    To use codecs, we can write UTF-8 characters into csv file import codecs with open('ExcelUtf8.csv', ...

  7. save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv)

    save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv) 2019-10-2 ...

  8. Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

    PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I ...

  9. SQL Script for select data from ebs and make a csv file to FTP

    DECLARE CURSOR cur_lcy_test IS SELECT rcta.customer_trx_id, rcta.trx_number, rcta.trx_date FROM ra_c ...

随机推荐

  1. Hibernate入门教程

    Hibernate 随心所欲的使用面向对象思想操纵数据库. Table of contents 介绍 搭建开发环境 半sql半面向对象写法 完全的sql写法 完全的面向对象写法 Hibernate H ...

  2. JAVA输入随笔

    做题时经常遇到输入的问题,很麻烦 写一点点自己对于输入的随笔,以备后查 这里都以整数为例,其他类型的话换成相应方法就行了 1.知道一共多少行,每一行只有一个整数 这是比较简单的输入,可以用Scanne ...

  3. 关于web界面设计的整体可维护性的感悟

    1.表现与数据分开管理: 某些数据具备特殊的表现格式,比如颜色,大小等等.为了对这些格式表现分开管理进行 a.使用css定义该类型数据的表现形式: 定义数据的类别,通过该类别对数据格式进行统一定义 . ...

  4. 进阶篇:5.3.1)均方根法(Root-Sum-Squares,RSS)

    本章目的:了解均方根法,运用均方根法. 1.定义 均方根法(Root-Sum-Squares,RSS):均方根法是统计分析法的一种,是把尺寸链中的各个尺寸公差的平方之和再开根即得到关键尺寸的公差. 其 ...

  5. Codeforces - tag::flows 大合集 [完坑 x14]

    589F 题意:给出n个时间区间,每个区间挑定长的非连续区间,求不同个区间不存在时间冲突的最大定长,输出乘上n 二分图模型+二分长度,左顶点集为区间编号,右顶点集为时间编号(1...10000),汇点 ...

  6. HDU - 4866 主席树 二分

    题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...

  7. 【实战】sqlmap显示有注入却无法爆出库名

    sqlmap爆mssql数据库时采用的语句如下图: 从语句中不难看出,如果关键字select被“(非tamper绕过)处理”了,那sqlmap是无法爆出数据库的,这时我们可以使用原始的猜解法, #判断 ...

  8. grep练习

    1.查找特定字符串 [root@server3 mnt]# cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/no ...

  9. 转:五年java人的一点感悟

    转自:五年java人的一点感悟 恍然间,发现自己在这个行业里已经摸爬滚打了五年了,原以为自 己就凭已有的项目经验和工作经历怎么着也应该算得上是一个业内比较资历的人士了,但是今年在换工作的过程中却遭到了 ...

  10. js获取字符串字节的位数

    ifSubUser.getBlength = function(str){ ;i--;){ n += str.charCodeAt(i) > ? : ; } return n; }