Skip to content

Commit 8dd8a94

Browse files
committed
Add ListToCSV Class
Add ListToCSV Class
1 parent 78fa45f commit 8dd8a94

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CsharpLibs/CsharpLibs.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
3535
<HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
3636
</Reference>
37+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net40\Newtonsoft.Json.dll</HintPath>
39+
</Reference>
3740
<Reference Include="NPOI, Version=2.4.1.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
3841
<HintPath>..\packages\NPOI.2.4.1\lib\net40\NPOI.dll</HintPath>
3942
</Reference>
@@ -84,6 +87,7 @@
8487
<Compile Include="DataGridViewPrint.cs" />
8588
<Compile Include="DESEncrptor.cs" />
8689
<Compile Include="HttpHelper.cs" />
90+
<Compile Include="ListToCSV.cs" />
8791
<Compile Include="Loger.cs" />
8892
<Compile Include="Loger2.cs" />
8993
<Compile Include="MessageHelperWinform.cs" />

CsharpLibs/ListToCsv.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace DEVGIS.CsharpLibs
8+
{
9+
/// <summary>
10+
///
11+
/// </summary>
12+
public class ListToCSV
13+
{
14+
public static string ExportToCSV<T>(List<T> list)
15+
{
16+
if (list == null || list.Count <= 0)
17+
{
18+
throw (new Exception("List is null or empty!"));
19+
}
20+
StringBuilder sb = new StringBuilder();
21+
string header = string.Empty;
22+
var properties = typeof(T).GetProperties();
23+
if (properties == null || properties.Length <= 0)
24+
{
25+
throw (new Exception("Properties is null!"));
26+
}
27+
for (int i = 0; i < properties.Length; i++)
28+
{
29+
if (i == 0)
30+
{
31+
header += properties[i].Name;
32+
}
33+
else
34+
{
35+
header += "," + properties[i].Name;
36+
}
37+
sb.AppendLine(header);
38+
}
39+
foreach (var item in list)
40+
{
41+
string rowdata = string.Empty;
42+
for (int i = 0; i < properties.Length; i++)
43+
{
44+
try
45+
{
46+
var value = JsonConvert.SerializeObject(properties[i].GetValue(item, null));
47+
value = value.TrimStart('[').TrimEnd(']').Replace(",", ";");
48+
if (i == 0)
49+
{
50+
rowdata += value;
51+
}
52+
else
53+
{
54+
rowdata += "," + value;
55+
}
56+
}
57+
catch (Exception ex)
58+
{
59+
if (i == 0)
60+
{
61+
rowdata += string.Empty;
62+
}
63+
else
64+
{
65+
rowdata += ","+string.Empty;
66+
}
67+
}
68+
}
69+
sb.AppendLine(rowdata);
70+
}
71+
return sb.ToString();
72+
}
73+
}
74+
}

CsharpLibs/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
4+
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net40" />
45
<package id="NPOI" version="2.4.1" targetFramework="net40" />
56
<package id="Oracle.ManagedDataAccess" version="18.6.0" targetFramework="net40" />
67
<package id="SharpZipLib" version="0.86.0" targetFramework="net40" />

0 commit comments

Comments
 (0)