• 大小: 3.59MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-08-10
  • 语言: C#
  • 标签: C#,NPOI  

资源简介

最近工作需要做一个导入Excel功能,特地学习制作,代码逻辑清晰,注释明确,方便新手入门。可导入多表头Excel数据文件,免去新手找DLL、无从下手等一系列问题。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XlsImportDemo
{
    public class BeanToMapUtil
    {
        /**
        * 将一个 Map 对象转化为一个 JavaBean
        * @param type 要转化的类型
        * @param map 包含属性值的 map
        * @return 转化出来的 JavaBean 对象
        * @throws IntrospectionException 如果分析类属性失败
        * @throws IllegalAccessException 如果实例化 JavaBean 失败
        * @throws InstantiationException 如果实例化 JavaBean 失败
        * @throws InvocationTargetException 如果调用属性的 setter 方法失败
        */
        public static object convertMap(User objSource Dictionaryject> map)
        {

            Type typeSource = objSource.GetType();
            object objTarget = Activator.CreateInstance(typeSource);//创建目标对象
            PropertyInfo[] propertyDescriptors = typeSource.GetProperties();
            try
            {
                foreach (PropertyInfo p in propertyDescriptors)//遍历属性
                {
                    if (p.CanWrite)
                    {
                        string propertyName = p.Name;
                        if (map.Keys.Contains(propertyName))
                        {
                            object value = map[propertyName];

                            object[] args = new object[1];
                            args[0] = value;

                            p.SetMethod.Invoke(objTarget args);

                            //object objPropertyValue = p.GetValue(objSource null);
                            //if (objPropertyValue == null)
                            //{
                            //    p.SetValue(objTarget args);
                            //}

                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return objTarget;

        }


        /**
        * 将一个 JavaBean 对象转化为一个  Map
        * @param bean 要转化的JavaBean 对象
        * @return 转化出来的  Map 对象
        * @throws IntrospectionException 如果分析类属性失败
        * @throws IllegalAccessException 如果实例化 JavaBean 失败
        * @throws InvocationTargetException 如果调用属性的 setter 方法失败
        */
        public static Dictionaryject> convertBean(object bean)
        {
           Type sType= bean.GetType();
           Dictionaryject> returnMap = new Dictionaryject>();
           PropertyInfo[] propertyDescriptors = sType.GetProperties();
           foreach (PropertyInfo p in propertyDescriptors)
           {
               string propertyName = p.Name;
               if (!propertyName.Equals(“class“))
               {
                   MethodInfo readMethod = p.GetGetMethod();
                  
                   object result =readMethod.Invoke(beannew object[0]);
                   if (result != null)
                   {
                       return

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-09-30 10:59  OperateExcelDemo\
     文件         187  2015-01-29 10:37  OperateExcelDemo\App.config
     文件        3524  2015-01-30 11:41  OperateExcelDemo\BeanToMapUtil.cs
     目录           0  2015-01-29 10:37  OperateExcelDemo\bin\
     目录           0  2017-09-30 10:55  OperateExcelDemo\bin\Debug\
     文件      200704  2014-07-03 10:56  OperateExcelDemo\bin\Debug\ICSharpCode.SharpZipLib.dll
     文件     1603072  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.dll
     文件      421888  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.OOxml.dll
     文件      420479  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.OOxml.xml
     文件       85504  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.Openxml4Net.dll
     文件      153211  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.Openxml4Net.xml
     文件     1873408  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.OpenxmlFormats.dll
     文件     2134991  2014-12-11 05:48  OperateExcelDemo\bin\Debug\NPOI.xml
     文件        1057  2015-01-29 11:27  OperateExcelDemo\bin\Debug\user.xml
     文件       19456  2017-09-30 10:55  OperateExcelDemo\bin\Debug\XlsImportDemo.exe
     文件        1266  2017-09-30 10:55  OperateExcelDemo\bin\Debug\XlsImportDemo.exe.CodeAnalysisLog.xml
     文件         187  2015-01-29 10:37  OperateExcelDemo\bin\Debug\XlsImportDemo.exe.config
     文件           0  2017-09-30 10:55  OperateExcelDemo\bin\Debug\XlsImportDemo.exe.lastcodeanalysissucceeded
     文件       48640  2017-09-30 10:55  OperateExcelDemo\bin\Debug\XlsImportDemo.pdb
     文件       24224  2017-09-30 10:59  OperateExcelDemo\bin\Debug\XlsImportDemo.vshost.exe
     文件         187  2015-01-29 10:37  OperateExcelDemo\bin\Debug\XlsImportDemo.vshost.exe.config
     文件         490  2015-10-30 15:19  OperateExcelDemo\bin\Debug\XlsImportDemo.vshost.exe.manifest
     目录           0  2015-01-29 17:20  OperateExcelDemo\DLL\
     文件      200704  2014-07-03 10:56  OperateExcelDemo\DLL\ICSharpCode.SharpZipLib.dll
     文件     1603072  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.dll
     文件      421888  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.OOxml.dll
     文件      420479  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.OOxml.xml
     文件       85504  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.Openxml4Net.dll
     文件      153211  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.Openxml4Net.xml
     文件     1873408  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.OpenxmlFormats.dll
     文件     2134991  2014-12-11 05:48  OperateExcelDemo\DLL\NPOI.xml
............此处省略33个文件信息

评论

共有 条评论

相关资源