一个全面且高效的C

程序员有二十年 2024-09-09 12:14:42

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

项目介绍

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 CSVHelper { /// <summary> /// CSV转换成DataTable(OleDb数据库访问方式) /// </summary> /// <param name="csvPath">csv文件路径</param> /// <returns></returns> 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; } /// <summary> /// CSV转换成DataTable(文件流方式) /// </summary> /// <param name="csvPath">csv文件路径</param> /// <returns></returns> 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; } /// <summary> /// DataTable 生成 CSV /// </summary> /// <param name="dt">DataTable</param> /// <param name="csvPath">csv文件路径</param> 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 DataTableExtensions { public static T ToEntity<T>(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<T> ToEntities<T>(this DataTable table) where T : new() { List<T> entities = new List<T>();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

0 阅读:0

程序员有二十年

简介:感谢大家的关注