聊聊对NPOI的认识

一、简介

       NPOI是一个开源的C#读写Excel、Word等微软OLE2组件文档的项目,可以在没有安装Office的情况下对Word或Excel文档进行导入导出操作。

      官网:http://npoi.codeplex.com/

二、相关引用

      1.ICsharpcode.SharpZipLib.dll      对文件进行压缩、解压操作,支持Zip, GZip, BZip2 和Tar格式

      2.NPOI.dll                                      微软Excel BIFF(Excel 97-2003, doc)格式读写库,基础类库

      3.NPOI.OOXML.dll                        Excel 2007(xlsx)格式读写库,Word 2007(docx)格式读写库

      4.NPOI.OpenXml4Net.dll              OpenXml底层zip包读写库

      5.NPOI.OpenXmlFormats.dll         微软Office OpenXml对象关系库

三、导入Excel实例

      1.新建一个类,命名为ExcelHelper。

      2.添加引用:using NPOI.HSSF.UserModel;(Excel 97-2003, doc)格式读写库
                           using NPOI.SS.UserModel;Excel公用接口及Excel公式计算引擎
                           using NPOI.XSSF.UserModel;Excel 2007(xlsx)格式读写库

      3.添加ExcelToTable方法

        /// <summary>
        /// 读取Excel
        /// </summary>
        /// <param name="file">文件全路径</param>
        /// <param name="sheetName">excel表sheet名称</param>
        /// <returns>返回DaTaTable</returns>
        public static DataTable ExcelToTable(string file, string sheetName = null)
        {
            DataTable dt = new DataTable();
            try
            {
                using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))//打开excel文件
                {
                    IWorkbook workbook = null;
                    if (file.IndexOf(".xlsx") > 0) // 2007版本
                        workbook = new XSSFWorkbook(fs);
                    else if (file.IndexOf(".xls") > 0) // 2003版本
                        workbook = new HSSFWorkbook(fs);

                    ISheet sheet = null;
                    if (sheetName == null)
                        sheet = workbook.GetSheetAt(0);//根据坐标读区sheet表,0表示第一个
                    else
                        sheet = workbook.GetSheet(sheetName);//根据sheet名称读取sheet表

                    //列名
                    IRow rowHead = sheet.GetRow(sheet.FirstRowNum);//sheet.FirstRowNum 第一个单元格的行号
                    for (int i = 0; i < rowHead.LastCellNum; i++)//rowHead.LastCellNum 最后一个单元格的列号
                    {
                        string fildName = rowHead.GetCell(i).StringCellValue;
                        dt.Columns.Add(fildName, typeof(String));
                    }

                    //数据
                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)//sheet.LastRowNum最后一个单元格的行号
                    {
                        IRow row = sheet.GetRow(i);
                        DataRow dr = dt.NewRow();
                        for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
                        {
                            ICell cell = row.GetCell(j);
                            dr[j] = GetValueType(cell);
                            if (dr[j] == null)
                            {
                                break;
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

      4.添加GetValueType方法

        /// <summary>  
        /// 获取单元格类型(xlsx)  
        /// </summary>  
        /// <param name="cell"></param>  
        /// <returns></returns>  
        private static object GetValueType(ICell cell)
        {
            try
            {
                //XSSFCell
                if (cell == null)
                {
                    return null;
                }
                switch (cell.CellType)
                {
                    case NPOI.SS.UserModel.CellType.Blank: //BLANK:  
                        return null;
                    case NPOI.SS.UserModel.CellType.Boolean: //BOOLEAN:  
                        return cell.BooleanCellValue;
                    case NPOI.SS.UserModel.CellType.Numeric: //NUMERIC:  
                        return cell.NumericCellValue;
                    case NPOI.SS.UserModel.CellType.String: //STRING:  
                        return cell.StringCellValue;
                    case NPOI.SS.UserModel.CellType.Error: //ERROR:  
                        return cell.ErrorCellValue;
                    case NPOI.SS.UserModel.CellType.Formula: //FORMULA:  
                    default:
                        return "=" + cell.CellFormula;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

四、导出Excel实例

  .............

猜你喜欢

转载自blog.csdn.net/u011301348/article/details/81510884