Hanson.Common.ExcelUtils 2.0.0

dotnet add package Hanson.Common.ExcelUtils --version 2.0.0                
NuGet\Install-Package Hanson.Common.ExcelUtils -Version 2.0.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="Hanson.Common.ExcelUtils" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hanson.Common.ExcelUtils --version 2.0.0                
#r "nuget: Hanson.Common.ExcelUtils, 2.0.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 Hanson.Common.ExcelUtils as a Cake Addin
#addin nuget:?package=Hanson.Common.ExcelUtils&version=2.0.0

// Install Hanson.Common.ExcelUtils as a Cake Tool
#tool nuget:?package=Hanson.Common.ExcelUtils&version=2.0.0                

README

Hanson.Excel.Utils

主要功能

  1. 提供讀取 Excel
  • 支援讀取 Excel 97 檔案格式 (.xls)

  • 支援讀取 Excel 2007 檔案格式 (.xlsx)

  • 支援讀取 Excel Sheet 清單

  • 支援指定 Sheet 名稱讀取資料

  1. 提供 匯出 Excel
  • 支援匯出 Excel 97 檔案格式 (.xls)

  • 支援匯出 Excel 2007 檔案格式 (.xlsx)

  • 支援指定 Excel Style 功能

前置條件

  • 開發環境需具備 .Net8.0
  • 運行於 Windows Platform (x64)
  • 運行於 Linux Platform (x64)
  • 運行於 Raspberry PI Platform (x64)

安裝方式

授權

此專案採用的 License為 Apache-2.0

使用範例

  • 讀取Excel 傳回 DataTable
private void GetExcelData()
{
    var path = @"C:\temp\temp.xlsx";
    
    // 讀取 Excel 資料 - 預計讀取第一個 Sheet
    DataTable table = ExcelUtils.ReadExcel(path);
    
    // 讀取 Excel 資料 - 指定讀取檔案格式
    table = ExcelUtils.ReadExcel(path,ExcelType.Excel2007);
    
    // 讀取所有 sheet 名稱清單
    var sheetNames = ExcelUtils.ReadExcelSheets(path);
    // 讀取 Excel 資料 - 指定 sheet 名稱
    table = ExcelUtils.ReadExcel(path, sheetName: sheetNames[0]);

}
  • 讀取Excel 傳回 指定範型
using System.ComponentModel;
public class ExcelClass
{
    [Description("id")] //對應 Excel 標題名稱
    public string Id { get; set; }

    [Description("code")]
    public string Code { get; set; }

    [Description("emp Name")]
    public string Name { get; set; }

    [Description("birthday")]
    public DateTime Birthday { get; set; }

    [Description("age")]
    public double Age { get; set; }

    [Description("amount")]
    public double Amount { get; set; }
}

private void GetExcelClassData()
{
    var path = @"C:\temp\temp.xlsx";

    // 讀取 Excel 資料 - 預計讀取第一個 Sheet
    var table = ExcelUtils.ReadExcel<ExcelClass>(path);
    
    // 讀取 Excel 資料 - 指定讀取檔案格式
    table = ExcelUtils.ReadExcel<ExcelClass>(path,ExcelType.Excel2007);

    // 讀取所有 sheet 名稱清單
    var sheetNames = ExcelUtils.ReadExcelSheets(path);
    // 讀取 Excel 資料 - 指定 sheet 名稱
    table = ExcelUtils.ReadExcel<ExcelClass>(path, sheetName: sheetNames[0]);

}
  • 簡易匯出 Excel 的方式
private static void ExportData()
{
    var src = @"C:\temp\temp.xlsx";
    var dst = @"C:\temp\export.xlsx";
    var table = ExcelUtils.ReadExcel<ExcelClass>(src);
   
    // 匯出 Excel 檔案
    ExcelUtils.Export(table, dst);
    
    // 匯出 Excel 檔案 - 指定匯出格式
    ExcelUtils.Export(table, dst,ExcelType.Excel2007);
    
    // 匯出 Excel 檔案 - 指定匯出 sheet 名稱
    ExcelUtils.Export(table, dst,sheetName:"export");
}
  • 指定 Style 的 Excel 匯出方式
private void ExportStyleExcel
{
    var src = @"C:\temp\temp.xlsx";
    var dst = @"C:\temp\export.xlsx";

    List<ExcelClass> values = ExcelUtils.ReadExcel<ExcelClass>(src, ExcelType.Excel2007);

    //依據 values 產生要匯出的 Excel Sheet 的資料介面
    ISheet sheetData = ExcelUtils.GeneratedExportSheet(values, ExcelType.Excel2007);

    // 建立 Cell Style 介面
    ICellStyle cellStyle = sheetData.Workbook.CreateCellStyle();

    // 設定水平對齊方式
    cellStyle.Alignment = HorizontalAlignment.Center;

    // 字型設定
    IFont fontStyle = sheetData.Workbook.CreateFont();
    fontStyle.FontName = "Arial";
    fontStyle.FontHeightInPoints = 14;
    fontStyle.IsBold = true;
    fontStyle.IsItalic = false;
    fontStyle.IsStrikeout = false;
    cellStyle.SetFont(fontStyle);

    //Cell 前景顏色
    cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
    cellStyle.FillPattern = FillPattern.SolidForeground;

    // 指定 Style 套用於標題位置
    ExcelUtils.SetHeaderStyle(sheetData, cellStyle);

    // 指定 Style 套用的 位置 (row,column)
    ExcelUtils.SetStyle(sheetData, cellStyle,row:3,column:5);

    // 匯出套用 Style 的 ISheet 內容
    ExcelUtils.Export(sheetData, dst);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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
2.0.0 79 11/30/2024
1.0.1 118 4/8/2024