Spire.XLS 操作Excel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 @   互联网CV工程师  阅读(1103)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
点击右上角即可分享
微信分享提示