DataReader 是游标只读数据, 如果是大数据导出,用Datatable 将耗费巨大内存资源。因为Datatable 其实就是内存中的一个数据表

代码如下

/// <summary>
/// SqlDataReader 生成csv文件
/// </summary>
/// <param name="fileNameCsv">文件名(包含文件路径)</param>
/// <param name="dr">数据表</param>
/// <param name="hideColumnNames">要隐藏的列名</param>
/// <param name="encoding">编码【默认:GB2312】</param>
/// <returns></returns>
protected static void DownloadCsv(string fileNameCsv, string fileNameZip, SqlDataReader dr, string[] hideColumnNames, string encoding="gb2312")
{
if (dr != null)
{
try
{
Dictionary<string, string> hideCol = new Dictionary<string, string>();
foreach (string item in hideColumnNames)
{
hideCol.Add(item.Trim(), item);
} if (!File.Exists(fileNameCsv))
{
using (StreamWriter sw = new StreamWriter(fileNameCsv, false, Encoding.GetEncoding(encoding)))
{
string fieldName = string.Empty;
StringBuilder sb = new StringBuilder();
//写入表头
for (int i = 0; i < dr.FieldCount; i++)
{
fieldName = dr.GetName(i);
if (hideCol.ContainsKey(fieldName) == true || fieldName == "")
continue;
else
{
sb.Append(fieldName);
sb.Append(","); }
}
sw.WriteLine(sb.ToString().TrimEnd(','));
sb.Clear();
//写入导出数据
while (dr.Read())
{
//遍历每一列
for (int i = 0; i < dr.FieldCount; i++)
{
fieldName = dr.GetName(i);
if (hideCol.ContainsKey(fieldName) == true || fieldName == "")
continue;
else
{
if (!Convert.IsDBNull(dr[i]))
{
string content = string.Format("\"{0}\"", dr[i].ToString().Replace("\"", "\"\""));
sb.Append(content);
}
else
{
sb.Append("\"\"");
}
sb.Append(","); }
}
sw.WriteLine(sb.ToString().TrimEnd(','));
sb.Clear();//每行数据结束清空已经写入文本的数据
}
}
}
dr.Close();
string filePath = fileNameCsv.Substring(0, fileNameCsv.LastIndexOf(@"\")+1);//+1 包含最后一个斜杠
CreateZipFile(filePath, fileNameZip);//创建压缩文件
}
catch(Exception ex)
{
if(!dr.IsClosed)
{
dr.Close();
}
LogHelper.WriteErrorLog("ProcessDocument.DownloadCsv", ex.Message);
}
}
}

  

原创 Datareader 导出为csv文件方法的更多相关文章

  1. oracle导出多CSV文件的靠谱的

    oracle导出多CSV文件的问题 ---------------------------------------------------------------------- 用ksh脚本从orac ...

  2. java导出生成csv文件

    首先我们需要对csv文件有基础的认识,csv文件类似excel,可以使用excel打开,但是csv文件的本质是逗号分隔的,对比如下图: txt中显示: 修改文件后缀为csv后显示如下: 在java中我 ...

  3. 淘宝助理导出的csv文件使用的是什么编码,您猜?

    今天下午用Java读取从淘宝助理 V4.3 Beta1导出的csv文件,出现中文乱码情况. 一看就是文件编码引起的,不清楚淘宝助理导出的csv文件使用了什么编码,到百度搜索了一下,看到一些相关文章,但 ...

  4. R: 导入 csv 文件,导出到csv文件,;绘图后导出为图片、pdf等

    ################################################### 问题:导入 csv 文件 如何从csv文件中导入数据,?参数怎么设置?常用参数模板是啥? 解决方 ...

  5. 如何从sql server导出到csv文件

    如何从sql server导出到csv文件,具体代码如下: private static void WriteHeader(SqlDataReader reader, TextWriter outpu ...

  6. 导出excel、word、csv文件方法汇总

    http://www.woaic.com/2012/06/64 excel文件主要是输出html代码.以xls的文本格式保存文件. 生成excel格式的代码: /// <summary> ...

  7. es实战之数据导出成csv文件

    从es将数据导出分两步: 查询大量数据 将数据生成文件并下载 本篇主要是将第二步,第一步在<es实战之查询大量数据>中已讲述. csv vs excel excel2003不能超过6553 ...

  8. Java列表分页查询结果导出到CSV文件,导入CSV文件并解析

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  9. 导出到CSV文件

    一.手工导出导出 1.winform void DataGridViewToExcel(DataGridView dataGridView1) { SaveFileDialog saveFileDia ...

随机推荐

  1. EF测试自动修改数据库结构

    1.配置类 <connectionStrings> <add name="test1" connectionString="Data Source=.; ...

  2. Android SVN的配置

    关于svn有不错的图文博文,先po一个个人认为很不错的博文:http://ask.android-studio.org/?/article/97 接下来呢呢就是以我个人的思维模式来讲解svn啦: 当然 ...

  3. MongoDB学习:(一)MongoDB安装

    MongoDB学习:(一)MongoDB安装 MongoDB介绍:     直接百科了: MongoDB安装: 1:下载安装: MongoDB安装:https://www.mongodb.com/do ...

  4. python 模拟登录--day1

    ---------------edit-in-windows-by-pycharm---------------------import randomuname = "mologa" ...

  5. 关于C# DataTable 的一些操作

    经常操作DATATABLE  对于一些不需要再通过sql 来重复操作的   可以通过操作datatable来达到同样的效果 方法一: 也是广为人知的一种: YourDataTable.Columns. ...

  6. Java程序调用javascript等脚本的实现方法

    public static void main(String[] args) throws FileNotFoundException, ScriptException, NoSuchMethodEx ...

  7. 移动开发tip

    input点击出现背景色和边框,加入样式 -webkit-tap-highlight-color: rgba(255,255,255,0); ios下按钮糊掉,样式表不怎么起作用,使用-webkit- ...

  8. NSBundle控件和UIImageView和UIButton区别

    1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 模拟器应 ...

  9. js获取新浪天气接口

    <!doctype html> <html class="no-js fixed-layout"> <head> <meta charse ...

  10. cod-hw

    COD hw 4 Xinglu Wang 3140102282 2016-12-27 21:28:01 COD hw 45.35.3.15.3.35.3.45.3.55.3.65.45.4.15.4. ...