NPOI读写execl

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace UpdateData.Common
{
    public class WriterChangeCell
    {
        /// <summary>
        /// 将字符串数组写入EXCEL
        /// </summary>
        /// <param name="xlFile">EXCEL文件</param>
        /// <param name="sheetName">EXCEL表sheet名称</param>
        /// <param name="changeDateEntities">输入修改位置和修改内容</param>
        /// <returns>EXCEL文件是否成功</returns>
        public void WriteExcel(string xlFile, string sheetName, List<ChangeDateEntity> changeDateEntities)
        {
            try
            {
                var extension = Path.GetExtension(xlFile).ToLower();
                IWorkbook wk = null;

                using (FileStream fs = File.Open(xlFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                {
                    if (extension == ".xls")
                    {
                        wk = new HSSFWorkbook(fs);
                    }
                    else if (extension == ".xlsx")
                    {
                        wk = new XSSFWorkbook(fs);
                    }
                }

                ISheet sheet = wk.GetSheet(sheetName);

                foreach (var item in changeDateEntities)
                {
                    if (string.IsNullOrEmpty(item.DataValue))
                        continue;
                    //获取行
                    IRow row = sheet.GetRow(item.RowIndex);
                    //在列单元格  
                    ICell cell = row.GetCell(item.ColumnIndex);

                    cell.SetCellValue(item.DataValue);
                }

                MemoryStream stream = new MemoryStream();
                wk.Write(stream);
                var buf = stream.ToArray();

                //保存为Excel文件  
                using (FileStream fs = new FileStream(xlFile, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(buf, 0, buf.Length);
                    fs.Flush();
                }

                wk.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public List<string> ReaderExcel(string xlFile, List<TableLocation> changeDateEntities)
        {
            List<string> infoList = new List<string>();
            try
            {
                var extension = Path.GetExtension(xlFile).ToLower();
                IWorkbook wk = null;

                using (FileStream fs = File.Open(xlFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
                {
                    if (extension == ".xls")
                    {
                        wk = new HSSFWorkbook(fs);
                    }
                    else if (extension == ".xlsx")
                    {
                        wk = new XSSFWorkbook(fs);
                    }
                }

                ISheet sheet = wk.GetSheetAt(0);

                foreach (var item in changeDateEntities)
                {
                    //获取行
                    IRow row = sheet.GetRow(item.RowIndex);
                    //在列单元格  
                    ICell cell = row.GetCell(item.ColumnIndex);

                    string unit = "";
                    if (cell.CellType == CellType.Numeric)
                    {
                        if (DateUtil.IsCellDateFormatted(cell))
                        {
                            unit = cell.DateCellValue.ToString("D");
                        }
                        else
                        {
                            unit = cell.ToString();
                        }
                    }
                    else if (cell.CellType == CellType.Blank)//空数据类型
                    {
                        unit = "";
                    }
                    else if (cell.CellType == CellType.Formula)//公式类型
                    {
                        IFormulaEvaluator eva = null;
                        if (extension == ".xls")
                        {
                            eva = new HSSFFormulaEvaluator(wk);
                        }
                        else if (extension == ".xlsx")
                        {
                            eva = new XSSFFormulaEvaluator(wk);
                        }
                        unit = eva.Evaluate(cell).StringValue;
                    }
                    else //其他类型都按字符串类型来处理
                    {
                        unit = cell.StringCellValue;
                    }

                    infoList.Add(unit);

                }
                wk.Close();
                return infoList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值