/// <summary> /// 通过反射机制,获取类对象中的某个属性的值 /// </summary> /// <param name="columnName">属性</param> /// <param name="obj">对象</param> /// <returns></returns> public static string GetModelValue(object obj, string columnName) { try { Type Ts = obj.GetType(); string propertyName = MyStringUtil.getCamelName(columnName); System.Reflection.PropertyInfo propertyInfo = Ts.GetProperty(propertyName); if (propertyInfo != null) { object o = propertyInfo.GetValue(obj, null); string Value = Convert.ToString(o); if (string.IsNullOrEmpty(Value)) { return(null); } return(Value); } else { return(null); } } catch { return(null); } }
public void ExportDataToCVS(String fileName, System.Data.DataTable dt) { if (MyStringUtil.isEmpty(fileName)) { return; } if (dt == null || dt.Rows.Count <= 0) { return; } StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312")); StringBuilder sb = new StringBuilder(); for (int i = 0; i < dt.Columns.Count; i++) { sb.Append(dt.Columns[i].ColumnName.ToString() + ","); } sb.Append(Environment.NewLine); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { sb.Append(dt.Rows[i][j].ToString() + ","); } sb.Append(Environment.NewLine);//每写一行数据后换行 } sw.Write(sb.ToString()); sw.Flush(); sw.Close();//释放资源 }
public static bool SetModelValue(object obj, string columnName, string Value) { try { Type Ts = obj.GetType(); string propertyName = MyStringUtil.getCamelName(columnName); System.Reflection.PropertyInfo propertyInfo = Ts.GetProperty(propertyName); if (propertyInfo != null) { object v = Convert.ChangeType(Value, propertyInfo.PropertyType); propertyInfo.SetValue(obj, v, null); return(true); } else { return(false); } } catch { return(false); } }
public static DataSet ExcelToDS(String sheetName, String condition) { DataSet ds = null; OleDbConnection conn = null; try { conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; strExcel = "select * from [" + sheetName + "$]"; if (!MyStringUtil.isEmpty(condition)) { strExcel = strExcel + " where " + condition; } myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); conn.Close(); } catch (Exception e) { Log.Write(e.Message); } finally { if (conn != null && ConnectionState.Open == conn.State) { conn.Close(); } } return(ds); }
/// <summary> /// 获取某个数据表的列名及中文名 /// </summary> /// <param name="tableName"></param> /// <returns></returns> public Dictionary <String, String> getColumns(string tableName) { Dictionary <String, String> result = new Dictionary <String, String>(); string queryString = "select * from ts_table_label where TABLE_NAME='" + tableName + "'"; DataTable dataTable = ExecuteSelect(queryString); foreach (DataRow mDr in dataTable.Rows) { string columnName = (mDr["COL_NAME"].Equals(DBNull.Value)) ? "" : (String)mDr["COL_NAME"]; string columnLabel = (mDr["COL_LABEL"].Equals(DBNull.Value)) ? "" : (String)mDr["COL_LABEL"]; if (!MyStringUtil.isEmpty(columnName)) { if (MyStringUtil.isEmpty(columnLabel)) { columnLabel = columnName; } result.Add(columnName, columnLabel); } } return(result); }