码迷,mamicode.com
首页 > Windows程序 > 详细

C# CSV Generic T

时间:2019-01-31 23:41:11      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:name   cti   int   reading   type   alt   order   line   one   

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

标签:name   cti   int   reading   type   alt   order   line   one   

原文地址:https://www.cnblogs.com/Fred1987/p/10344396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!