Ejemplo n.º 1
0
        private DataTable ExcelToTable(string filePath)
        {
            DataTable dt = null;

            if (string.IsNullOrEmpty(filePath))
            {
                return(dt);
            }
            if (filePath.ToLower().EndsWith("xls"))
            {
                dt = NpoiHelper.GetInstance().ImportFromExcel(filePath, "0", 0);
            }
            else if (filePath.ToLower().EndsWith("xlsx"))
            {
                dt = NpoiHelper.GetInstance(false).ImportFromExcel(filePath, "0", 0);
            }

            return(dt);
        }
Ejemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();
            list.Add(new TestData {
                Name = "zhangshan", Age = 40, Birthday = DateTime.Now
            });
            list.Add(new TestData {
                Name = "lisi", Age = 28, Birthday = DateTime.Now
            });
            list.Add(new TestData {
                Name = "wangwu", Age = 36, Birthday = DateTime.Now
            });
            list1.Add(new ComplexData {
                Name = "zhangshan", Age = 40, Birthday = DateTime.Now, Detail = new Detail {
                    Tel = "02885394654", HomeAddr = new Address {
                        CommonAddr = "黄河村", PostNo = "610048"
                    }
                }
            });
            DataTable dt = new DataTable("TestTable");

            dt.Columns.Add("Name");
            dt.Columns.Add("Age");
            dt.Columns.Add("Birthday");
            DataRow dr = dt.NewRow();

            dr[0] = "zhangshan";
            dr[1] = 50;
            dr[2] = DateTime.Now;
            dt.Rows.Add(dr);

            NpoiHelper.GetInstance().ExportToExcel(dt);
            //NpoiHelper.GetInstance().ExportToExcel<TestData>(list);
            //NpoiHelper.GetInstance().ExportToExcel<ComplexData>(list1);

            //string file = AppDomain.CurrentDomain.BaseDirectory + "excel.xls";
            //NpoiHelper.GetInstance().ImportFromExcel(file,"0",0);
        }