• 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,然后安装,重新编译,调试

    你应该了解真相,真相使你自由!
  • 相关阅读:
    git ssh配置
    spring事务的传播行为
    springboot tomcat启动
    error while loading shared libraries: libstdc++.so.6: cannot open shared object file
    centos7安装nginx1
    linux jdk 配置
    NET CORE 3.1 UPLOAD
    vue table formrt datetime languer
    net core 3.1 swagger
    css
  • 原文地址:https://www.cnblogs.com/Hooper_he/p/9691289.html
Copyright © 2020-2023  润新知