public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path)
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
} string strPath = path + fileName; StringBuilder strColu = new StringBuilder();
StringBuilder strLineValue = new StringBuilder();
try
{
if (System.IO.File.Exists(strPath))
{
string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
string line = string.Empty;
foreach (string readLine in stringlines)
{
line += readLine + "\r\n";
}
line = line.Remove(line.Length - 2, 2); System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); sw.WriteLine(line); foreach (DataRow dr in dt.Rows)
{
strLineValue.Remove(0, strLineValue.Length);
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
strLineValue.Append(",");
}
strLineValue.Remove(strLineValue.Length - 1, 1);
sw.WriteLine(strLineValue.ToString());
}
sw.Close();
}
else
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strColu.Append(dt.Columns[i].ColumnName);
strColu.Append(",");
}
strColu.Remove(strColu.Length - 1, 1);
sw.WriteLine(strColu); foreach (DataRow dr in dt.Rows)
{
strLineValue.Remove(0, strLineValue.Length);
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
strLineValue.Append(",");
}
strLineValue.Remove(strLineValue.Length - 1, 1);
sw.WriteLine(strLineValue.ToString());
}
sw.Close();
}
}
catch (Exception ex)
{
throw ex;
} } public static void ExportLog(System.Data.DataTable dtLogInfo, string fileName, string path)
{
string strPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); if (!System.IO.Directory.Exists(strPath))
{
System.IO.Directory.CreateDirectory(strPath);
} strPath = path + fileName; StringBuilder strColu = new StringBuilder();
StringBuilder strValue = new StringBuilder();
try
{
if (System.IO.File.Exists(strPath))
{
string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
string line = string.Empty;
foreach (string readLine in stringlines)
{
line += readLine + "\r\n";
} System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
strValue.Remove(0, strValue.Length);
for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1);
string value = line + strValue.ToString();
sw.WriteLine(value);
sw.Close();
}
else
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8")); for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strColu.Append(dtLogInfo.Columns[i].ColumnName);
strColu.Append(",");
}
strColu.Remove(strColu.Length - 1, 1);
sw.WriteLine(strColu); strValue.Remove(0, strValue.Length);
for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
{
strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1);
sw.WriteLine(strValue.ToString()); sw.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}

  

出力csv的更多相关文章

  1. 【Excel】输出CSV文本

    '******************************************************************************* ' CSV形式テキストファイル書き出す ...

  2. 【Excel】读取CSV文本

    Option Explicit ' CSV形式テキストファイル(5カラム)読み込みサンプル Sub READ_TextFile() Const cnsTITLE = "テキストファイル読み込 ...

  3. mysql 大表拆分成csv导出

    最近公司有一个几千万行的大表需要按照城市的id字段拆分成不同的csv文件. 写了一个自动化的shell脚本 在/home/hdh 下面 linux-xud0:/home/hdh # lltotal 1 ...

  4. Bulk Insert:将文本数据(csv和txt)导入到数据库中

    将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INS ...

  5. [python] CSV read and write using module xlrd and xlwt

    1. get data from csv, skip header of the file. with open('test_data.csv','rb,) as csvfile: readCSV = ...

  6. 生成Tab键或逗号分隔的CSV

    <?php header("Content-type:text/csv;charset=utf-8"); header("Content-Disposition:a ...

  7. [数据科学] 从csv, xls文件中提取数据

    在python语言中,用丰富的函数库来从文件中提取数据,这篇博客讲解怎么从csv, xls文件中得到想要的数据. 点击下载数据文件http://seanlahman.com/files/databas ...

  8. csv表格处理(下)--纯JS解析导入csv

    多日前的上篇介绍了csv表格,以及JS结合后端PHP解析表格填充表单的方法.其中csv转换成二维数组的时候逻辑比较复杂多坑,幸好PHP有丰富的库函数来处理,而现在用JS解析的话就没有那么幸运了,一切都 ...

  9. csv表格处理(上)-- JS 与 PHP 协作导入导出

    CSV简介 在开发后台管理系统的时候,几乎无可避免的会遇到需要导入导出Excel表格的需求.csv也是表格的一种,其中文名为“逗号分隔符文件”.在Excel中打开如下图左边所示,在记事本打开如下图右边 ...

随机推荐

  1. windows7下硬盘安装ubuntu14.04

    windows7 ubuntu1404双系统 准备软件 安装步骤 step 1 step 2 step 3 step 4 windows7 + ubuntu14.04双系统 准备软件 1)grub4d ...

  2. 理解CRC校验

    举个最简单的例子,A向B发送一个数字,B如何检测数字在传输过程中有没有发生错误呢? A和B之间,定下一个协议,两边都知道一个除数X,A向B发送数字的时候,同时把余数附带后面发过去.比如,两边定的除数是 ...

  3. ThinkPHP CURD方法盘点:data方法

    data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值,可能大家不太习惯用这个方法,今天来讲解下如何用好data方法. 用法 写操作 通常情况下我们都是通过create方法或者赋值 ...

  4. 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

    1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...

  5. mysql中删除表

    有两种方式: 1.delete from table table_name; 2.truncate table table_name; 第一种中,清空表后,主键id会在原先的记录基础上继续增加,而第二 ...

  6. 360开源的类Redis存储系统:Pika

    Pika 是 360 DBA 和基础架构组联合开发的类 Redis 存储系统,完全支持 Redis 协议,用户不需要修改任何代码,就可以将服务迁移至 Pika.有维护 Redis 经验的 DBA 维护 ...

  7. 火狐HACK

     /*针对Firefox*/@-moz-document url-prefix() {    #nav{ width:200px; }} 

  8. 如何在mac上創建txt文档

    文件編輯(Mac下的記事本),shift + command + T 進入純文字編輯模式!

  9. 常用免费快递查询API对接案例

    现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...

  10. git 版本控制系统初学

    Git -The stupid content tracker, 傻瓜内容跟踪器,是一个由Linux内核开发者Linus为了更好地管理Linux内核开发而创立的分布式版本控制软件. 1.建立本地git ...