123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Uninpho.DBOperation.Model;
- namespace Uninpho.Tools.components
- {
-
-
-
- static class CsvToListData
- {
- public static List<T_xxxw_csvdata> Listdata_csvdata { get; set; }
- public static List<T_xxxw_csvlist> Listdata_csvlist { get; set; }
-
-
-
- public static void CsvToDataTable(TableName tablename)
- {
- OpenFileDialog fd = new OpenFileDialog();
- fd.Filter = "表格|*.csv";
- string strPath;
- if (fd.ShowDialog() == DialogResult.OK)
- {
- try
- {
- strPath = fd.FileName;
- DataTable datatable = new DataTable();
- FileStream fileStream = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.Read);
- StreamReader streamReader = new StreamReader(strPath, Encoding.GetEncoding("gb2312"));
- string strLine = "";
- string[] arrayLine = null;
- string[] tableHead = null;
- int columnCount = 0;
- bool IsFrist = true;
- while ((strLine = streamReader.ReadLine()) != null)
- {
- if (IsFrist == true)
- {
- tableHead = strLine.Split(',');
- IsFrist = false;
- columnCount = tableHead.Length;
-
- for (int i = 0; i < columnCount; i++)
- {
- DataColumn dataColumn = new DataColumn(tableHead[i]);
- datatable.Columns.Add(dataColumn);
- }
- }
- else
- {
- arrayLine = strLine.Split(',');
- DataRow dataRow = datatable.NewRow();
- for (int j = 0; j < columnCount; j++)
- {
- dataRow[j] = arrayLine[j];
- }
- datatable.Rows.Add(dataRow);
- }
- }
- if (arrayLine != null && arrayLine.Length > 0)
- {
- datatable.DefaultView.Sort = tableHead[0];
- }
- streamReader.Close();
- streamReader.Dispose();
- fileStream.Close();
- fileStream.Dispose();
-
-
- switch (tablename)
- {
- case TableName.T_xxxw_csvdata:
- Listdata_csvdata = DataTableToList<T_xxxw_csvdata>(datatable, TableName.T_xxxw_csvdata);
-
- foreach (var item in Listdata_csvdata)
- {
- string uuid = System.Guid.NewGuid().ToString("N");
- item.Id = uuid;
- }
- break;
- case TableName.T_xxxw_csvlist:
- Listdata_csvlist = DataTableToList<T_xxxw_csvlist>(datatable, TableName.T_xxxw_csvlist);
-
- foreach (var item in Listdata_csvlist)
- {
- string uuid = System.Guid.NewGuid().ToString("N");
- item.Id = uuid;
- }
- break;
- default: break;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
-
-
-
-
-
-
- private static List<T> DataTableToList<T>(DataTable dt, TableName tablename) where T : class, new()
- {
-
- Assist_xxxwcsvdata assist_xxxwcsvdata = new Assist_xxxwcsvdata();
- Assist_xxxwcsvlist assist_xxxwcsvlist = new Assist_xxxwcsvlist();
- if (dt == null || dt.Rows.Count == 0)
- return null;
- List<T> result = new List<T>();
- foreach (DataRow dr in dt.Rows)
- {
- try
- {
- T res = new T();
- for (int i = 0; i < dr.Table.Columns.Count; i++)
- {
- PropertyInfo propertyInfo = null;
- switch (tablename)
- {
- case TableName.T_xxxw_csvdata:
- propertyInfo = res.GetType().GetProperty(GetTypeValue(dr.Table.Columns[i].ColumnName, assist_xxxwcsvdata), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
- break;
- case TableName.T_xxxw_csvlist:
- propertyInfo = res.GetType().GetProperty(GetTypeValue(dr.Table.Columns[i].ColumnName, assist_xxxwcsvlist), BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
- break;
- default: break;
- }
- if (propertyInfo != null)
- {
- var value = dr[i];
- switch (propertyInfo.PropertyType.FullName)
- {
- case "System.Decimal":
- if (value == DBNull.Value) { propertyInfo.SetValue(res, 0.0, null); break; }
- propertyInfo.SetValue(res, Convert.ToDecimal(value), null); break;
- case "System.String":
- if (value == DBNull.Value) { propertyInfo.SetValue(res, "-", null); break; }
- propertyInfo.SetValue(res, value.ToString(), null); break;
- case "System.Int32":
- if (value == DBNull.Value) { propertyInfo.SetValue(res, 0, null); break; }
- propertyInfo.SetValue(res, Convert.ToInt32(value), null); break;
- case "System.Double":
- if (value == DBNull.Value) { propertyInfo.SetValue(res, 0.0, null); break; }
- propertyInfo.SetValue(res, Convert.ToDouble(value), null); break;
- default:
- propertyInfo.SetValue(res, value, null); break;
- }
- }
- }
- result.Add(res);
- }
- catch (Exception ex)
- {
- ex = new Exception(string.Format("请检查第{0}行数据", result.Count + 2));
- throw ex;
- }
- }
- return result;
- }
-
-
-
-
-
-
-
- private static string GetTypeValue<T>(string s, T t)
- {
- string columnName = "";
- Type assistType = t.GetType();
- PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
- for (int i = 0; i < assistType.GetFields().Length; i++)
- {
- if (assistType.GetFields()[i] != null)
- {
- if (assistType.GetFields()[i].GetValue(t) != null && assistType.GetFields()[i].GetValue(t).ToString() == s)
- {
- columnName = assistType.GetFields()[i].Name;
-
- break;
- }
- }
- }
- return columnName;
- }
- }
- }
|