NPOI 导入 03版本和 07版本

        #region 导入03Excel

        /// <summary>  
        /// 读取Excel文件到DataSet中  
        /// </summary>  
        /// <param name="filePath">文件路径</param>  
        /// <returns></returns>  
        public static DataTable ExcelToDataTable03(string filePath)
        {
            DataTable dt = new DataTable();

            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                ICell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        }


        /// <summary>
        /// 读取Excel文件到DataSet中  
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="sheetName">Excel中Sheet名</param>
        /// <returns></returns>
        public static DataTable ExcelToDataTable03(string filePath, string sheetName)
        {
            DataTable dt = new DataTable();

            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            ISheet sheet = hssfworkbook.GetSheet(sheetName);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                ICell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        }



        /// <summary>
        /// 读取Excel文件到DataSet中  
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static DataTable ExcelToDataTable03(Stream stream)
        {
            DataTable dt = new DataTable();

            HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
            ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                ICell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        }


        /// <summary>
        /// 读取Excel文件到DataSet中
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <param name="sheetName">Excel中Sheet名称</param>
        /// <returns></returns>
        public static DataTable ExcelToDataTable03(Stream stream, string sheetName)
        {
            DataTable dt = new DataTable();
            try
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(stream);
                ISheet sheet = hssfworkbook.GetSheet(sheetName);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;

                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }

                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();

                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }

                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
            catch
            {
                return dt;
            }
        }

        #endregion


        #region 导入Excel o7版

        /// <summary>
        /// 读取Excel文件到DataSet中  
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static DataTable ExcelToDataTable07(Stream stream)
        {
            DataTable dt = new DataTable();
            try
            {
                IWorkbook workbook = new XSSFWorkbook(stream);
                ISheet sheet = workbook.GetSheetAt(0);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;

                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }

                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();

                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }

                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
            catch
            {
                return dt;
            }
        }


        /// <summary>
        /// 根据文件流导入Excel到DataTable
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public static DataTable ExcelToDataTable07(Stream stream, string sheetName)
        {
            DataTable dt = new DataTable();
            try
            {
                IWorkbook workbook = new XSSFWorkbook(stream);
                ISheet sheet = workbook.GetSheet(sheetName);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

                IRow headerRow = sheet.GetRow(0);
                int cellCount = headerRow.LastCellNum;

                for (int j = 0; j < cellCount; j++)
                {
                    ICell cell = headerRow.GetCell(j);
                    dt.Columns.Add(cell.ToString());
                }

                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    DataRow dataRow = dt.NewRow();

                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = row.GetCell(j).ToString();
                    }

                    dt.Rows.Add(dataRow);
                }
                return dt;
            }
            catch
            {
                return dt;
            }
        }

        #endregion

 

转载于:https://www.cnblogs.com/acgk/p/4378495.html

NPOI-2.2.0.0 的.net2.0和.net4.0版。(.net2.0版实测有效) (一)传统操作Excel遇到的问题: 1、如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导出过程中出问题可能导致服务器宕机。 2、Excel会把只包含数字的列进行类型转换,本来是文本型的,Excel会将其转成数值型的,比如编号000123会变成123。 3、导出时,如果字段内容以“-”或“=”开头,Excel会把它当成公式进行,会报错。 4、Excel会根据Excel文件前8行分析数据类型,如果正好你前8行某一列只是数字,那它会认为该列为数值型,自动将该列转变成类似1.42702E+17格式,日期列变成包含日期和数字的。 (二)使用NPOI的优势 1、您可以完全免费使用该框架 2、包含了大部分EXCEL的特性(单元格样式、数据格式、公式等等) 3、专业的技术支持服务(24*7全天候) (非免费) 4、支持处理的文件格式包括xls, xlsx, docx. 5、采用面向接口的设计架构( 可以查看 NPOI.SS 的命名空间) 6、同时支持文件的导入和导出 7、基于.net 2.0 也支持xlsx 和 docx格式(当然也支持.net 4.0) 8、来自全世界大量成功且真实的测试Cases 9、大量的实例代码 11、你不需要在服务器上安装微软的Office,可以避免版权问题。 12、使用起来比Office PIA的API更加方便,更人性化。 13、你不用去花大力气维护NPOI,NPOI Team会不断更新、改善NPOI,绝对省成本。 14、不仅仅对与Excel可以进行操作,对于doc、ppt文件也可以做对应的操作 NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集,这一规律不仅体现在Excel读写库中,也体现在pdf读写库中,目前市面上大部分的pdf库仅支持生成,不支持导入
使用NPOI导入Excel文件可以使用以下步骤: 1. 添加NPOI引用:在Visual Studio中,右键单击项目,选择“管理NuGet程序包”,搜索NPOI并安装。 2. 导入命名空间:在代码文件中添加以下命名空间引用: ``` using NPOI.HSSF.UserModel; // for *.xls files using NPOI.XSSF.UserModel; // for *.xlsx files using NPOI.SS.UserModel; // for both *.xls and *.xlsx files ``` 3. 加载Excel文件:使用以下代码打开Excel文件: ``` string filePath = @"C:\Sample.xlsx"; // file path of the Excel file IWorkbook workbook; // initialize the workbook variable using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (Path.GetExtension(filePath) == ".xls") { workbook = new HSSFWorkbook(fileStream); // for *.xls files } else { workbook = new XSSFWorkbook(fileStream); // for *.xlsx files } } ``` 4. 读取工作表和行:使用以下代码读取工作表和行: ``` ISheet sheet = workbook.GetSheetAt(0); // get the first worksheet IRow row = sheet.GetRow(0); // get the first row ``` 5. 读取单元格值:使用以下代码读取单元格值: ``` string cellValue = row.GetCell(0).ToString(); // get the value of the first cell in the first row ``` 6. 循环读取单元格值:使用以下代码循环读取单元格值: ``` for (int i = 0; i < sheet.LastRowNum; i++) { row = sheet.GetRow(i); // get the i-th row if (row != null) { for (int j = 0; j < row.LastCellNum; j++) { string cellValue = row.GetCell(j).ToString(); // get the value of the j-th cell in the i-th row // do something with the cell value } } } ``` 以上是使用NPOI导入Excel文件的基本步骤。需要注意的是,NPOI只支持读取Excel文件,如果需要写入Excel文件,需要使用另外的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值