/// <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. 子元素margin-top为何会影响父元素?

    子元素margin-top为何会影响父元素? 引用地址:https://blog.csdn.net/sinat_27088253/article/details/52954688 2016年10月28 ...

  2. SQL总结-----触发器

    概念 触发器是一种特殊类型的存储过程,不由用户直接调用.创建触发器时会对其进行定义,以便在对特定表或列作特定类型的数据修改时执行. 触发器可以查询其他表,而且可以包含复杂的 SQL 语句. 它们主要用 ...

  3. 海思3519A上移植OpenCV3.x

    环境安装与准备 下载opencv3.4.1. 或者 下载opencv3.2.0. 进入到opencv所在的目录新建目录build,install 安装cmake: apt-get install cm ...

  4. input 标签只能输入数字

    $("input[name='contact']").keyup(function(){ $("input[name='contact']").attr(&qu ...

  5. BottomSheetDialogFragment 如何设置高度和禁止滑动

    主要是获取dialog 的BottomSheetBehavior 然后设置 setPeekHeight 和 BottomSheetCallback. private BottomSheetBehavi ...

  6. ubuntu 18 常用软件安装

    主要内容 1.安装 Ubuntu 18.04 LTS 2.安装 Google Chrome 3.安装 OpenVPN Client 4.安装 Docker CE 5.安装 MySQL Server 转 ...

  7. Ubuntu下配置安装Hadoop 2.2

    ---恢复内容开始--- 这两天玩Hadoop,之前在我的Mac上配置了好长时间都没成功的Hadoop环境,今天想在win7 虚拟机下的Ubuntu12.04 64位机下配置, 然后再建一个组群看一看 ...

  8. Zookeeper概念学习系列之paxos协议

    不多说,直接上干货! 前言 一种最终一致的算法,paxos算法. paxos算法是由大牛lamport发明的,关于paxos算法有很多趣事.比如lamport论文最初由故事描述来引入算法,以至于那班习 ...

  9. spring-boot启动后在浏览器打开指定页面

    来自:https://stackoverflow.com/questions/27378292/launch-browser-automatically-after-spring-boot-webap ...

  10. 【LESS系列】基本语法

    这里将直接以实例的方式展示 LESS 的基本语法. less code 是编译前的代码,css code 是编译后的代码. 本文的内容,同样是引自[http://www.ibm.com/develop ...