Exemplo n.º 1
0
        /// <summary>
        /// 加载工作簿
        /// </summary>
        /// <param name="fileName">文件名称,绝对路径</param>
        protected override void LoadWorkbook(string fileName)
        {
            FileStream fs = File.OpenRead(fileName);

            switch (ExcelFormat)
            {
            case ExcelFormat.Xls:
                _workbook = new HSSFWorkbook(fs);
                break;

            case ExcelFormat.Xlsx:
                _workbook = new XSSFWorkbook(fs);
                break;

            default:
                throw new Exception("未知 Excel 格式文件");
            }
            fs.Close();

            // 读取当前表数据
            var sheetNum = _workbook.NumberOfSheets;

            for (int i = 0; i < sheetNum; i++)
            {
                ISheet sheet     = _workbook.GetSheetAt(i);
                var    worksheet = new NpoiWorkSheet(sheet);
                WorkSheets.Add(worksheet);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 插入工作表
        /// </summary>
        /// <param name="sheetName">工作表名称</param>
        /// <returns></returns>
        public override IWorkSheet InsertSheet(string sheetName)
        {
            var sheet     = _workbook.CreateSheet(sheetName);
            var worksheet = new NpoiWorkSheet(sheet);

            WorkSheets.Add(worksheet);
            return(worksheet);
        }