feat: 实现密码加密存储与异步数据预览功能

- 在 Config.cs 中添加 DPAPI 加密支持,确保密码安全存储,兼容旧版配置。
- 更新 MainForm.cs,重构数据预览逻辑为异步方法,提升用户体验,避免界面冻结。
- 修改相关方法以支持异步操作,确保在处理大文件时界面响应流畅。
- 更新项目文件以引用 System.Security,支持加密功能。
This commit is contained in:
chenshoulin
2026-11-01 21:57:53 +08:00
parent 8a6f9d55bc
commit 8824a81758
4 changed files with 75 additions and 10 deletions
+18 -8
View File
@@ -39,7 +39,7 @@ namespace TfsExcelImporter
_btnLoadIterations.Click += async (s, e) => await LoadIterationsAsync();
_btnBrowse.Click += (s, e) => BrowseExcel();
_btnTemplate.Click += (s, e) => ExportTemplate();
_btnPreview.Click += (s, e) => PreviewData();
_btnPreview.Click += async (s, e) => await PreviewDataAsync();
_btnImport.Click += async (s, e) => await ImportAsync();
_btnFixArea.Click += async (s, e) => await FixAreaAsync();
_btnLinkParent.Click += async (s, e) => await LinkParentAsync();
@@ -222,7 +222,7 @@ namespace TfsExcelImporter
}
// ------------------------------------------------------------- Excel
private void BrowseExcel()
private async void BrowseExcel()
{
using (var dlg = new OpenFileDialog())
{
@@ -235,11 +235,11 @@ namespace TfsExcelImporter
if (dlg.ShowDialog(this) != DialogResult.OK) return;
_txtExcel.Text = dlg.FileName;
SaveUiToConfig();
PreviewData();
await PreviewDataAsync();
}
}
private void ExportTemplate()
private async void ExportTemplate()
{
using (var dlg = new SaveFileDialog())
{
@@ -253,7 +253,7 @@ namespace TfsExcelImporter
Log("模板已导出: " + dlg.FileName);
_txtExcel.Text = dlg.FileName;
SaveUiToConfig();
PreviewData();
await PreviewDataAsync();
}
catch (Exception ex)
{
@@ -263,7 +263,10 @@ namespace TfsExcelImporter
}
}
private void PreviewData()
// ------------------------------------------------------------- 预览
// ClosedXML 全量载入工作簿,大文件在 UI 线程读会冻结界面——放 Task.Run,
// 读期间 SetBusy 防止重复点击;await 后自动回到 UI 线程再更新表格。
private async Task PreviewDataAsync()
{
var path = _txtExcel.Text.Trim();
if (string.IsNullOrEmpty(path) || !File.Exists(path))
@@ -272,9 +275,10 @@ namespace TfsExcelImporter
Append("预览未执行:Excel 文件不存在", LogLevel.Warn);
return;
}
SetBusy(true);
try
{
_rows = ExcelService.ReadRows(path);
_rows = await Task.Run(() => ExcelService.ReadRows(path));
_grid.DataSource = null;
_grid.DataSource = _rows.Select((r, i) => new
{
@@ -305,6 +309,10 @@ namespace TfsExcelImporter
ShowError(_errExcel, _fExcel, _cardData, "读取失败:" + FirstLine(ex.Message));
UpdateEmptyState();
}
finally
{
SetBusy(false);
}
}
// ------------------------------------------------------------- Area 修正
@@ -421,13 +429,15 @@ namespace TfsExcelImporter
}
if (_rows.Count == 0)
{
try { _rows = ExcelService.ReadRows(path); }
SetBusy(true);
try { _rows = await Task.Run(() => ExcelService.ReadRows(path)); }
catch (Exception ex)
{
ShowError(_errExcel, _fExcel, _cardData, "读取失败:" + FirstLine(ex.Message));
Log("读取 Excel 失败: " + FirstLine(ex.Message));
return;
}
finally { SetBusy(false); }
}
if (_rows.Count == 0)
{