Exemple #1
0
        /// <summary>
        ///     读取excel ,默认第一行为标头
        ///     读取所有页
        /// </summary>
        /// <param name="strFileName">excel文档路径</param>
        /// <returns></returns>
        public static DataTable ExcelImport(string strFileName, List <string> lstCol, bool allSheets)
        {
            if (!allSheets)
            {
                return(ExcelImport(strFileName));
            }
            var dt = DataTableHelper.CreateTable(lstCol);

            ISheet sheet      = null;
            var    countSheet = 0;

            using (var file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
            {
                if (strFileName.IndexOf(".xlsx") == -1) //2003
                {
                    var hssfworkbook = new HSSFWorkbook(file);
                    countSheet = hssfworkbook.NumberOfSheets;
                    // sheet = hssfworkbook.GetSheetAt(0);
                }
                else //2007
                {
                    var xssfworkbook = new XSSFWorkbook(file);
                    countSheet = xssfworkbook.NumberOfSheets;
                    //sheet = xssfworkbook.GetSheetAt(0);
                }
            }

            for (var i = 0; i < countSheet; i++)
            {
                DataTableHelper.AddTableData(dt, ExcelImport(strFileName, i, lstCol));
            }
            return(dt);
        }