OfficeTool 1.2.0

dotnet add package OfficeTool --version 1.2.0                
NuGet\Install-Package OfficeTool -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OfficeTool" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OfficeTool --version 1.2.0                
#r "nuget: OfficeTool, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OfficeTool as a Cake Addin
#addin nuget:?package=OfficeTool&version=1.2.0

// Install OfficeTool as a Cake Tool
#tool nuget:?package=OfficeTool&version=1.2.0                

This is an Office tool, currently available functions include reading Excel data, generating Excel files, etc...

Examples

new OfficeTool.Samples.CreateExcelSimpleExample().Create();
new OfficeTool.Samples.CreateExcelExample().Create();
new OfficeTool.Samples.CreateExcelCustomExample().Create();
string filePath = "D:\\c#\\ConsoleApp\\bin\\Debug\\net6.0\\officeTemp\\20240604.xlsx";
var data = new OfficeTool.Samples.ReadExcelExample().Read(filePath);
var allData = new OfficeTool.Samples.ReadExcelAllExample().Read(filePath);

Demo

using NPOI.SS.UserModel;
using OfficeTool.ExcelHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static OfficeTool.Models.Excel;


class Program
{
    static void Main()
    {
            //依赖注入
            IExcelHelper excelHelper;

            excelHelper = new OfficeTool.ExcelHelpers.ExcelHelper();

            var basePath = AppDomain.CurrentDomain.BaseDirectory;
            var filePath = Path.Combine(basePath, "file");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            //读取单个Sheet并返回IEnumerable<T>
            var readData = excelHelper.ReadExcel<ExcelDemoModel>(readFilePath, ExcelDemoModel.ExcelHeader);
            //读取全部Sheet并返回List<IEnumerable<T>>
            var allData = excelHelper.ReadSheets<ExcelDemoModel>(readFilePath, ExcelDemoModel.ExcelHeader);

            //Excel数据源
            var insertData = new List<ExcelDemoModel>
            {
                new ExcelDemoModel() { Age = 20, City = "aa", Name = "张三" },
                new ExcelDemoModel() { Age = 22, City = "bb", Name = "李四" },
                new ExcelDemoModel() { Age = 25, City = "cc", Name = "王五" }
            };

            //简单导出
            excelHelper.CreateExcel(Path.Combine(filePath + "", $"{DateTime.Now:yyyyMMddHHmmssfff}.xlsx"), ExportHeaders, insertData);


            //自定义导出
            var wb = excelHelper.CreateWorkbook();
            var sheet1 = wb.CreateSheet("Test");
            //设置标题
            excelHelper.SetTitle(wb, sheet1, "班级人员年龄统计表", ExportHeaders.Count() - 1);
            //渲染表格
            excelHelper.RenderTable(sheet1, ExportHeaders, insertData);
            //增加统计行
            excelHelper.CreateRow(sheet1, (row) =>
            {
                for (int i = 0; i < ExportHeaders.Count(); i++)
                {
                    var cell = row.CreateCell(i);
                    excelHelper.SetCellValue(cell, DataType.String, i == 0 ? "平均年龄" : $"{Math.Ceiling(insertData.Sum(item => item.Age) / (insertData.Count() * 1.0))}岁");
                    excelHelper.SetCellStyle(cell, excelHelper.CreateStyle(wb: wb, border: true));
                }
            });
            //合并单元格
            excelHelper.MergedCells(sheet1, sheet1.LastRowNum, sheet1.LastRowNum, 0, 1);
            excelHelper.MergedCells(sheet1, sheet1.LastRowNum, sheet1.LastRowNum, 2, 3);
            //导出文件
            excelHelper.Save(wb, Path.Combine(filePath + "", $"{DateTime.Now:yyyyMMddHHmmssfff}.xlsx"));


        }

        //渲染单元格样式
        public static Func<IWorkbook, ICell, ExcelDemoModel, int, int, int, object> ExportHeadersFilter = (wookbook, cell, data, dataIndex, rowIndex, cellIndex) =>
        {
            var cellStyle = wookbook.OTCreateStyle(fontColor: NPOI.HSSF.Util.HSSFColor.Red.Index, bold: true);
            if (data.Age < 25) cell.CellStyle = cellStyle;
            return "";
        };
        public static IEnumerable<Header<ExcelDemoModel>> ExportHeaders => new List<Header<ExcelDemoModel>>
        {
            new Header<ExcelDemoModel> { Title = "序号", DataSource = "Index", ShowIndex = true, RenderCellStyle = ExportHeadersFilter},
            new Header<ExcelDemoModel> { Title = "姓名", DataSource = "Name", Width = 3600, RenderCellStyle = ExportHeadersFilter },
            new Header<ExcelDemoModel> { Title = "年龄", DataSource = "Age", DataType = DataType.Number, RenderCellStyle = ExportHeadersFilter},
            new Header<ExcelDemoModel> { Title = "城市", DataSource = "City", Render = (data) => data.City, RenderCellStyle = ExportHeadersFilter }
        };

        public class ExcelDemoModel
        {

            public string Name { get; set; }
            public int Age { get; set; }
            public string City { get; set; }

            public static Dictionary<string, string> ExcelHeader =
                new Dictionary<string, string>()
                {
                {"姓名","Name" },
                {"年龄","Age" },
                {"城市","City" },
                };

        }

}

Effect

效果图

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 118 9/6/2024
1.1.0 102 6/6/2024
1.0.5 93 6/6/2024
1.0.4 101 6/5/2024
1.0.3 95 6/5/2024
1.0.2 99 6/5/2024
1.0.1 98 6/4/2024
1.0.0 98 6/4/2024

This is a convenient tool for reading and generating Office Excel.