zoukankan      html  css  js  c++  java
  • C# NPOI read excel files include xls and xlsx

    1.Install-package npoi;

    2.Add necessary namespace as below.

    using System.IO;
    using System.IO.Compression;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;

    3.code 

     static void ReadPrintExcelViaNPOI(string excelFileName)
            {
                string excelExtension=  Path.GetExtension(excelFileName);
                switch(excelExtension)
                {
                    case ".xls":
                        NPOIReadXls(excelFileName);
                        break;
                    case ".xlsx":
                        NPOIReadXlsX(excelFileName);
                        break;
                }   
            }
    
            static void NPOIReadXls(string xlsFileName)
            {
                try
                {
                    HSSFWorkbook book;
                    using (FileStream fs = new FileStream(xlsFileName, FileMode.Open, FileAccess.Read))
                    {
                        book = new HSSFWorkbook(fs);
                        ISheet st = book.GetSheetAt(0);
                        int rowsCount = st.LastRowNum;
                        totalRowsCount = rowsCount;
                        for (int i = 0; i < rowsCount; i++)
                        {
                            int columnsCount = st.GetRow(i).Cells.Count();
                            StringBuilder rowBuilder = new StringBuilder();
                            for (int j = 0; j < columnsCount; j++)
                            {
    
                                ICell cell = st.GetRow(i).GetCell(j);
                                string cellValue = string.Empty;
                                if (cell != null)
                                {
                                    switch (cell.CellType)
                                    {
                                        case CellType.String:
                                            cellValue = cell.StringCellValue;
                                            break;
                                        case CellType.Numeric:
                                            cellValue = cell.NumericCellValue.ToString();
                                            break;
                                    }
                                }
                                rowBuilder.Append(cellValue + ",");
                            }
                            string msg = rowBuilder.ToString().TrimEnd(new char[] { ',' });
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                Console.WriteLine(msg);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
            static void NPOIReadXlsX(string xlsxFileName)
            {
                try
                {
                    XSSFWorkbook  book;
                    using (FileStream fs = new FileStream(xlsxFileName, FileMode.Open, FileAccess.Read))
                    {
                        book = new XSSFWorkbook(fs);
                        ISheet st = book.GetSheetAt(0);
                        int rowsCount = st.LastRowNum;
                        totalRowsCount = rowsCount;
                        for (int i = 0; i < rowsCount; i++)
                        {
                            int columnsCount = st.GetRow(i).Cells.Count();
                            StringBuilder rowBuilder = new StringBuilder();
                            for (int j = 0; j < columnsCount; j++)
                            {
                                ICell cell = st.GetRow(i).GetCell(j);
                                string cellValue = string.Empty;
                                if (cell != null)
                                {
                                    switch (cell.CellType)
                                    {
                                        case CellType.String:
                                            cellValue = cell.StringCellValue;
                                            break;
                                        case CellType.Numeric:
                                            cellValue = cell.NumericCellValue.ToString();
                                            break;
                                    }
                                }
                                rowBuilder.Append(cellValue + ",");
                            }
                            string msg = rowBuilder.ToString().TrimEnd(new char[] { ',' });
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                Console.WriteLine(msg);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
  • 相关阅读:
    Yaf 在 Nginx 中的配置
    关于 GPG 用这个 就够 了
    PHP 服务器端处理跨域问题
    Linux 终端代理方式
    【转载】Oracle数据字典详解
    【转载】Oracle之内存结构(SGA、PGA)
    【转载】 使用rman进行坏块修复(ORA-01578、ORA-01110)
    【转载】使用Exp和Expdp导出数据的性能对比与优化
    【转载】oracle dbms_metadata.get_ddl的使用方法总结
    ORACLE执行详解
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13195932.html
Copyright © 2011-2022 走看看