Files
Ramitta-lib/template/MainWindow.xaml.cs
2025-09-27 11:02:31 +08:00

222 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Neo4j.Driver;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.Streaming.Values;
using NPOI.XSSF.UserModel;
using Ramitta;
using Ramitta.lib;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;
using static Ramitta.Excel;
using static Ramitta.lib.Basic;
using static Ramitta.lib.CryptoHelper;
using static Ramitta.SQLite;
using static Ramitta.winDataGrid;
namespace template
{
public partial class MainWindow : Window
{
public class XlsxRows
{
public string { get; set; }
public string { get; set; }
public float { get; set; }
public List<string> { get; set; }
public string { get; set; }
}
static string? filePath;
#region MainWindow
public MainWindow(StartupEventArgs e)
{
Startupe = ParseCommandLineArgs(e.Args);
InitializeComponent();
if (Startupe.TryGetValue("getfile", out string filePath))
{
MainWindow.filePath = filePath;
DebugBar(Debugtag, $"操作目标:{filePath}", 绿);
}
else
{
DebugBar(Debugtag, $"未指定操作目标", );
}
}
#endregion
public static List<XlsxRows> EvaluateFormulaExample(string filePath, string? checkTitle = null)
{
IWorkbook workbook;
// 1. 根据文件扩展名创建正确的 Workbook 实例
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (System.IO.Path.GetExtension(filePath).ToLower() == ".xlsx")
{
workbook = new XSSFWorkbook(fs);
}
else
{
workbook = new HSSFWorkbook(fs); // 用于 .xls
}
}
// 2. 获取第一个工作表
ISheet sheet = workbook.GetSheetAt(0);
ISheet? sheetRelate = null;
if (workbook.NumberOfSheets > 1)
{
sheetRelate = workbook.GetSheetAt(1); // 获取第二个工作表
}
// 3. 公式表化
if (sheetRelate != null)
{
Dictionary<string, ICell> valueDictionary = new Dictionary<string, ICell>();
for (int i = 0; i <= sheetRelate.LastRowNum; i++)
{
IRow row = sheetRelate.GetRow(i);
if (row != null)
{
ICell keyCell = row.GetCell(0);
ICell valueCell = row.GetCell(1);
if (keyCell != null && valueCell != null)
{
string key = keyCell.ToString();
valueDictionary[key] = valueCell; // 存入字典
}
}
}
valueDictionary["单元数"]?.SetCellValue(50);
}
// 4. 遍历跳过行首
List<XlsxRows> result= new List<XlsxRows>();
for (int i = 1; i <= sheet.LastRowNum; i++) {
XlsxRows member = new();
member. = sheet.GetRow(i).GetCell(0)?.ToString() ?? "";
member. = sheet.GetRow(i).GetCell(1)?.ToString() ?? "";
member. = new();
member. = sheet.GetRow(i).GetCell(4)?.ToString() ?? "";
try {
member. = xlsxEvaluatorDouble(
workbook,
sheet.GetRow(i).GetCell(2));
result.Add(member);
}
catch {
member. = -1;
}
}
// 5. 关闭工作簿(如果不再需要)
workbook.Close();
return result;
}
public static float xlsxEvaluatorDouble(IWorkbook workbook,ICell cell) {
IFormulaEvaluator evaluator = workbook.GetCreationHelper().CreateFormulaEvaluator();
// 5. 判断单元格类型是否为公式
if (cell.CellType == CellType.Formula)
{
// 6. 计算公式并获取计算后的单元格值CellValue
CellValue evaluatedCellValue = evaluator.Evaluate(cell);
// 7. 根据计算结果的类型获取值
switch (evaluatedCellValue.CellType)
{
case CellType.Numeric:
float numericValue = (float)(evaluatedCellValue.NumberValue);
return numericValue;
break;
case CellType.String:
string stringValue = evaluatedCellValue.StringValue;
break;
case CellType.Boolean:
bool boolValue = evaluatedCellValue.BooleanValue;
break;
case CellType.Error:
// 处理错误值
Debug.WriteLine("公式计算错误");
break;
default:
Debug.WriteLine("未知类型的公式结果");
break;
}
}
else
{
return (float)cell.NumericCellValue;
}
throw new NotImplementedException("并非都是double返回值");
}
private async void (object sender, RoutedEventArgs e)
{
var ret=EvaluateFormulaExample(@"C:\Users\Xeroly\Desktop\newmagg.xlsx");
foreach (var cell in ret) {
Debug.WriteLine($"{cell.物号}->{cell.名称描述}:{cell.数量}");
}
}
private async void (object sender, RoutedEventArgs e)
{
}
private async void (object? sender = null, RoutedEventArgs? e=null)
{
}
private async void (object sender, RoutedEventArgs e)
{
}
private void (object sender, RoutedEventArgs e)
{
}
private void (object sender, RoutedEventArgs e)
{
}
private void (object sender, RoutedEventArgs e)
{
}
private void (object sender, RoutedEventArgs e)
{
}
}
}