/// <summary> /// Inserts values of all ExcelTableEntity properties to DataTable /// </summary> /// <param name="obj"></param> /// <param name="dtEntityInfo"></param> private void InsertEntityDataToTable(object obj, System.Data.DataTable dtEntityInfo) { DataRow row = dtEntityInfo.Rows.Add(); //Lists all Properties of ExcelTableEntity Type entityType = typeof(ExcelTableEntity); PropertyInfo[] ProList = entityType.GetProperties(); foreach (PropertyInfo Pro in ProList) { if (Pro.PropertyType.Name.Contains("IDictionary")) { Dictionary <string, EntityProperty> dicEntity = (Dictionary <string, EntityProperty>)Pro.GetValue(obj, null); foreach (string key in dicEntity.Keys) { if (!dtEntityInfo.Columns.Contains(key)) { DataColumn col = new DataColumn(key); dtEntityInfo.Columns.Add(col); } row[key] = dicEntity[key].PropertyAsObject.ToString(); } } else if (Pro.Name != "ETag") { row[Pro.Name] = Pro.GetValue(obj, null).ToString(); } } }
/// <summary> /// Inserts values of all ExcelTableEntity properties to DataTable /// </summary> /// <param name="obj"></param> /// <param name="dtEntityInfo"></param> private void InsertEntityDataToTable(object obj, System.Data.DataTable dtEntityInfo, params string[] filter) { try { DataRow row = dtEntityInfo.Rows.Add(); //Lists all Properties of ExcelTableEntity Type entityType = typeof(ExcelTableEntity); PropertyInfo[] ProList = entityType.GetProperties(); foreach (PropertyInfo Pro in ProList) { if (Pro.PropertyType.Name.Contains("IDictionary")) { Dictionary <string, EntityProperty> dicEntity = (Dictionary <string, EntityProperty>)Pro.GetValue(obj, null); foreach (string key in dicEntity.Keys) { if (!dtEntityInfo.Columns.Contains(key)) { DataColumn col = new DataColumn(key); dtEntityInfo.Columns.Add(col); } if (filter == null || filter.Length == 0 || filter.Contains(key)) { row[key] = dicEntity[key].PropertyAsObject.ToString(); } else { row[key] = "<hidden>"; } } } else if (Pro.Name != "ETag") { row[Pro.Name] = Pro.GetValue(obj, null).ToString(); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 导出excel文件 /// </summary> /// <param name="Columns">列名和显示名的键值对</param> /// <param name="DataList">数据列表</param> /// <param name="ExcelName">导出的excel文件名称</param> /// <param name="encodingType">字符集</param> public static void ExportExcel <T>(Dictionary <string, string> Columns, List <T> DataList, string ExcelName, string encodingType = "UTF-8") { Type TType = typeof(T); Dictionary <PropertyInfo, string> ColumnPros = new Dictionary <PropertyInfo, string>(); foreach (string ProName in Columns.Keys) { ColumnPros.Add(TType.GetProperty(ProName), Columns[ProName]); } string shtnl = ""; shtnl = "<table border='1' cellspacing='1' cellpadding='1'>"; shtnl = shtnl + "<thead>"; foreach (string Th_Text in ColumnPros.Values) { shtnl = shtnl + "<th>" + Th_Text + "</th>"; } shtnl = shtnl + "</thead><tbody>"; for (int i = 0; i < DataList.Count; i++) { shtnl = shtnl + "<tr>"; foreach (PropertyInfo Pro in ColumnPros.Keys) { shtnl = shtnl + "<td>" + Pro.GetValue(DataList[i], null) + "</td>"; } shtnl = shtnl + "</tr>"; } shtnl = shtnl + "</tbody></table>"; string HEADER = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">" + "<meta http-equiv=Content-Type content=\"text/html; charset=\"gb2312\">" + "<head>" + "<!--[if gte mso 9]><xml>" + "<x:ExcelWorkbook>" + "<x:ExcelWorksheets>" + "<x:ExcelWorksheet>" + "<x:Name>Sheet1</x:Name>" + "<x:WorksheetOptions>" + "<x:Print>" + "<x:ValidPrinterInfo />" + "</x:Print>" + "</x:WorksheetOptions>" + "</x:ExcelWorksheet>" + "</x:ExcelWorksheets>" + "</x:ExcelWorkbook>" + "</xml>" + "<![endif]-->"; Encoding encoding = Encoding.GetEncoding(encodingType); HttpContext.Current.Response.ContentEncoding = encoding; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelName, encoding)); HttpContext.Current.Response.ContentType = "ms-excel/application"; StringBuilder sbHtml = new StringBuilder(); sbHtml.AppendFormat(@"{0}</head> <body>{1}</body> </html>", HEADER, shtnl); HttpContext.Current.Response.Write(sbHtml.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.End(); }
/// <summary> /// Sets title of column of DataTable using property of ExcelTableEntity /// </summary> /// <param name="obj"></param> /// <param name="dtEntityInfo"></param> private void SetColumnTitle(object obj, System.Data.DataTable dtEntityInfo) { try { //Lists all Properties of ExcelTableEntity Type entityType = typeof(ExcelTableEntity); PropertyInfo[] ProList = entityType.GetProperties(); foreach (PropertyInfo Pro in ProList) { if (Pro.PropertyType.Name.Contains("IDictionary")) { Dictionary <string, EntityProperty> dicEntity = (Dictionary <string, EntityProperty>)Pro.GetValue(obj, null); foreach (string key in dicEntity.Keys) { DataColumn col = new DataColumn(key); dtEntityInfo.Columns.Add(col); } } else { DataColumn col = new DataColumn(Pro.Name); dtEntityInfo.Columns.Add(col); } } } catch (Exception ex) { throw ex; } }
private void ModifyPatBtn_Click(object sender, EventArgs e) { string HeaderText = ""; string CellValue = ""; string ProValue = ""; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("./Configurations/Recipe.xml"); XmlNodeList xmlelem = xmldoc.SelectSingleNode("Recipes").ChildNodes; //XmlNode xmlelems = (XmlNode)xmldoc.SelectSingleNode("//Recipe"); int ChooseIndex = this.RecDataView.CurrentRow.Index; int Count = this.PatternDataView.Rows.Count; //foreach (Pattern SinglePatt in context.ModuleRecipe[ChooseIndex].PatternArray) //{ //} for (int i = 0; i < Count; i++) { Pattern SinglePattern = context.ModuleRecipe[ChooseIndex].PatternArray[i]; Type PatternTp = SinglePattern.GetType(); foreach (PropertyInfo Pro in PatternTp.GetProperties()) { for (int j = 0; j < 4; j++) { HeaderText = this.PatternDataView.Columns[j].HeaderText; CellValue = this.PatternDataView.Rows[i].Cells[j].Value.ToString(); ProValue = Pro.GetValue(SinglePattern, null).ToString(); if (Pro.Name == HeaderText && ProValue != CellValue) { //ModifyElementValue(xmlelem, HeaderText, ProValue, CellValue); foreach (XmlNode xl in xmlelem) { XmlNodeList xmllist = xl.ChildNodes; foreach (XmlNode single in xl.ChildNodes) { XmlElement item = (XmlElement)single; if (item.GetAttribute(HeaderText) == ProValue) { item.SetAttribute(HeaderText, CellValue); } } } } } for (int n = 4; n < this.PatternDataView.Columns.Count; n++) { HeaderText = this.PatternDataView.Columns[n].HeaderText; CellValue = this.PatternDataView.Rows[i].Cells[n].Value.ToString(); ProValue = Pro.GetValue(SinglePattern, null).ToString(); if (Pro.Name == HeaderText && ProValue != CellValue) { foreach (XmlNode xl in xmlelem) { XmlNodeList xmllist = xl.ChildNodes; foreach (XmlNode single in xl.ChildNodes) { foreach (XmlNode item in single.ChildNodes) { XmlElement value = (XmlElement)item; if (value.Name == HeaderText) { item.InnerText = CellValue; } } } } } } } } xmldoc.Save("./Configurations/Recipe1.xml"); }