Files
Ramitta-lib/template/MainWindow.xaml.cs

222 lines
7.0 KiB
C#
Raw Normal View History

2025-09-12 18:34:33 +08:00
using Neo4j.Driver;
using Newtonsoft.Json;
2025-09-03 16:16:00 +08:00
using Newtonsoft.Json.Linq;
2025-09-27 11:02:31 +08:00
using NPOI.HSSF.UserModel;
2025-09-12 18:34:33 +08:00
using NPOI.SS.Formula.Functions;
2025-09-27 11:02:31 +08:00
using NPOI.SS.UserModel;
using NPOI.XSSF.Streaming.Values;
using NPOI.XSSF.UserModel;
2025-08-30 21:14:02 +08:00
using Ramitta;
2025-09-12 18:34:33 +08:00
using Ramitta.lib;
using System.Collections.Generic;
2025-08-30 21:14:02 +08:00
using System.Data.Common;
2025-09-12 18:34:33 +08:00
using System.Data.SQLite;
2025-08-29 14:57:55 +08:00
using System.Diagnostics;
2025-09-03 16:16:00 +08:00
using System.IO;
2025-09-12 18:34:33 +08:00
using System.Linq;
using System.Security.Cryptography;
using System.Security.Policy;
2025-08-29 14:57:55 +08:00
using System.Text;
2025-09-12 18:34:33 +08:00
using System.Text.RegularExpressions;
2025-08-29 14:57:55 +08:00
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;
2025-08-30 21:14:02 +08:00
using System.Xml.Linq;
2025-09-12 18:34:33 +08:00
using static Ramitta.Excel;
2025-08-30 21:14:02 +08:00
using static Ramitta.lib.Basic;
2025-09-12 18:34:33 +08:00
using static Ramitta.lib.CryptoHelper;
using static Ramitta.SQLite;
2025-08-30 21:14:02 +08:00
using static Ramitta.winDataGrid;
2025-08-29 14:57:55 +08:00
namespace template
{
public partial class MainWindow : Window
{
2025-09-27 11:02:31 +08:00
public class XlsxRows
{
public string { get; set; }
public string { get; set; }
public float { get; set; }
public List<string> { get; set; }
public string { get; set; }
}
2025-08-29 14:57:55 +08:00
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, $"未指定操作目标", );
}
2025-08-30 21:14:02 +08:00
2025-08-29 14:57:55 +08:00
}
#endregion
2025-09-27 11:02:31 +08:00
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返回值");
}
2025-09-12 18:34:33 +08:00
private async void (object sender, RoutedEventArgs e)
2025-08-29 14:57:55 +08:00
{
2025-09-27 11:02:31 +08:00
var ret=EvaluateFormulaExample(@"C:\Users\Xeroly\Desktop\newmagg.xlsx");
foreach (var cell in ret) {
Debug.WriteLine($"{cell.物号}->{cell.名称描述}:{cell.数量}");
}
2025-08-29 14:57:55 +08:00
}
2025-09-12 18:34:33 +08:00
private async void (object sender, RoutedEventArgs e)
2025-08-29 14:57:55 +08:00
{
}
2025-09-12 18:34:33 +08:00
private async void (object? sender = null, RoutedEventArgs? e=null)
2025-08-29 14:57:55 +08:00
{
2025-09-27 11:02:31 +08:00
2025-08-29 14:57:55 +08:00
}
2025-09-12 18:34:33 +08:00
private async void (object sender, RoutedEventArgs e)
2025-08-29 14:57:55 +08:00
{
2025-09-27 11:02:31 +08:00
}
private void (object sender, RoutedEventArgs e)
{
2025-08-29 14:57:55 +08:00
2025-09-27 11:02:31 +08:00
}
2025-09-12 18:34:33 +08:00
2025-09-27 11:02:31 +08:00
private void (object sender, RoutedEventArgs e)
{
2025-09-12 18:34:33 +08:00
2025-09-27 11:02:31 +08:00
}
2025-09-12 18:34:33 +08:00
2025-09-27 11:02:31 +08:00
private void (object sender, RoutedEventArgs e)
{
}
private void (object sender, RoutedEventArgs e)
{
2025-09-03 16:16:00 +08:00
2025-09-12 18:34:33 +08:00
}
}
2025-08-29 14:57:55 +08:00
}