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. iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效

    ViewDidLoad中注册手势的部分代码如下: [cpp] view plaincopy UITapGestureRecognizer *oneTap = [[[UITapGestureRecogn ...

  2. EasyARM i.mx28学习笔记——开箱试用总结

    0 前言     本月初(2014年8月)购买了周立功的EasyARM开发板,主控为EasyARM i.mx287.出于下面几个理由购买了该开发板.     [1]主要原因,有人约我一起学习一起使用该 ...

  3. github fork项目后,代码更新

    协助约定 每个人都可以fork一份自己的repo,所有的修改都在自己私有的repo上进行:修改完成,测试通过后通过给主repo发pull request请求合并:主repo(Johnqing/n.js ...

  4. js 数组排序要注意的问题,返回的值最好为 -1, 0, 1之间的值

    var test10Elements = [7, 6, 5, 4, 3, 2, 1, 0, 8, 9]; var comparefn = function (x, y) { return x - y; ...

  5. 应聘.net开发工程师常见的面试题(四)

    1.在Asp.net中所有的自定义用户控件都必须继承自________? 答:Control. 2.在.Net中所有可序列化的类都被标记为_____? 答:[serializable] 3.在.Net ...

  6. java_jdbc_可滚动结果集与分页

    public static void create2(int i) { Connection conn = null; Statement st = null; ResultSet rs = null ...

  7. extremeComponents(ec)源码分析

    eXtremeComponents(简称ec)是一系列提供高级显示的开源JSP定制标签,当前的包含的组件为eXtremeTable,用于以表形式显示数据. 其本质是jsp的自定义标签,抓住这一点就抓住 ...

  8. Lodash Filter

    var persons = [{name:'1',age:'20'}, {name:'2', age:'25'}];_.filter(persons, {'age': '25'}); //return ...

  9. linux下安装软件后的环境变量设置

    /home/username/.bashrc文件中加入如下内容 export LM_LICENSE_FILE=/home/program_files/modeltech_10.2c/modeltech ...

  10. hasLayout与Block formatting contexts的学习(上)

    hasLayout与Block formatting contexts的学习 @(css BFC)[IE hasLayout|妙瞳] hasLayout是什么? haslayout 是Windows ...