public int InDataFormExcel(Guid programID, string table, string file, out string msg, string numberFiled = "") { int num = 0; msg = ""; if (table.IsNullOrEmpty()) { msg = "没有选择表"; return(num); } DBConnection dbConnection = new DBConnection(); RoadFlow.Data.Model.ProgramBuilder programBuilder = this.Get(programID); if (programBuilder == null) { msg = "未找到应用程序设计"; return(num); } RoadFlow.Data.Model.DBConnection conn = dbConnection.Get(programBuilder.DBConnID, true); if (conn == null) { msg = "未找到相应的数据库连接"; return(num); } List <RoadFlow.Data.Model.ProgramBuilderFields> all = new ProgramBuilderFields().GetAll(programID); if (all.Count == 0) { msg = "应用程序未设置列表字段"; return(num); } try { DataTable dataTable = NPOIHelper.ReadToDataTable(file, 1); if (dataTable.Rows.Count == 0) { msg = "未发现要导入的数据"; return(num); } List <string> fieldsBySql = dbConnection.GetFieldsBySQL(programBuilder.DBConnID, "select * from " + table + " where 1=0"); DataTable dt = new DataTable(table); string str = DateTimeNew.Now.ToString("yyyyMMddHHmmssfffff"); foreach (RoadFlow.Data.Model.ProgramBuilderFields programBuilderFields in all) { RoadFlow.Data.Model.ProgramBuilderFields filed = programBuilderFields; if (!filed.Field.IsNullOrEmpty() && fieldsBySql.Find((Predicate <string>)(p => p.Equals(filed.Field, StringComparison.CurrentCultureIgnoreCase))) != null) { dt.Columns.Add(filed.Field); } } if (!numberFiled.IsNullOrEmpty()) { dt.Columns.Add(numberFiled); } foreach (DataRow row1 in (InternalDataCollectionBase)dataTable.Rows) { DataRow row2 = dt.NewRow(); foreach (DataColumn column in (InternalDataCollectionBase)dt.Columns) { DataColumn col = column; RoadFlow.Data.Model.ProgramBuilderFields programBuilderFields = all.Find((Predicate <RoadFlow.Data.Model.ProgramBuilderFields>)(p => p.Field.Equals(col.ColumnName))); if (programBuilderFields != null) { row2[col.ColumnName] = row1[programBuilderFields.ShowTitle]; } } if (!numberFiled.IsNullOrEmpty()) { row2[numberFiled] = (object)str; } dt.Rows.Add(row2); } num = dbConnection.DataTableToDB(conn, dt); Log.Add("通过应用程序导入了数据-表(" + table + ")标识(" + str + ")", file, Log.Types.其它分类, "", "", (RoadFlow.Data.Model.Users)null); return(num); } catch (Exception ex) { msg = ex.Message; return(num); } }
public int InDataFormExcel(Guid programID, string table, string file, out string msg, string numberFiled = "") { int result = 0; msg = ""; if (table.IsNullOrEmpty()) { msg = "没有选择表"; return(result); } DBConnection dBConnection = new DBConnection(); RoadFlow.Data.Model.ProgramBuilder programBuilder = Get(programID); if (programBuilder == null) { msg = "未找到应用程序设计"; return(result); } RoadFlow.Data.Model.DBConnection dBConnection2 = dBConnection.Get(programBuilder.DBConnID); if (dBConnection2 == null) { msg = "未找到相应的数据库连接"; return(result); } List <RoadFlow.Data.Model.ProgramBuilderFields> all = new ProgramBuilderFields().GetAll(programID); if (all.Count != 0) { try { DataTable dataTable = NPOIHelper.ReadToDataTable(file); if (dataTable.Rows.Count == 0) { msg = "未发现要导入的数据"; return(result); } List <string> fieldsBySQL = dBConnection.GetFieldsBySQL(programBuilder.DBConnID, "select * from " + table + " where 1=0"); DataTable dataTable2 = new DataTable(table); string text = DateTimeNew.Now.ToString("yyyyMMddHHmmssfffff"); foreach (RoadFlow.Data.Model.ProgramBuilderFields item in all) { if (!item.Field.IsNullOrEmpty() && fieldsBySQL.Find((string p) => p.Equals(item.Field, StringComparison.CurrentCultureIgnoreCase)) != null) { dataTable2.Columns.Add(item.Field); } } if (!numberFiled.IsNullOrEmpty()) { dataTable2.Columns.Add(numberFiled); } foreach (DataRow row in dataTable.Rows) { DataRow dataRow2 = dataTable2.NewRow(); foreach (DataColumn column in dataTable2.Columns) { RoadFlow.Data.Model.ProgramBuilderFields programBuilderFields = all.Find((RoadFlow.Data.Model.ProgramBuilderFields p) => p.Field.Equals(column.ColumnName)); if (programBuilderFields != null) { dataRow2[column.ColumnName] = row[programBuilderFields.ShowTitle]; } } if (!numberFiled.IsNullOrEmpty()) { dataRow2[numberFiled] = text; } dataTable2.Rows.Add(dataRow2); } result = dBConnection.DataTableToDB(dBConnection2, dataTable2); Log.Add("通过应用程序导入了数据-表(" + table + ")标识(" + text + ")", file); return(result); } catch (Exception ex) { msg = ex.Message; return(result); } } msg = "应用程序未设置列表字段"; return(result); }