csharp: datagridview Convert csv file
/// <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的更多相关文章
- 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” “ ...
- C# - CSV file reader
// ------------------------------------------------------------------------------------------------- ...
- 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 ...
- [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, ...
- Qt Read and Write Csv File
This page discusses various available options for working with csv documents in your Qt application. ...
- 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', ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- P1642 规划 01分数规划+树形DP
$ \color{#0066ff}{ 题目描述 }$ 某地方有N个工厂,有N-1条路连接它们,且它们两两都可达.每个工厂都有一个产量值和一个污染值.现在工厂要进行规划,拆除其中的M个工厂,使得剩下的工 ...
- Flutter Dialog 屏蔽返回键
使用 WillPopScope + Future.value(false); 屏蔽返回键.代码如下: showDialog<Null>( context: context, // Buil ...
- git提交代码
安装 Windows 下载安装地址 Linux 1 yum install git / apt-get install git 安装后执行,正常显示则安装正常 1 git --version 使用 生 ...
- am335x uboot启动流程分析
基本指令含义 .globl _start .globl指示告诉汇编器,_start这个符号要被链接器用到,所以要在目标文件的符号表中标记它是一个全局符号 b,bl b是不带返回的跳转 bl带返回的跳 ...
- php session的简单使用
创建session: session_start(); $_SESSION['name'] = $value; 获取session: session_start(); echo $_SESSION[' ...
- Java 数组实现堆栈操作
class Stack { private int stck[] ; private int tos ; Stack(int size) { // 一个参数的构造参数 stck = new int[s ...
- Python实现WEB QQ 登录与消息发送(第一版本 2015.06.26)
WEB QQ的登录步骤与协议,需要的度娘下,很多. 转载说明来源:http://www.cnblogs.com/ryhan/p/4602762.html 我这实现是参考了度娘搜的 和自己抓包分析的. ...
- Jmeter基础元件
测试计划 1.Test Plan (测试计划) 用来描述一个性能测试,包含与本次性能测试所有相关的功能.也就说JMeter创建性能测试的所有内容是于基于一个计划的. 下面看看一个计划下面都有哪些功能模 ...
- 修改Tomcat使用的JVM内存大小
我的服务器的配置: # OS specific support. $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...