diff --git a/WindowsFormsApp6/MainForm.Designer.cs b/WindowsFormsApp6/MainForm.Designer.cs index cd1e6a7..e86a8a1 100644 --- a/WindowsFormsApp6/MainForm.Designer.cs +++ b/WindowsFormsApp6/MainForm.Designer.cs @@ -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 diff --git a/WindowsFormsApp6/MainForm.cs b/WindowsFormsApp6/MainForm.cs index e1b25d1..9ad9445 100644 --- a/WindowsFormsApp6/MainForm.cs +++ b/WindowsFormsApp6/MainForm.cs @@ -520,7 +520,7 @@ namespace WindowsFormsApp6 int hangTime = ConvertSingleRegisterToInt(hangTimeReg[0]); // 读取运行速度(D410,1个寄存器,整数,单位: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); } } + /// + /// 显示导出选项对话框 + /// + 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; + } + + /// + /// 导出选项类 + /// + 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);