NPOI——判断工作表是否隐藏, 单元格是否合并

来源:互联网 发布:营销数据管理软件 编辑:程序博客网 时间:2024/06/10 01:58
using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.SS.UserModel;using System.Data;using System.IO;using NPOI.XSSF.UserModel;using NPOI.HSSF.UserModel;using NengLong.Util;namespace ExcelTest{    class Program    {        static void Main(string[] args)        {            string filePath = @"C:\Users\Administrator\Desktop\xxx.xlsx";            IWorkbook workbook = null;            ISheet sheet = null;            int rowIdx = 0;            int colIdx = 0;            try            {                //注:FileShare.ReadWrite 允许打开文件的同时程序可以操作.                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);                if (filePath.IndexOf(".xlsx") > 0) // 2007版本                    workbook = new XSSFWorkbook(fs);                else if (filePath.IndexOf(".xls") > 0) // 2003版本                    workbook = new HSSFWorkbook(fs);                //查询到所有的工作表及是否隐藏                Console.WriteLine("NumberOfSheets: {0}", workbook.NumberOfSheets);                for (int i = 0; i < workbook.NumberOfSheets; i++)                {                    sheet = workbook.GetSheetAt(i);                    Console.WriteLine("Sheet Name:{0}, IsHidden:{1}", sheet.SheetName, workbook.IsSheetHidden(i));                }                //获取第一个可用行                rowIdx = sheet.FirstRowNum;                IRow r= sheet.GetRow(sheet.FirstRowNum);                //输出此行的所有列的列名及是否合并列                for (colIdx = r.FirstCellNum; colIdx < r.LastCellNum; colIdx++)                 {                    ICell cell = r.GetCell(colIdx);                    if (cell!=null && !string.IsNullOrEmpty(cell.StringCellValue))                    {                        Console.WriteLine("row/col:{0}/{1}, Cell Header:{2}, isMergedCell:{3}"                            , sheet.FirstRowNum                            , colIdx                            , cell.StringCellValue                            , cell.IsMergedCell);                    }                }                fs.Close();            }            catch (Exception ex)            {                Console.WriteLine("row/col:{0}/{1}, errMsg:{2}"                    , rowIdx                    , colIdx                    , ex.Message);            }            Console.WriteLine("End");            Console.Read();        }    }}

0 0