C# CSV Generic T
This artice will write the main step to export generic data via csv with complete code and step by step.
1.Down load EntityFramework
Install-package entityframework -v 6.2.0
2.Add EF data model with only one table for simplicity.Take the AdventureWorks2017.Sales.SalesOrderDetail for example.
3.Override the ToString() method of the newly added data model cs file.
public override string ToString()
{
string formatMsg = SalesOrderID + "," + SalesOrderDetailID + "," + CarrierTrackingNumber + "," +
OrderQty + "," + ProductID + "," + SpecialOfferID + "," + UnitPrice + "," + UnitPriceDiscount + "," +
LineTotal + "," + rowguid + "," + ModifiedDate + Environment.NewLine;
return formatMsg;
}
4.To be exact and precise,I will add StopWatch to log the cost in milliseconds.
The full code as below.
So far in my own pc, I can test as much as 3.8 million rows data and cost about 25 seconds as the screenshot illustrated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
namespace ConsoleApp322
{
class Program
{
static string cvsFileFullName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".csv";
static string fullLogFileName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + "log.txt";
static Stopwatch stopWatch = new Stopwatch();
static int exportNumber = 0;
static void Main(string[] args)
{
ExportTToCVS();
}
static void ExportTToCVS()
{
using (AdventureWorks2017Entities db = new AdventureWorks2017Entities())
{
List<SalesOrderDetail> orderList = db.SalesOrderDetails.ToList();
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
orderList.AddRange(orderList);
ExportListTToCVS<SalesOrderDetail>(orderList);
}
}
static void ExportListTToCVS<T>(List<T> dataList)
{
if(dataList!=null && dataList.Any())
{
stopWatch.Start();
exportNumber = dataList.Count;
StringBuilder orderBuilder = new StringBuilder();
var firstRowData = dataList.FirstOrDefault();
var orderProps = firstRowData.GetType().GetProperties().ToList();
orderBuilder.AppendLine(string.Join(",", orderProps.Select(x=>x.Name).ToArray()));
foreach(var dl in dataList)
{
orderBuilder.Append(dl.ToString());
}
using (StreamWriter streamWriter = new StreamWriter(cvsFileFullName))
{
streamWriter.WriteLine(orderBuilder.ToString());
}
stopWatch.Stop();
Process proc = Process.GetCurrentProcess();
long exportMemory = proc.PrivateMemorySize64;
string exportMsg = $"File path:{cvsFileFullName},export number:{exportNumber}," +
$"cost: {stopWatch.ElapsedMilliseconds} milliseconds,memory:{exportMemory}";
File.AppendAllText(fullLogFileName, exportMsg + Environment.NewLine);
}
}
}
}
C# CSV Generic T的更多相关文章
- C#, CSV,Generic, 泛型,导出
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- .net如何向csv添加一列
using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...
- Reading and Writing CSV Files in C#
Introduction A common requirement is to have applications share data with other programs. Although t ...
- 根据日期 读取三个csv不留指定日期的内容 新保存一个文件
using System;using System.Collections.Generic;using System.Drawing;using System.Globalization;using ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- C# csv 操作类
using System.Data; using System.IO; using System.Text; namespace YanZhiwei.DotNet2.Utilities.Common ...
- c# 简单文件流读写CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- 【C#】用C#通过读取数据库方式读取CSV文件
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- 快速读取csv平面文件,并导入数据库,简单小工具
using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using Syst ...
随机推荐
- Dynamics CRM 2015/2016新特性之七:有了文档模板,打印分析So Easy
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复190或者20160216可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 从CRM 2015 UR1开始, ...
- ABP进阶教程2 - 组合查询
点这里进入ABP进阶教程目录 更新数据传输对象 打开应用层(即JD.CRS.Application)的Course\Dto\GetAllCoursesInput.cs //Course数据传输对象(查 ...
- 【Servlet】JavaWeb应用的执行流程
Tomcat与Servlet简介 Tomcat Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.S ...
- python 逐行读取txt文件
逐行读取txt文件 path = r'D:\123456\1.txt'with open(path, 'r', encoding='utf-8') as f: for line in f: ...
- Lustre 文件系统安装
制作一个本地镜像 reposync configfile: [root@localhost html]# cat lustre-repo.conf [lustre-server] name=lustr ...
- [视频教程] 最新版swoole安装和TASKS功能测试
今天我们来安装和测试一下php的多并发高性能网络通信扩展,这个扩展是使用C语音开发的,加载到PHP以后,在PHP的层面上实现了多并发异步通信,模拟了go语音的很多特性,极大的拓宽了PHP的应用场景. ...
- RC4 对称加密
public class RC4 { byte[] s = new byte[256]; byte[] key; byte keylen;// 4 ~ 16 int pi = 0; int pj = ...
- HashMap了解吗?
HashCode() HashMap 底层实现 HashMap 的长度为什么默认初始长度是16,并且每次resize()的时候,长度必须是2的幂次方? HashMap 死链问题 Java 8 与 Ja ...
- Vue生命周期钩子---2
vue生命周期简介 咱们从上图可以很明显的看出现在vue2.0都包括了哪些生命周期的函数了. 生命周期探究 对于执行顺序和什么时候执行,看上面两个图基本有个了解了.下面我们将结合代码去看看钩子函数的执 ...
- jwt揭秘(含源码示例和视频)
JSON Web Tokens,是一种开发的行业标准 RFC 7519 ,用于安全的表示双方之间的声明.目前,jwt广泛应用在系统的用户认证方面,特别是现在前后端分离项目. 1. jwt认证流程 在项 ...