using NPOI.HSSF.UserModel;
using NPOI.SS.Formula;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using System.Linq;
namespace YBEE.EQM.Core;
///
/// Excel操作工具
///
public static class ExcelUtil
{
///
/// 获取 Workbook 实例
///
///
///
///
public static IWorkbook GetWorkbook(string filePath, FileStream fileStream)
{
string fileExt = Path.GetExtension(filePath).ToLower();
if (fileExt == ".xls")
{
return new HSSFWorkbook(fileStream);
}
else
{
return new XSSFWorkbook(fileStream);
}
}
///
/// 创建 Workbook
///
///
///
public static IWorkbook GetWorkbook(string filePath)
{
string fileExt = Path.GetExtension(filePath).ToLower();
if (fileExt == ".xls")
{
return new HSSFWorkbook();
}
else
{
return new XSSFWorkbook();
}
}
///
/// 获取公式计算器
///
///
///
public static BaseFormulaEvaluator GetEvaluator(IWorkbook workbook)
{
if (workbook is HSSFWorkbook)
{
return new HSSFFormulaEvaluator(workbook);
}
return new XSSFFormulaEvaluator(workbook);
}
///
/// 根据列索引获取字母列名
///
///
///
public static string GetColumnNameByIndex(int index)
{
int num = index;
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray();
string str = "";
while (num >= 0)
{
var n = letters[num % 26];
str = $"{n}{str}";
if (num == 0)
{
break;
}
num = (int)(Math.Floor(num * 1.0 / 26) - 1);
}
return str;
}
}