ext.net GridPanelHelper
<pre name="code" class="csharp">/// <summary> ///GridPanelHelper
<span style="font-family: Arial, Helvetica, sans-serif;">///date:2014.7.22 by:wyl</span>
///GridPanel在我们系统中保存的是子表信息 /// </summary> public class GridPanelHelper { /// <summary> /// gridpanel 单元格双击事件 /// </summary> public const string fn = "function(e){e.cancel=true;return false;}"; #region 保存GridPanel /// <summary> /// 保存gridpanel子表数据 /// </summary> /// <param name="jsonData">子表Store的json数据</param> /// <param name="store">子表store(Ext.Net.Store)</param> /// <param name="tableName">子表名称</param> /// <param name="parentID">父表记录的ID</param> public static int SaveGridData(string jsonData, Ext.Net.Store store, string tableName, int parentID) { //反序列化得到JArray数组 JArray ja = (JArray)JsonConvert.DeserializeObject(jsonData); if (ja.Count <= 0) { return 0; } RecordFieldCollection RecordField = store.Reader[0].Fields; string parentField = RecordField[0].Name; //父表记录的ID string fields = parentField;//字段列表 string values = ""; //插入值列表 string sqlStr = ""; string selSql = ""; for (int i = 0; i < ja.Count; i++) //行循环 { values = "'" + parentID.ToString() + "'"; //父表ID取参数传过来的,而不是json中的 for (int j = 1; j < RecordField.Count; j++) //列循环 { if (i == 0) //只执行一次最外层循环 { fields += "," + RecordField[j].Name; } //date:2014.7.22 by:wyl if (ja[i][RecordField[j].Name] == null) { string str = " "; values += "," + "'" + str + "'"; } else { values += "," + "'" + ja[i][RecordField[j].Name].ToString() + "'"; } } selSql += "select " + values + " union all "; //构造select语句 } sqlStr = string.Format(@"begin delete from {0:g} where {1:g}={2:g}; insert into {0:g}({3:g}) {4:g} ; end;", tableName, parentField, parentID, fields, selSql.Substring(0, selSql.Length - 10)); return Sunway.DBUtility.DbHelperSQL.ExecuteNonQuery(CommandType.Text, sqlStr, null); } /// <summary> /// 保存gridpanel数据 /// </summary> /// <param name="jsonData">Store的json数据</param> /// <param name="store">gridpanel的store</param> /// <param name="tableName">保存的表名称</param> public static int SaveGridData(string jsonData, Store store, string tableName) { //用Newtonsoft.Json得到JArray数组 JArray ja = (JArray)JsonConvert.DeserializeObject(jsonData); RecordFieldCollection RecordField = store.Reader[0].Fields; string fields = RecordField[0].Name;//字段列表 string values = ""; //插入值列表 string sqlStr = ""; string selSql = ""; string sqlStr1 = ""; string[] sqlArray; for (int i = 0; i < ja.Count; i++) { values = "'" + ja[i][RecordField[0].Name].ToString() + "'"; for (int j = 1; j < RecordField.Count; j++) { if (i == 0) { fields += "," + RecordField[j].Name; } values += "," + "'" + ja[i][RecordField[j].Name].ToString() + "'"; } selSql += "select " + values + " union all "; } sqlStr = string.Format("delete from {0:g}", tableName); if (selSql != "")//grid没有记录时(可能内容全删除了,或是没填) { sqlStr1 = string.Format(@"insert into {0:g}({1:g}) {2:g} ", tableName, fields, selSql.Substring(0, selSql.Length - 10)); } if (selSql == "") { return Sunway.DBUtility.DbHelperSQL.ExecuteNonQuery(CommandType.Text, sqlStr, null); } else { sqlArray = new string[] { selSql, selSql }; return TranHelp.ExecTran(sqlArray); } } /// <summary> /// 保存gridpanel子表(带触发器) /// </summary> /// <param name="jsonData">子表Store的json数据</param> /// <param name="store">子表store(Ext.Net.Store)</param> /// <param name="tableName">子表名称</param> /// <param name="parentID">父表记录的ID</param> /// <param name="isTrigger">是否带触发器</param> public static int SaveGridData(string jsonData, Ext.Net.Store store, string tableName, int parentID, bool isTrigger) { int num = 0; if (isTrigger == true) { //反序列化得到JArray数组 JArray ja = (JArray)JsonConvert.DeserializeObject(jsonData); RecordFieldCollection RecordField = store.Reader[0].Fields; string parentField = RecordField[0].Name; //父表记录的ID string fields = parentField;//字段列表 string values = ""; //插入值列表 string sqlStr = ""; string insSql = ""; for (int i = 0; i < ja.Count; i++) //行循环 { values = "'" + parentID.ToString() + "'"; for (int j = 1; j < RecordField.Count; j++) //列循环 { if (i == 0) //只执行一次最外层循环 { fields += "," + RecordField[j].Name; } values += "," + "'" + ja[i][RecordField[j].Name].ToString() + "'"; } insSql += string.Format("insert into {0:g}({1:g}) values({2:g});", tableName, fields, values); //构造Insert语句 } sqlStr = string.Format(@"begin delete from {0:g} where {1:g}={2:g}; {3:g} end;", tableName, parentField, parentID, insSql); try { num = Sunway.DBUtility.DbHelperSQL.ExecuteNonQuery(CommandType.Text, sqlStr, null); } catch (Exception) { num = -1; } } return num; } /// <summary> /// 保存grid行 /// </summary> /// <param name="jsonData"></param> /// <param name="store"></param> /// <param name="tableName"></param> /// <param name="parentID"></param> /// <returns></returns> public static int SaveGridRow(string jsonData, Ext.Net.Store store, string tableName, string parentID) { //反序列化得到JArray数组 JArray ja = (JArray)JsonConvert.DeserializeObject(jsonData); RecordFieldCollection RecordField = store.Reader[0].Fields; string parentField = RecordField[0].Name; //父表记录的ID string fields = "";//字段列表 string values = ""; //插入值列表 string sqlStr = ""; string insSql = ""; for (int j = 1; j < RecordField.Count; j++) //列循环 { fields += RecordField[j].Name+","; values += "'"+ja[0][RecordField[j].Name].ToString() + "',"; } insSql += string.Format("insert into {0:g}({1:g}) values({2:g});", tableName, fields.Substring(0,fields.Length-1), values.Substring(0,values.Length-1)); //构造Insert语句 if (parentID != "") { sqlStr = string.Format(@"begin delete from {0:g} where {1:g}={2:g};{3:g} end;", tableName, parentField, parentID, insSql); } else { sqlStr = insSql; } return Sunway.DBUtility.DbHelperSQL.ExecuteNonQuery(CommandType.Text, sqlStr, null); } #endregion #region 导入Excel /// <summary> /// 导入Excel到GridPanel,应用com组件 /// </summary> /// <param name="filenameurl">物理路径</param> /// <param name="sheetIndex">sheet名称的索引</param> /// <returns></returns> public static void ImpExecleToGrid(string filenameurl, GridPanel grd) { int sheetIndex = 1;//sheet名称的索引 Microsoft.Office.Interop.Excel.Workbook wb = null; Microsoft.Office.Interop.Excel.Worksheet ws = null; ArrayList columnArr = new ArrayList();//列字段表 DataSet myDs = new DataSet(); DataTable xlsTable = myDs.Tables.Add("show"); object missing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); if (excel != null) { excel.Visible = false; excel.UserControl = true; // 以只读的形式打开EXCEL文件 wb = excel.Workbooks.Open(filenameurl, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing); //取得第一个工作薄 ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(sheetIndex); //取得总记录行数(包括标题列) int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数 int columnsint = ws.UsedRange.Cells.Columns.Count;//得到列数 DataRow dr; string colName = ""; for (int i = 1; i < columnsint; i++) { colName = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[1, i]).Text.ToString(); if (grd.ColumnModel.Columns[i].Header == colName) { xlsTable.Columns.Add(grd.ColumnModel.Columns[i].DataIndex, typeof(string)); columnArr.Add(grd.ColumnModel.Columns[i].DataIndex); } } for (int i = 2; i <= rowsint; i++) { dr = xlsTable.NewRow(); for (int j = 1; j < columnsint; j++) { dr[columnArr[j - 1].ToString()] = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, j]).Text.ToString(); } xlsTable.Rows.Add(dr); } } excel.Quit(); excel = null; //Dispose(ws, wb); GC.Collect(); grd.Store[0].DataSource = xlsTable; grd.Store[0].DataBind(); } #endregion /// <summary> /// 设置列是否可修改 /// </summary> /// <param name="cm">ColumnModel</param> /// <param name="isEditable"></param> public static void SetColumnsEditable(ColumnModel cm,bool isEditable) { for (int i = 0; i < cm.Columns.Count; i++) { cm.SetEditable(i, isEditable); } } public static void SaveStore(Store st, string ID) { } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。