一个全面且高效的C#帮助类库

一个全面且高效的C#帮助类库

编码文章call10242025-03-06 11:47:5236A+A-

我们致力于探索、分享和推荐最新的实用技术栈、开源项目、框架和实用工具。每天都有新鲜的开源资讯等待你的发现!


项目介绍

Common.Utility是一个C#编写的实用工具库,它收集并整理了大量的辅助类,旨在提供一系列方便开发者在.NET环境中使用的功能。

日常工作收集,各式各样的几乎都能找到,所有功能性代码都是独立的类,类与类之间没有联系,可以单独引用至项目。

功能介绍

CSVHelper

 using Quartz.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;

namespace DotNet.Utilities.CSVHelper
{
public class CSVHelper
{

///
/// CSV转换成DataTable(OleDb数据库访问方式)
///

/// <param name="csvPath">csv文件路径
///
public static DataTable CSVToDataTableByOledb(string csvPath)
{
DataTable csvdt = new DataTable("csv");
if (!File.Exists(csvPath))
{
throw new FileNotFoundException("csv文件路径不存在!");
}

FileInfo fileInfo = new FileInfo(csvPath);
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileInfo.DirectoryName + ";Extended Properties='Text;'"))
{
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + fileInfo.Name + "]", conn);
adapter.Fill(csvdt);
}
return csvdt;
}

///
/// CSV转换成DataTable(文件流方式)
///

/// <param name="csvPath">csv文件路径
///
public static DataTable CSVToDataTableByStreamReader(string csvPath)
{
DataTable csvdt = new DataTable("csv");

int intColCount = 0;
bool blnFlag = true;
DataColumn column;
DataRow row;
string strline = ;
string[] aryline;
Encoding encoding=;

using (StreamReader reader = new StreamReader(csvPath, encoding))
{
while (!string.IsOrEmpty((strline = reader.ReadLine())))
{
aryline = strline.Split(new char[] { ',' });

if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
column = new DataColumn(aryline[i]);
csvdt.Columns.Add(column);
}
continue;
}

row = csvdt.NewRow();
for (int i = 0; i < intColCount; i++)
{
row[i] = aryline[i];
}
csvdt.Rows.Add(row);
}
}

return csvdt;
}

///
/// DataTable 生成 CSV
///

/// <param name="dt">DataTable
/// <param name="csvPath">csv文件路径
public static void DataTableToCSV(DataTable dt, string csvPath)
{
if ( == dt)
return;

StringBuilder csvText = new StringBuilder();
StringBuilder csvrowText = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
{
csvrowText.Append(",");
csvrowText.Append(dc.ColumnName);
}
csvText.AppendLine(csvrowText.ToString().Substring(1));

foreach (DataRow dr in dt.Rows)
{
csvrowText = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
{
csvrowText.Append(",");
csvrowText.Append(dr[dc.ColumnName].ToString().Replace(',', ' '));
}
csvText.AppendLine(csvrowText.ToString().Substring(1));
}

File.WriteAllText(csvPath, csvText.ToString(), Encoding.Default);
}
}
}

DataTable转实体

 public static class DataTableExtensions
{
public static T ToEntity(this DataTable table) where T : new()
{
T entity = new T();
foreach (DataRow row in table.Rows)
{
foreach (var item in entity.GetType().GetProperties())
{
if (row.Table.Columns.Contains(item.Name))
{
if (DB.Value != row[item.Name])
{
Type newType = item.PropertyType;
//判断type类型是否为泛型,因为able是泛型类,
if (newType.IsGenericType
&& newType.GetGenericTypeDefinition().Equals(typeof(able<>)))//判断convertsionType是否为able泛型类
{
//如果type为able类,声明一个ableConverter类,该类提供从able类到基础基元类型的转换
System.ComponentModel.ableConverter ableConverter = new System.ComponentModel.ableConverter(newType);
//将type转换为able对的基础基元类型
newType = ableConverter.UnderlyingType;
}

item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), );

}

}
}
}

return entity;
}

public static List ToEntities(this DataTable table) where T : new()
{
List entities = new List();
if (table == )
return ;
foreach (DataRow row in table.Rows)
{
T entity = new T();
foreach (var item in entity.GetType().GetProperties())
{
if (table.Columns.Contains(item.Name))
{
if (DB.Value != row[item.Name])
{
Type newType = item.PropertyType;
//判断type类型是否为泛型,因为able是泛型类,
if (newType.IsGenericType
&& newType.GetGenericTypeDefinition().Equals(typeof(able<>)))//判断convertsionType是否为able泛型类
{
//如果type为able类,声明一个ableConverter类,该类提供从able类到基础基元类型的转换
System.ComponentModel.ableConverter ableConverter = new System.ComponentModel.ableConverter(newType);
//将type转换为able对的基础基元类型
newType = ableConverter.UnderlyingType;
}
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), );
}
}
}
entities.Add(entity);
}
return entities;
}
}

开源地址

https://github.com/laochiangx/Common.Utility

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4