日常更新
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
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;
|
||||
@@ -36,6 +40,15 @@ 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
|
||||
@@ -57,47 +70,152 @@ namespace template
|
||||
}
|
||||
#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)
|
||||
{
|
||||
var properties = new Dictionary<string, string>
|
||||
{
|
||||
{ "文件夹", "承重柱" }
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
var newmore = await neo4jService.GetRelatedNodesAsync("文件", properties);
|
||||
Debug.WriteLine("=== ☆ ===");
|
||||
private void 载入(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
HashSet<string> 北平 = new HashSet<string>();
|
||||
foreach (var nodeDict in newmore)
|
||||
{
|
||||
if (nodeDict.ContainsKey("型号") && !string.IsNullOrEmpty(nodeDict["型号"]))
|
||||
{
|
||||
北平.Add(nodeDict["型号"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 输出结果
|
||||
Debug.WriteLine($"不重复的型号数量: {北平.Count}");
|
||||
private void 卸载(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
foreach (var 型号 in 北平)
|
||||
{
|
||||
Debug.WriteLine($"型号: {型号}");
|
||||
}
|
||||
}
|
||||
|
||||
private void 转录(object sender, RoutedEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
private void 逆转(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user