Ejemplo n.º 1
0
        /// <summary>
        /// 找指定表名下可用的键值对
        /// </summary>
        /// <param name="notfield">排除指定的字段或者列</param>
        /// <param name="outField">返回的字段信息</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        private static Dictionary <string, string> GetDictPrintHeader(string[] notfield, string tableName, out string[] outField)
        {
            string[] field = GlobalLngResource.GetColumnsFrmRes(notfield, tableName);
            outField = field;
            Dictionary <string, string> dictPrintHead = new Dictionary <string, string>();

            foreach (string item in field)
            {
                if (GlobalLngResource.GetRes(item, tableName) != null)
                {
                    dictPrintHead.Add(item, GlobalLngResource.GetRes(item, tableName));
                }
            }
            return(dictPrintHead);
        }
Ejemplo n.º 2
0
        public static MemoryStream ListToExcel<T1>(IList<T> list, string tableName, string[] notfield)
        {
            //获取指定表的所有字段
            string[] propertyName = GlobalLngResource.GetColumnsFrmRes(notfield, tableName);

            //创建流对象
            using (MemoryStream ms = new MemoryStream())
            {
                //将参数写入到一个临时集合中
                List<string> propertyNameList = new List<string>();
                List<string> propertyValueList = new List<string>();
                if (propertyName != null)
                    propertyNameList.AddRange(propertyName);
                //根据参数遍历资源文件值
                foreach (string item in propertyName)
                {
                    if (!string.IsNullOrEmpty(GlobalLngResource.GetRes(item, tableName)))
                    {
                        propertyValueList.Add(GlobalLngResource.GetRes(item, tableName));
                    }
                    //if (rm.GetString(item) != null)
                    //    propertyValueList.Add(rm.GetString(item));
                }
                //创NOPI的相关对象
                IWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();
                IRow headerRow = sheet.CreateRow(0);
                if (list.Count > 0)
                {
                    //通过反射得到对象的属性集合
                    //  PropertyInfo[] propertys = list[0].GetType().GetProperties();
                    //遍历资源文件值集合生成excel的表头标题
                    for (int i = 0; i < propertyValueList.Count; i++)
                    {
                        headerRow.CreateCell(i).SetCellValue(propertyValueList[i].ToString());
                    }
                    int rowIndex = 1;
                    //遍历集合生成excel的行集数据
                    for (int i = 0; i < list.Count; i++)
                    {
                        IRow dataRow = sheet.CreateRow(rowIndex);
                        for (int j = 0; j < propertyNameList.Count; j++)
                        {
                            //通过反射得到参数对象的属性
                            PropertyInfo property = list[i].GetType().GetProperty(propertyNameList[j].ToString());
                            if (propertyNameList.Contains(property.Name))
                            {
                                object obj = property.GetValue(list[i], null);
                                if (obj != null)
                                    dataRow.CreateCell(j).SetCellValue(obj.ToString());
                            }
                        }
                        rowIndex++;
                    }
                }
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }