c#自带压缩类实现数据库表导出到CSV压缩文件

系统 2440 0

     

c#自带压缩类实现数据库表导出到CSV压缩文件的方法

在导出大量CSV数据的时候,常常体积较大,采用C#自带的压缩类,可以方便的实现该功能,并且压缩比例很高,该方法在我的开源工具DataPie中已经经过实践检验。我的上一篇博客《 功能齐全、效率一流的免费开源数据库导入导出工具(c#开发,支持SQL server、SQLite、ACCESS三种数据库),每月借此处理数据5G以上 》中有该工具的完整源码,有需要的同学可以看看。

 

     在.net 4.5中,可以轻松创建zip文件 ,首先需要引入 System.IO.Compression.dll、System.IO.Compression.FileSystem.dll两个文件。其中ZipArchive 类表示一些压缩使用 Zip 文件格式的文件。ZipArchiveEntry 类表示单个 ZipArchive。ZipArchive 通常包含一个或多个 ZipArchiveEntry 实例。

 

     DataPie中实现csv文件压缩导出的主要代码如下:

      using System;

using System.Linq;

using System.Text;

using System.IO.Compression;

using System.Data;

using System.Diagnostics;

using System.IO;



namespace DataPie.Core

{

   public class DBToZip

    {





        public static int DataReaderToZip(String zipFileName, IDataReader reader, string tablename)

        {

            Stopwatch watch = Stopwatch.StartNew();

            watch.Start();

            using (FileStream fsOutput = new FileStream(zipFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))

            {

                using (ZipArchive archive = new ZipArchive(fsOutput, ZipArchiveMode.Update))

                {

                    ZipArchiveEntry readmeEntry = archive.CreateEntry(tablename + ".csv");

                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open(), Encoding.UTF8))

                    {

                        for (int i = 0; i < reader.FieldCount; i++)

                        {

                            if (i > 0)

                                writer.Write(',');

                            writer.Write(reader.GetName(i) );

                        }

                        writer.Write(Environment.NewLine);



                        while (reader.Read())

                        {

                            for (int i = 0; i < reader.FieldCount; i++)

                            {

                                if (i > 0)

                                    writer.Write(',');

                                String v = reader[i].ToString();

                                if (v.Contains(',') || v.Contains('\n') || v.Contains('\r') || v.Contains('"'))

                                {

                                    writer.Write('"');

                                    writer.Write(v.Replace("\"", "\"\""));

                                    writer.Write('"');

                                }

                                else

                                {

                                    writer.Write(v);

                                }

                            }

                            writer.Write(Environment.NewLine);

                        }



                    }

                }





            }

            watch.Stop();

            return Convert.ToInt32(watch.ElapsedMilliseconds / 1000);



        }







    }

}
    

 

c#自带压缩类实现数据库表导出到CSV压缩文件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论