出力csv
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的更多相关文章
- 【Excel】输出CSV文本
'******************************************************************************* ' CSV形式テキストファイル書き出す ...
- 【Excel】读取CSV文本
Option Explicit ' CSV形式テキストファイル(5カラム)読み込みサンプル Sub READ_TextFile() Const cnsTITLE = "テキストファイル読み込 ...
- mysql 大表拆分成csv导出
最近公司有一个几千万行的大表需要按照城市的id字段拆分成不同的csv文件. 写了一个自动化的shell脚本 在/home/hdh 下面 linux-xud0:/home/hdh # lltotal 1 ...
- Bulk Insert:将文本数据(csv和txt)导入到数据库中
将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INS ...
- [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 = ...
- 生成Tab键或逗号分隔的CSV
<?php header("Content-type:text/csv;charset=utf-8"); header("Content-Disposition:a ...
- [数据科学] 从csv, xls文件中提取数据
在python语言中,用丰富的函数库来从文件中提取数据,这篇博客讲解怎么从csv, xls文件中得到想要的数据. 点击下载数据文件http://seanlahman.com/files/databas ...
- csv表格处理(下)--纯JS解析导入csv
多日前的上篇介绍了csv表格,以及JS结合后端PHP解析表格填充表单的方法.其中csv转换成二维数组的时候逻辑比较复杂多坑,幸好PHP有丰富的库函数来处理,而现在用JS解析的话就没有那么幸运了,一切都 ...
- csv表格处理(上)-- JS 与 PHP 协作导入导出
CSV简介 在开发后台管理系统的时候,几乎无可避免的会遇到需要导入导出Excel表格的需求.csv也是表格的一种,其中文名为“逗号分隔符文件”.在Excel中打开如下图左边所示,在记事本打开如下图右边 ...
随机推荐
- 从零开始学android开发-IDE空间不够报错
E:\ProSoft\adt-bundle-windows-x86-20140321\eclipse目录下 右键eclipse用记事本打开 可以设置运行的最大的运行空间
- iOS 音频拼接
工作中或许会遇到这样的需求,将两段不同的音频合成一个音频(暂且称之为音频拼接),实现起来相对来说不是很难,再介绍如何拼接之前,先了解下AVFoundation下的几个基本知识点. 基本知识 AVAss ...
- Folder and jar
- (转载)c语言指针学习
前言 近期俄罗斯的陨石.四月的血月.五月北京的飞雪以及天朝各种血腥和混乱,给人一种不详的预感.佛祖说的末法时期,五浊恶世 ,十恶之世,人再无心法约束,道德沦丧,和现在正好吻合.尤其是在天朝,空气,水, ...
- 动态修改 C 语言函数的实现
Objective-C 作为基于 Runtime 的语言,它有非常强大的动态特性,可以在运行期间自省.进行方法调剂.为类增加属性.修改消息转发链路,在代码运行期间通过 Runtime 几乎可以修改 O ...
- IIS 之 启用日志记录
如何为网站启用日志记录或 在 Microsoft Internet Information Services (IIS) 6.0 中,在 IIS 5.0 中,并在 IIS 4.0 中的FTP 站点.可 ...
- Android_Notification
xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- JAVA_JDBC
测试类: 1 import java.util.ArrayList; import java.util.List; /** * 创建数据库: * 1.加载驱动 * Class.forName(&quo ...
- C++的4种编程范型
基于过程procedural-based 基于对象object-based 面向对象object-oriented 泛型技术generics
- Create RCU
BI创建(数据)分析.仪表盘.报表前,都需要对数据进行建模,在oracle biee里称为创建“资料档案库”-该文件后缀为RPD,所以一般也称为创建RPD文件. 步骤: 1.从windows开始菜单里 ...