C#读写Excel表格文件NPOI方式无需安装office .xls后缀没问题

  /// <summary>
        /// 读Excel
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static DataTable getexcel(String fileName)
        {
            DataTable dt = new DataTable();
            IWorkbook workbook = null; //新建IWorkbook对象 
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            if (fileName.IndexOf(".xlsx") > 0) // 2007版本 
            {
                workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook 
            }
            else if (fileName.IndexOf(".xls") > 0) // 2003版本 
            {
                workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook 
            }
            ISheet sheet = workbook.GetSheetAt(0); //获取第一个工作表 
            IRow row;// = sheet.GetRow(0); //新建当前工作表行数据 
            // MessageBox.Show(sheet.LastRowNum.ToString());
            row = sheet.GetRow(0); //row读入头部
            if (row != null)
            {
                for (int m = 0; m < row.LastCellNum; m++) //表头 
                {
                    string cellValue = row.GetCell(m).ToString(); //获取i行j列数据 
                    Console.WriteLine(cellValue);
                    dt.Columns.Add(cellValue);
                }
            }
            for (int i = 1; i <= sheet.LastRowNum; i++) //对工作表每一行 
            {
                System.Data.DataRow dr = dt.NewRow();
                row = sheet.GetRow(i); //row读入第i行数据 
                if (row != null)
                {
                    for (int j = 0; j < row.LastCellNum; j++) //对工作表每一列 
                    {
                        string cellValue = row.GetCell(j).ToString(); //获取i行j列数据 
                        Console.WriteLine(cellValue);
                        dr[j] = cellValue;
                    }
                }
                dt.Rows.Add(dr);
            }
            Console.ReadLine();
            fileStream.Close();
            return dt;
        }

  

        /// <summary>
        /// 将datatable对象保存为Excel文件
        /// 提供Excel保存路径及datatable数据对象,成功返回真,失败返回假。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool DataTableToExcel(String path, DataTable dt)
        {
            bool result = false;
            IWorkbook workbook = null;
            FileStream fs = null;
            IRow row = null;
            ISheet sheet = null;
            ICell cell = null;
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    workbook = new HSSFWorkbook();
                    sheet = workbook.CreateSheet("Sheet0");//创建一个名称为Sheet0的表 
                    int rowCount = dt.Rows.Count;//行数 
                    int columnCount = dt.Columns.Count;//列数

                    //设置列头 
                    row = sheet.CreateRow(0);//excel第一行设为列头 
                    for (int c = 0; c < columnCount; c++)
                    {
                        cell = row.CreateCell(c);
                        cell.SetCellValue(dt.Columns[c].ColumnName);
                    }

                    //设置每行每列的单元格, 
                    for (int i = 0; i < rowCount; i++)
                    {
                        row = sheet.CreateRow(i + 1);
                        for (int j = 0; j < columnCount; j++)
                        {
                            cell = row.CreateCell(j);//excel第二行开始写入数据 
                            cell.SetCellValue(dt.Rows[i][j].ToString());
                        }
                    }
                    using (fs = File.OpenWrite(path))
                    {
                        workbook.Write(fs);//向打开的这个xls文件中写入数据 
                        result = true;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                if (fs != null)
                {
                    fs.Close();
                }
                return false;
            }
        }

  添加引用到你的工程中,并使用using字段进行引用。

 

报错:未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf116

解决方案:删除所有引用的NPOI相关的dll,直接右键工程名称,点击“管理NuGet程序包”,搜索NPOI,然后安装,重新编译,调试

 

posted @ 2018-09-22 21:31  haiping_he  阅读(666)  评论(0编辑  收藏  举报