加入若干pgsql的接口

This commit is contained in:
2025-12-13 16:33:41 +08:00
parent 97337f31ce
commit f9db3c7226
10 changed files with 1526 additions and 131 deletions

View File

@@ -1,6 +1,9 @@
using NPOI.SS.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.DirectoryServices;
using System.IO;
using System.Windows.Controls;
namespace Ramitta.lib
{
@@ -15,7 +18,7 @@ namespace Ramitta.lib
var result = new List<Dictionary<string, string>>();
// 打开 Excel 文件
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var workbook = new XSSFWorkbook(fs);
ISheet sheet = null;
@@ -87,7 +90,7 @@ namespace Ramitta.lib
var result = new Dictionary<string, List<string>>();
// 打开 Excel 文件
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var workbook = new XSSFWorkbook(fs);
ISheet sheet = null;
@@ -186,6 +189,90 @@ namespace Ramitta.lib
return cell;
}
public static ICell? getRowCell(IRow row, object cellIndex)
{
int actualCellIndex = 0;
if (cellIndex is int intIndex)
{
// 如果输入是数字,直接使用
actualCellIndex = intIndex;
}
else if (cellIndex is string strIndex)
{
actualCellIndex = ColToIndex(strIndex);
}
else
{
return null;
}
if (row == null) return null;
ICell cell = row.GetCell(actualCellIndex);
if (cell == null) return null;
return cell;
}
/// <summary>
/// 获取单元格的字符串值,如果是公式则先计算
/// </summary>
/// <param name="cell">NPOI单元格对象</param>
/// <returns>单元格的值字符串如果为空返回null</returns>
public static string? getFormula(ICell cell)
{
if (cell == null)
return null;
try
{
// 如果是公式单元格
if (cell.CellType == CellType.Formula)
{
// 获取公式计算器
if (cell.Sheet?.Workbook != null)
{
var evaluator = cell.Sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator();
var cellValue = evaluator.Evaluate(cell);
// 将计算结果转为字符串
if (cellValue == null) return null;
switch (cellValue.CellType)
{
case CellType.String:
return cellValue.StringValue?.Trim();
case CellType.Numeric:
return cellValue.NumberValue.ToString();
case CellType.Boolean:
return cellValue.BooleanValue.ToString();
case CellType.Error:
return "#ERROR";
case CellType.Blank:
return null;
default:
return cellValue.FormatAsString()?.Trim();
}
}
else
{
// 没有计算器,获取单元格的字符串表示
return cell.ToString()?.Trim();
}
}
else
{
// 非公式单元格,直接获取字符串表示
return cell.ToString()?.Trim();
}
}
catch (Exception)
{
// 计算出错时返回空
return null;
}
}
public static String? getRowCellStr(ISheet sheet, int rowIndex, object cellIndex)
{
@@ -207,6 +294,149 @@ namespace Ramitta.lib
var cellValue = getRowCell(sheet, rowIndex, actualCellIndex)?.ToString();
return string.IsNullOrWhiteSpace(cellValue) ? null : cellValue;
}
public static String? getRowCellStr(IRow row, object cellIndex,bool Formula=false)
{
int actualCellIndex = 0;
if (cellIndex is int intIndex)
{
// 如果输入是数字,直接使用
actualCellIndex = intIndex;
}
else if (cellIndex is string strIndex)
{
actualCellIndex = ColToIndex(strIndex);
}
else
{
return null;
}
if (Formula) {
return getFormula(getRowCell(row, actualCellIndex));
} else {
var cellValue = getRowCell(row, actualCellIndex)?.ToString();
return string.IsNullOrWhiteSpace(cellValue) ? null : cellValue;
}
}
public static float? getRowCellFloat(ISheet sheet, int rowIndex, object cellIndex)
{
int actualCellIndex = 0;
if (cellIndex is int intIndex)
{
actualCellIndex = intIndex;
}
else if (cellIndex is string strIndex)
{
actualCellIndex = ColToIndex(strIndex);
}
else
{
return null;
}
var cell = getRowCell(sheet, rowIndex, actualCellIndex);
if (cell == null) return null;
// 获取单元格的实际值(不是公式本身)
string cellValue;
if (cell.CellType == CellType.Formula)
{
// 如果是公式,获取公式计算后的值
cellValue = cell.NumericCellValue.ToString();
}
else
{
cellValue = cell.ToString();
}
// 解析为float
if (float.TryParse(cellValue, out float result))
{
return result;
}
return null;
}
public static float? getRowCellFloat(IRow row, object cellIndex)
{
int actualCellIndex = 0;
if (cellIndex is int intIndex)
{
actualCellIndex = intIndex;
}
else if (cellIndex is string strIndex)
{
actualCellIndex = ColToIndex(strIndex);
}
else
{
return null;
}
var cell = getRowCell(row, actualCellIndex);
if (cell == null) return null;
// 获取单元格的实际值(不是公式本身)
string cellValue;
if (cell.CellType == CellType.Formula)
{
// 如果是公式,获取公式计算后的值
cellValue = cell.NumericCellValue.ToString();
}
else
{
cellValue = cell.ToString();
}
// 解析为float
if (float.TryParse(cellValue, out float result))
{
return result;
}
return null;
}
public static double? getRowCellDouble(IRow row, object cellIndex)
{
int actualCellIndex = 0;
if (cellIndex is int intIndex)
{
actualCellIndex = intIndex;
}
else if (cellIndex is string strIndex)
{
actualCellIndex = ColToIndex(strIndex);
}
else
{
return null;
}
var cell = getRowCell(row, actualCellIndex);
if (cell == null) return null;
// 获取单元格的实际值(不是公式本身)
string cellValue;
if (cell.CellType == CellType.Formula)
{
// 如果是公式,获取公式计算后的值
cellValue = cell.NumericCellValue.ToString();
}
else
{
cellValue = cell.ToString();
}
if (double.TryParse(cellValue, out double result))
{
return result;
}
return null;
}
// 简短版本
// 列名字转为列号
@@ -214,5 +444,49 @@ namespace Ramitta.lib
{
return col.ToUpper().Aggregate(0, (cur, ch) => cur * 26 + (ch - 'A'));
}
public static ICellStyle CreateStyle(
IWorkbook workbook,
bool Border = true,
short? fontSize = null,
bool isBold = false,
bool isItalic = false,
string fontName = "宋体",
HorizontalAlignment hAlign = HorizontalAlignment.Left, // 水平对齐
VerticalAlignment vAlign = VerticalAlignment.Center) // 垂直对齐) // 新增:字体名称,默认为宋体
{
ICellStyle style = workbook.CreateCellStyle();
if (Border)
{
// 设置边框
style.BorderTop = BorderStyle.Thin;
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
}
// 设置对齐方式
style.Alignment = hAlign;
style.VerticalAlignment = vAlign;
// 创建字体
IFont font = workbook.CreateFont();
// 设置字体名称
font.FontName = fontName;
// 设置字号
if (fontSize.HasValue)
font.FontHeightInPoints = fontSize.Value;
// 设置粗体斜体
font.IsBold = isBold;
font.IsItalic = isItalic;
style.SetFont(font);
return style;
}
}
}