2024-11-21 16:28:36 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Unicode;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Avalonia.Controls;
|
2024-11-26 15:44:31 +08:00
|
|
|
|
using Avalonia.Controls.Notifications;
|
|
|
|
|
using detect.gui.VWMS;
|
2024-11-21 16:28:36 +08:00
|
|
|
|
using OfficeOpenXml;
|
2024-11-26 15:44:31 +08:00
|
|
|
|
using Splat;
|
2024-11-21 16:28:36 +08:00
|
|
|
|
|
|
|
|
|
namespace detect.gui.Views;
|
|
|
|
|
|
|
|
|
|
public class WebViewService(Window? w)
|
|
|
|
|
{
|
|
|
|
|
public Window? Self { get; set; } = w;
|
|
|
|
|
|
|
|
|
|
[Obsolete("过时的")]
|
|
|
|
|
public async Task<string?> ImportExcel()
|
|
|
|
|
{
|
|
|
|
|
var dialog = new OpenFileDialog
|
|
|
|
|
{
|
|
|
|
|
Title = "打开文件",
|
|
|
|
|
AllowMultiple = false,
|
|
|
|
|
Filters =
|
|
|
|
|
[
|
|
|
|
|
new FileDialogFilter { Name = "Excel Files", Extensions = { "*.xls; *.xlsx" } }
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
var result = await dialog.ShowAsync(Self!);
|
|
|
|
|
if (result is not { Length: 1 }) return null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var ccc = ConvertExcelToJson(result[0]);
|
|
|
|
|
return ConvertExcelToJson(result[0]);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ConvertExcelToJson(string excelFilePath)
|
|
|
|
|
{
|
|
|
|
|
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
|
|
|
var fileInfo = new FileInfo(excelFilePath);
|
|
|
|
|
using var package = new ExcelPackage(fileInfo);
|
|
|
|
|
var worksheet = package.Workbook.Worksheets[0];
|
|
|
|
|
var rowCount = worksheet.Dimension.Rows;
|
|
|
|
|
var colCount = worksheet.Dimension.Columns;
|
|
|
|
|
|
|
|
|
|
var data = new List<Dictionary<string, object>>();
|
|
|
|
|
var headers = Enumerable.Range(1, colCount)
|
|
|
|
|
.Select(col => worksheet.Cells[1, col].Text)
|
|
|
|
|
.ToList();
|
|
|
|
|
for (var row = 2; row <= rowCount; row++)
|
|
|
|
|
{
|
|
|
|
|
var rowData = new Dictionary<string, object>();
|
|
|
|
|
for (var col = 1; col <= colCount; col++)
|
|
|
|
|
{
|
|
|
|
|
rowData[headers[col - 1]] = worksheet.Cells[row, col].Text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.Add(rowData);
|
|
|
|
|
}
|
|
|
|
|
return JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) });
|
|
|
|
|
}
|
2024-11-26 15:44:31 +08:00
|
|
|
|
|
|
|
|
|
public Task SetIsLoading(bool isLoading)
|
|
|
|
|
{
|
|
|
|
|
Locator.Current.GetService<MainWindowModel>()!.IsLoading = isLoading;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SetMessage(string messageText, string messageType)
|
|
|
|
|
{
|
|
|
|
|
Locator.Current.GetService<MainWindowModel>()!.Message = messageType switch
|
|
|
|
|
{
|
|
|
|
|
"success" => new MessageItem(messageText, NotificationType.Success),
|
|
|
|
|
"information" => new MessageItem(messageText, NotificationType.Information),
|
|
|
|
|
"error" => new MessageItem(messageText, NotificationType.Error),
|
|
|
|
|
_ => new MessageItem(messageText, NotificationType.Warning)
|
|
|
|
|
};
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2024-11-21 16:28:36 +08:00
|
|
|
|
}
|