Spire.XLS 操作Excel

using Spire.Xls;
using System.Linq;

namespace WrapperPrint.Tools
{
    /// <summary>
    /// 因为npoi应不会真正删除行,只会清除数据,导致打印出来空标签
    /// 所以增加此帮助类删除数据。
    /// nuget引入FreeSpire.XLS组件
    /// </summary>
    public class SpireEexcelHelper
    {
        private Workbook workbook;
        private Worksheet sheet;
        public string FileName { get; set; }

        public SpireEexcelHelper(string fileName)
        {
            this.workbook = new Workbook();
            workbook.LoadFromFile(fileName);
            this.sheet = workbook.Worksheets[0];
            FileName = fileName;
        }
        /// <summary>
        /// 删除指定行
        /// </summary>
        /// <param name="startIndex">开始行</param>
        /// <param name="endIndex">结束行</param>
        public void DeleteRows(int startIndex, int endIndex)
        {
            for (int i = startIndex; i <= endIndex; i++)
            {
                sheet.DeleteRow(i + 1);//索引从0开始
            }
        }
        public void DeleteRows(int startIndex)
        {
            int endIndex = GetRowCount();
            sheet.DeleteRow(startIndex + 1, endIndex);
        }
        public int GetRowCount()
        {
            return sheet.Rows.Count();
        }
        public void Save()
        {
            //要保存成老版本的
            workbook.SaveToFile(FileName, ExcelVersion.Version97to2003);
            workbook.Dispose();
        }
    }
}

  

posted @ 2022-08-09 15:59  互联网CV工程师  阅读(1066)  评论(0编辑  收藏  举报