Example #1
0
        private DataTable LoadXLSX(string path, string table, int countColumns)
        {
            //read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
            //book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added.
            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                xssfworkbook = new XSSFWorkbook(file);
            }

            ISheet sheet = null;

            bool found = false;

            int c = xssfworkbook.Count();

            for (int i = 0; i < c; i++)
            {
                sheet = xssfworkbook.GetSheetAt(i);

                if (sheet.SheetName.ToLower(CultureInfo.InvariantCulture) == table.ToLower(CultureInfo.InvariantCulture))
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                return(new DataTable());
            }

            return(ReadSheet(sheet, countColumns));
        }