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 标记 { 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 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 valueDictionary = new Dictionary(); 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 result= new List(); 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) { } } }