C#使用NPOI对EXCle增 删 改 查 方法

最近闲来无事封装了使用NPOI对excle文件增删改查的方法,亲测可用附代码:

   /// <summary>
        /// 根据路径和表名得到Dictionary数据字典
        /// </summary>
        /// <param name="excelPath"></param>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public Dictionary<int, Dictionary<string, string>> GetDictionaryBySheetName(string excelPath, string sheetName)
        {
            Dictionary<int, Dictionary<string, string>> sheetDic = new Dictionary<int, Dictionary<string, string>>();
            DataTable dt = GetDataTableBySheetName(excelPath, sheetName);
            if (dt.Rows.Count <= 0) return sheetDic;
            //1 获取表头
            int index = 0;
            foreach (DataRow itemRow in dt.Rows)
            {
                Dictionary<string, string> rowDic = new Dictionary<string, string>();
                for (int columnsI = 0; columnsI < dt.Columns.Count; columnsI++)
                {
                    rowDic.Add(dt.Columns[columnsI].ToString(), itemRow[columnsI].ToString());
                }
                sheetDic.Add(index, rowDic);
                index++;
            }
            return sheetDic;
        }

        /// <summary>
        /// 根据路径和表名得到datatabel
        /// </summary>
        /// <param name="excelPath"></param>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public DataTable GetDataTableBySheetName(string excelPath, string sheetName)
        {
            DataTable dt = new DataTable();
            dt.TableName = sheetName;
            IWorkbook workbook = GetOpenWorkbookByExtension(excelPath);
            ISheet sheet;
            if (!TryPassWorkbook(workbook, sheetName, out sheet)) return dt;
            //1 读取Excel 第一行创建列头
            IRow headRow = sheet.GetRow(0);    //如果需要指定列头  可以在此处传入参数(修改该方法)
            ICell cell = null;
            string tempStr = string.Empty;
            for (int columnI = 0; columnI < headRow.LastCellNum; columnI++)
            {
                cell = headRow.GetCell(columnI);
                tempStr = cell == null ? columnI.ToString() : cell.ToString();
                if (dt.Columns.Contains(tempStr))
                {
                    throw new Exception(string.Format("EXCLE表{0}存在相同列属性【{1}】", sheet.SheetName, tempStr));
                    //处理excel列值相同问题 
                }
                else
                {
                    dt.Columns.Add(tempStr, typeof(string));
                }

            }
            IRow row = null;
            //2 填充数据
            for (int rowI = 1; rowI <= sheet.LastRowNum; rowI++)
            {
                row = sheet.GetRow(rowI);
                DataRow dtRow = dt.NewRow();
                if (null == row) continue;
                //3 遍历单元格
                for (int columJ = 0; columJ < dt.Columns.Count; columJ++)
                {
                    cell = row.GetCell(columJ);
                    if (0 == columJ && (null == cell || "##" == cell.ToString())) break;
                    if (null == cell) continue;
                    tempStr = cell.ToString();
                    dtRow[columJ] = tempStr;
                }
                dt.Rows.Add(dtRow);

            }

            return dt;
        }

        /// <summary>
        /// 移除工作薄中Sheet
        /// </summary>
        /// <param name="excelPath">文件路径</param>
        /// <param name="sheetName">表名</param>
        /// <returns>返回结果</returns>
        public bool RemoveSheetByName(string excelPath, string sheetName)
        {

            //1 得到工作簿
            IWorkbook workbook = GetOpenWorkbookByExtension(excelPath);
            if (null == workbook) return false;
            int sheetIndex = workbook.GetSheetIndex(sheetName);
            if (sheetIndex < 0) return false;
            workbook.RemoveSheetAt(sheetIndex);
            return SaveExcelFile(excelPath, workbook);

        }
        public bool RemoveSheetByName(string excelPath, int sheetIndex)
        {
            //1 得到工作簿
            IWorkbook workbook = GetOpenWorkbookByExtension(excelPath);
            if (null == workbook) return false;
            if (workbook.NumberOfSheets < sheetIndex) return false;
            workbook.RemoveSheetAt(sheetIndex);
            return SaveExcelFile(excelPath, workbook);
        }

有需要可以下载(添加现有项就能用):https://download.csdn.net/download/weixin_43542114/12424263

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值