更新1653

This commit is contained in:
GukSang.Jin
2026-01-06 16:53:32 +08:00
parent aa54677db8
commit 027a40e2bf
2 changed files with 143 additions and 16 deletions

View File

@@ -379,7 +379,7 @@ namespace WindowsFormsApp6
this.button5.TabIndex = 3;
this.button5.Text = "🎲 模拟数据";
this.button5.UseVisualStyleBackColor = false;
this.button5.Visible = false;
this.button5.Visible = true;
this.button5.Click += new System.EventHandler(this.Button5_Click);
//
// buttonExport

View File

@@ -520,7 +520,7 @@ namespace WindowsFormsApp6
int hangTime = ConvertSingleRegisterToInt(hangTimeReg[0]);
// 读取运行速度D4101个寄存器整数单位mm/min
ushort[] runSpeedReg = _modbusMaster.ReadHoldingRegisters(slaveId, 410, 1);
ushort[] runSpeedReg = _modbusMaster.ReadHoldingRegisters(slaveId, 410, 5);
int runSpeed = ConvertSingleRegisterToInt(runSpeedReg[0]);
// 使用反射获取Form2的私有字段
@@ -1559,6 +1559,14 @@ namespace WindowsFormsApp6
private void ButtonExport_Click(object sender, EventArgs e)
{
// 显示导出选项对话框
var exportOptions = ShowExportOptionsDialog();
if (exportOptions == null)
{
// 用户取消了导出
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Excel 文件 (*.xlsx)|*.xlsx",
@@ -1568,10 +1576,122 @@ namespace WindowsFormsApp6
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
ExportIntegratedReport(saveFileDialog.FileName);
ExportIntegratedReport(saveFileDialog.FileName, exportOptions);
}
}
/// <summary>
/// 显示导出选项对话框
/// </summary>
private ExportOptions ShowExportOptionsDialog()
{
// 创建对话框
Form optionsDialog = new Form
{
Text = "选择导出内容",
Width = 350,
Height = 250,
StartPosition = FormStartPosition.CenterParent,
FormBorderStyle = FormBorderStyle.FixedDialog,
MaximizeBox = false,
MinimizeBox = false
};
Label label = new Label
{
Text = "请选择要导出的测试项目:",
Location = new Point(20, 20),
AutoSize = true,
Font = new Font("微软雅黑", 10F)
};
CheckBox chkAbsorptionTime = new CheckBox
{
Text = "液体吸收时间",
Location = new Point(40, 60),
AutoSize = true,
Checked = true,
Font = new Font("微软雅黑", 9F)
};
CheckBox chkAbsorptionAmount = new CheckBox
{
Text = "液体吸收量",
Location = new Point(40, 90),
AutoSize = true,
Checked = true,
Font = new Font("微软雅黑", 9F)
};
CheckBox chkWickingRate = new CheckBox
{
Text = "液体芯吸速率",
Location = new Point(40, 120),
AutoSize = true,
Checked = true,
Font = new Font("微软雅黑", 9F)
};
Button btnOK = new Button
{
Text = "确定",
DialogResult = DialogResult.OK,
Location = new Point(100, 160),
Width = 80,
Height = 30
};
Button btnCancel = new Button
{
Text = "取消",
DialogResult = DialogResult.Cancel,
Location = new Point(190, 160),
Width = 80,
Height = 30
};
optionsDialog.Controls.AddRange(new Control[] {
label,
chkAbsorptionTime,
chkAbsorptionAmount,
chkWickingRate,
btnOK,
btnCancel
});
optionsDialog.AcceptButton = btnOK;
optionsDialog.CancelButton = btnCancel;
// 显示对话框
if (optionsDialog.ShowDialog(this) == DialogResult.OK)
{
// 检查是否至少选择了一项
if (!chkAbsorptionTime.Checked && !chkAbsorptionAmount.Checked && !chkWickingRate.Checked)
{
MessageBox.Show("请至少选择一个导出项目!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
}
return new ExportOptions
{
IncludeAbsorptionTime = chkAbsorptionTime.Checked,
IncludeAbsorptionAmount = chkAbsorptionAmount.Checked,
IncludeWickingRate = chkWickingRate.Checked
};
}
return null;
}
/// <summary>
/// 导出选项类
/// </summary>
private class ExportOptions
{
public bool IncludeAbsorptionTime { get; set; }
public bool IncludeAbsorptionAmount { get; set; }
public bool IncludeWickingRate { get; set; }
}
private void Button5_Click(object sender, EventArgs e)
{
switch (tabControl1.SelectedIndex)
@@ -1613,14 +1733,14 @@ namespace WindowsFormsApp6
}
}
private void ExportIntegratedReport(string filePath)
private void ExportIntegratedReport(string filePath, ExportOptions options)
{
try
{
IWorkbook workbook = new XSSFWorkbook();
// 创建单个整合的工作表
CreateIntegratedSheet(workbook);
CreateIntegratedSheet(workbook, options);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
@@ -1635,7 +1755,7 @@ namespace WindowsFormsApp6
}
}
private void CreateIntegratedSheet(IWorkbook workbook)
private void CreateIntegratedSheet(IWorkbook workbook, ExportOptions options)
{
ISheet sheet = workbook.CreateSheet("吸收性测试报告");
var styles = CreateReportStyles(workbook);
@@ -1679,18 +1799,25 @@ namespace WindowsFormsApp6
currentRow++; // 空行
// 3. 创建液体吸收时间部分
CreateForm1Section(sheet, ref currentRow, sampleCount, dataTable1, styles);
// 3. 创建液体吸收时间部分(根据选项)
if (options.IncludeAbsorptionTime)
{
CreateForm1Section(sheet, ref currentRow, sampleCount, dataTable1, styles);
currentRow++; // 空行
}
currentRow++; // 空行
// 4. 创建液体吸收量部分(根据选项)
if (options.IncludeAbsorptionAmount)
{
CreateForm2Section(sheet, ref currentRow, sampleCount, dataTable2, styles);
currentRow++; // 空行
}
// 4. 创建液体吸收量部分
CreateForm2Section(sheet, ref currentRow, sampleCount, dataTable2, styles);
currentRow++; // 空行
// 5. 创建液体芯吸速率部分
CreateForm3Section(sheet, ref currentRow, sampleCount, dataTable3, styles);
// 5. 创建液体芯吸速率部分(根据选项)
if (options.IncludeWickingRate)
{
CreateForm3Section(sheet, ref currentRow, sampleCount, dataTable3, styles);
}
// 设置列宽
sheet.SetColumnWidth(0, 20 * 256);