Esempio n. 1
0
        /// <summary>
        /// 读Excel单元格的数据
        /// </summary>
        /// <param name="cell">Excel单元格</param>
        /// <param name="type">列数据类型</param>
        /// <param name="obj"></param>
        /// <returns>object 单元格数据</returns>
        private static void SetValue(ICell cell, PropertyInfo type, object obj)
        {
            if (cell == null)
            {
                return;
            }

            object cellValue = NPOIExtensions.GetValueByCellStyle(cell, cell?.CellType);

            string dataType = type.PropertyType.FullName;

            if (dataType == "System.String")
            {
                //  cellValue == type(double),会有异常
                type.SetValue(obj, cellValue?.ToString().Trim(), null);
            }
            else if (dataType == "System.DateTime")
            {
                DateTime pdt = Convert.ToDateTime(cellValue);
                type.SetValue(obj, pdt, null);
            }
            else if (dataType?.Contains("System.DateTime") == true)
            {
                DateTime?pdt;
                if (string.IsNullOrWhiteSpace(cellValue?.ToString()))
                {
                    pdt = null;
                }
                else
                {
                    pdt = Convert.ToDateTime(cellValue);
                }
                type.SetValue(obj, pdt, null);
            }
            else if (dataType == "System.Boolean")
            {
                bool pb = Convert.ToBoolean(cellValue);
                type.SetValue(obj, pb, null);
            }
            else if (dataType == "System.Int16")
            {
                Int16 pi16 = Convert.ToInt16(cellValue);
                type.SetValue(obj, pi16, null);
            }
            else if (dataType == "System.Int32")
            {
                Int32 pi32 = Convert.ToInt32(cellValue);
                type.SetValue(obj, pi32, null);
            }
            else if (dataType == "System.Int64")
            {
                Int64 pi64 = Convert.ToInt64(cellValue);
                type.SetValue(obj, pi64, null);
            }
            else if (dataType == "System.Byte")
            {
                Byte pb = Convert.ToByte(cellValue);
                type.SetValue(obj, pb, null);
            }
            else if (dataType == "System.Decimal")
            {
                System.Decimal pd = Convert.ToDecimal(cellValue);
                type.SetValue(obj, pd, null);
            }
            else if (dataType == "System.Double")
            {
                double pd = Convert.ToDouble(cellValue);
                type.SetValue(obj, pd, null);
            }
            else
            {
                type.SetValue(obj, null, null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 实体保存到Excel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entitys"></param>
        /// <param name="filePath"></param>
        public static void ToExcel <T>(this List <T> entitys, string filePath) where T : new()
        {
            var table = NPOIExtensions.ToDatatableFromList(entitys);

            DataTableToExcel(table, filePath);
        }