Files
NonWovenFabric/WindowsFormsApp6/MainForm.cs

554 lines
21 KiB
C#
Raw Normal View History

2025-12-31 09:43:35 +08:00
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
2025-12-31 17:26:27 +08:00
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.SS.Util;
using NPOIBorderStyle = NPOI.SS.UserModel.BorderStyle;
using NPOIHorizontalAlignment = NPOI.SS.UserModel.HorizontalAlignment;
2025-12-31 09:43:35 +08:00
namespace WindowsFormsApp6
{
public partial class MainForm : Form
2025-12-31 17:26:27 +08:00
{
private Form1 form1Instance;
private Form2 form2Instance;
private Form3 form3Instance;
private System.Windows.Forms.Timer clockTimer;
2025-12-31 09:43:35 +08:00
public MainForm()
{
InitializeComponent();
2025-12-31 17:26:27 +08:00
InitializeClockTimer();
InitializeTabControl();
InitializeEmbeddedForms();
}
private void InitializeClockTimer()
{
clockTimer = new System.Windows.Forms.Timer();
clockTimer.Interval = 1000;
clockTimer.Tick += (s, e) => label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
clockTimer.Start();
}
2025-12-31 09:43:35 +08:00
2025-12-31 17:26:27 +08:00
private void InitializeTabControl()
{
tabControl1.SelectedIndexChanged += TabControl1_SelectedIndexChanged;
}
2025-12-31 09:43:35 +08:00
2025-12-31 17:26:27 +08:00
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateTitleForCurrentTab();
2025-12-31 17:41:01 +08:00
// 根据选中的 Tab 显示/隐藏切换布局按钮
// 只有在 Form3 (Tab 2, index=2) 时显示切换按钮
buttonToggleLayout.Visible = (tabControl1.SelectedIndex == 2);
// 更新切换按钮的文本
if (tabControl1.SelectedIndex == 2 && form3Instance != null)
{
UpdateToggleButtonText();
}
2025-12-31 09:43:35 +08:00
}
2025-12-31 17:26:27 +08:00
private void UpdateTitleForCurrentTab()
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
switch (tabControl1.SelectedIndex)
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
case 0:
label1.Text = " 液体吸收时间测试报告";
break;
case 1:
label1.Text = " 液体吸收量测试报告";
break;
case 2:
label1.Text = " 液体芯吸速率测试报告";
break;
2025-12-31 09:43:35 +08:00
}
}
2025-12-31 17:26:27 +08:00
private void InitializeEmbeddedForms()
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
form1Instance = new Form1();
form1Instance.TopLevel = false;
form1Instance.FormBorderStyle = FormBorderStyle.None;
form1Instance.Dock = DockStyle.Fill;
Panel panel1 = new Panel();
panel1.Dock = DockStyle.Fill;
panel1.Controls.Add(form1Instance.Controls["tableLayoutPanel1"].Controls["panel3"]);
tabPage1.Controls.Add(panel1);
form1Instance.Show();
form2Instance = new Form2();
form2Instance.TopLevel = false;
form2Instance.FormBorderStyle = FormBorderStyle.None;
form2Instance.Dock = DockStyle.Fill;
Panel panel2 = new Panel();
panel2.Dock = DockStyle.Fill;
panel2.Controls.Add(form2Instance.Controls["tableLayoutPanel1"].Controls["panel3"]);
tabPage2.Controls.Add(panel2);
form2Instance.Show();
form3Instance = new Form3();
form3Instance.TopLevel = false;
form3Instance.FormBorderStyle = FormBorderStyle.None;
form3Instance.Dock = DockStyle.Fill;
Panel panel3 = new Panel();
panel3.Dock = DockStyle.Fill;
panel3.Controls.Add(form3Instance.Controls["tableLayoutPanel1"].Controls["panel3"]);
tabPage3.Controls.Add(panel3);
form3Instance.Show();
2025-12-31 09:43:35 +08:00
}
2025-12-31 17:26:27 +08:00
private void buttonPrint_Click(object sender, EventArgs e)
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
MessageBox.Show("打印功能开发中", "提示");
2025-12-31 09:43:35 +08:00
}
2025-12-31 17:26:27 +08:00
private void buttonExport_Click(object sender, EventArgs e)
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Excel 文件 (*.xlsx)|*.xlsx",
FileName = $"测试报告_{DateTime.Now:yyyyMMdd_HHmmss}",
Title = "导出整合报告"
};
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
ExportIntegratedReport(saveFileDialog.FileName);
}
}
private void button5_Click(object sender, EventArgs e)
{
switch (tabControl1.SelectedIndex)
{
case 0:
form1Instance.GenerateMockData();
break;
case 1:
form2Instance.GenerateMockData();
break;
case 2:
form3Instance.GenerateMockData();
break;
}
}
2025-12-31 17:41:01 +08:00
private void buttonToggleLayout_Click(object sender, EventArgs e)
{
if (form3Instance != null)
{
// 调用 Form3 的公共切换方法
form3Instance.ToggleTableLayout();
// 更新按钮文本
UpdateToggleButtonText();
}
}
/// <summary>
/// 更新切换按钮的文本
/// </summary>
private void UpdateToggleButtonText()
{
if (form3Instance != null)
{
// 使用公共方法获取布局状态
bool isVertical = form3Instance.IsVerticalLayout();
buttonToggleLayout.Text = isVertical ? "🔄 横向布局" : "🔄 纵向布局";
}
}
2025-12-31 17:26:27 +08:00
private void ExportIntegratedReport(string filePath)
{
try
{
IWorkbook workbook = new XSSFWorkbook();
CreateForm1Sheet(workbook);
CreateForm2Sheet(workbook);
CreateForm3Sheet(workbook);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}
MessageBox.Show($"导出成功:{filePath}", "成功");
}
catch (Exception ex)
{
MessageBox.Show($"导出失败:{ex.Message}", "错误");
}
2025-12-31 09:43:35 +08:00
}
2025-12-31 17:26:27 +08:00
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
2025-12-31 09:43:35 +08:00
{
2025-12-31 17:26:27 +08:00
clockTimer?.Stop();
clockTimer?.Dispose();
Application.Exit();
2025-12-31 09:43:35 +08:00
}
2025-12-31 17:26:27 +08:00
2025-12-31 17:54:34 +08:00
private void CreateForm1Sheet(IWorkbook workbook)
{
ISheet sheet = workbook.CreateSheet("液体吸收时间");
var styles = CreateReportStyles(workbook);
// 获取 Form1 的数据
var sampleCountField = form1Instance.GetType()
.GetField("currentSampleCount", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
int sampleCount = sampleCountField != null ? (int)sampleCountField.GetValue(form1Instance) : 5;
var dataTableField = form1Instance.GetType()
.GetField("sampleDataTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
DataTable dataTable = dataTableField?.GetValue(form1Instance) as DataTable;
if (dataTable == null || dataTable.Rows.Count == 0)
{
// 创建空表格
CreateEmptySheet(sheet, "液体吸收时间", sampleCount, styles);
return;
}
int currentRow = 0;
// 创建标题行
IRow titleRow = sheet.CreateRow(currentRow++);
ICell titleCell = titleRow.CreateCell(0);
titleCell.SetCellValue("液体吸收时间");
titleCell.CellStyle = styles.titleStyle;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, sampleCount));
titleRow.Height = 600;
currentRow++; // 空行
// 创建表头
IRow headerRow = sheet.CreateRow(currentRow++);
headerRow.Height = 400;
ICell seqCell = headerRow.CreateCell(0);
seqCell.SetCellValue("序号");
seqCell.CellStyle = styles.headerStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = headerRow.CreateCell(i);
cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle;
}
// 填充数据行
foreach (DataRow dataRow in dataTable.Rows)
{
string rowName = dataRow["序号"]?.ToString() ?? "";
if (string.IsNullOrEmpty(rowName)) continue;
IRow row = sheet.CreateRow(currentRow++);
row.Height = 380;
ICell nameCell = row.CreateCell(0);
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
{
if (double.TryParse(dataRow[$"试样{i}"].ToString(), out double value))
{
cell.SetCellValue(value);
}
}
cell.CellStyle = styles.yellowStyle;
}
}
// 设置列宽
sheet.SetColumnWidth(0, 20 * 256);
for (int i = 1; i <= sampleCount; i++)
{
sheet.SetColumnWidth(i, 15 * 256);
}
}
private void CreateForm2Sheet(IWorkbook workbook)
{
ISheet sheet = workbook.CreateSheet("液体吸收量");
var styles = CreateReportStyles(workbook);
// 获取 Form2 的数据
var sampleCountField = form2Instance.GetType()
.GetField("currentSampleCount", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
int sampleCount = sampleCountField != null ? (int)sampleCountField.GetValue(form2Instance) : 5;
var dataTableField = form2Instance.GetType()
.GetField("sampleDataTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
DataTable dataTable = dataTableField?.GetValue(form2Instance) as DataTable;
if (dataTable == null || dataTable.Rows.Count == 0)
{
CreateEmptySheet(sheet, "液体吸收量", sampleCount, styles);
return;
}
int currentRow = 0;
// 创建标题行
IRow titleRow = sheet.CreateRow(currentRow++);
ICell titleCell = titleRow.CreateCell(0);
titleCell.SetCellValue("液体吸收量");
titleCell.CellStyle = styles.titleStyle;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, sampleCount));
titleRow.Height = 600;
currentRow++; // 空行
// 创建表头
IRow headerRow = sheet.CreateRow(currentRow++);
headerRow.Height = 400;
ICell seqCell = headerRow.CreateCell(0);
seqCell.SetCellValue("序号");
seqCell.CellStyle = styles.headerStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = headerRow.CreateCell(i);
cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle;
}
// 填充数据行
foreach (DataRow dataRow in dataTable.Rows)
{
string rowName = dataRow["序号"]?.ToString() ?? "";
if (string.IsNullOrEmpty(rowName)) continue;
IRow row = sheet.CreateRow(currentRow++);
row.Height = 380;
ICell nameCell = row.CreateCell(0);
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
{
object value = dataRow[$"试样{i}"];
if (value != null && double.TryParse(value.ToString(), out double numValue))
{
cell.SetCellValue(numValue);
}
}
cell.CellStyle = styles.yellowStyle;
}
}
// 设置列宽
sheet.SetColumnWidth(0, 25 * 256);
for (int i = 1; i <= sampleCount; i++)
{
sheet.SetColumnWidth(i, 18 * 256);
}
}
private void CreateForm3Sheet(IWorkbook workbook)
{
ISheet sheet = workbook.CreateSheet("液体芯吸速率");
var styles = CreateReportStyles(workbook);
// 获取 Form3 的数据
var sampleCountField = form3Instance.GetType()
.GetField("currentSampleCount", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
int sampleCount = sampleCountField != null ? (int)sampleCountField.GetValue(form3Instance) : 5;
var dataTableField = form3Instance.GetType()
.GetField("sampleDataTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
DataTable dataTable = dataTableField?.GetValue(form3Instance) as DataTable;
if (dataTable == null || dataTable.Rows.Count == 0)
{
CreateEmptySheet(sheet, "液体芯吸速率", sampleCount, styles);
return;
}
int currentRow = 0;
// 创建标题行
IRow titleRow = sheet.CreateRow(currentRow++);
ICell titleCell = titleRow.CreateCell(0);
titleCell.SetCellValue("液体芯吸速率");
titleCell.CellStyle = styles.titleStyle;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, sampleCount * 3));
titleRow.Height = 600;
currentRow++; // 空行
// 创建第一级表头试样1-N
IRow headerRow1 = sheet.CreateRow(currentRow++);
headerRow1.Height = 400;
ICell cell0 = headerRow1.CreateCell(0);
cell0.SetCellValue("序号");
cell0.CellStyle = styles.headerStyle;
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow, 0, 0));
int colIndex = 1;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = headerRow1.CreateCell(colIndex);
cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle;
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, colIndex, colIndex + 2));
colIndex += 3;
}
// 创建第二级表头1、2、3
IRow headerRow2 = sheet.CreateRow(currentRow++);
headerRow2.Height = 400;
colIndex = 1;
for (int i = 1; i <= sampleCount; i++)
{
for (int j = 1; j <= 3; j++)
{
ICell cell = headerRow2.CreateCell(colIndex++);
cell.SetCellValue(j.ToString());
cell.CellStyle = styles.headerStyle;
}
}
// 填充数据行
foreach (DataRow dataRow in dataTable.Rows)
{
string rowName = dataRow["序号"]?.ToString() ?? "";
if (string.IsNullOrEmpty(rowName)) continue;
IRow row = sheet.CreateRow(currentRow++);
row.Height = 380;
ICell nameCell = row.CreateCell(0);
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
colIndex = 1;
for (int i = 1; i <= sampleCount; i++)
{
for (int j = 1; j <= 3; j++)
{
ICell cell = row.CreateCell(colIndex++);
string columnName = $"试样{i}_{j}";
if (dataTable.Columns.Contains(columnName) && dataRow[columnName] != DBNull.Value)
{
if (double.TryParse(dataRow[columnName].ToString(), out double value))
{
if (Math.Abs(value) >= 0.001)
{
cell.SetCellValue(value);
}
}
}
cell.CellStyle = styles.yellowStyle;
}
}
}
// 设置列宽
sheet.SetColumnWidth(0, 20 * 256);
for (int i = 1; i <= sampleCount * 3; i++)
{
sheet.SetColumnWidth(i, 12 * 256);
}
}
private void CreateEmptySheet(ISheet sheet, string title, int sampleCount,
(ICellStyle titleStyle, ICellStyle headerStyle, ICellStyle dataStyle, ICellStyle yellowStyle) styles)
{
// 创建标题行
IRow titleRow = sheet.CreateRow(0);
ICell titleCell = titleRow.CreateCell(0);
titleCell.SetCellValue(title);
titleCell.CellStyle = styles.titleStyle;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, sampleCount));
titleRow.Height = 600;
// 创建表头
IRow headerRow = sheet.CreateRow(2);
headerRow.Height = 400;
ICell seqCell = headerRow.CreateCell(0);
seqCell.SetCellValue("序号");
seqCell.CellStyle = styles.headerStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = headerRow.CreateCell(i);
cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle;
}
// 设置列宽
sheet.SetColumnWidth(0, 20 * 256);
for (int i = 1; i <= sampleCount; i++)
{
sheet.SetColumnWidth(i, 15 * 256);
}
}
private (ICellStyle titleStyle, ICellStyle headerStyle, ICellStyle dataStyle, ICellStyle yellowStyle)
CreateReportStyles(IWorkbook workbook)
{
// 标题样式
ICellStyle titleStyle = workbook.CreateCellStyle();
IFont titleFont = workbook.CreateFont();
titleFont.FontHeightInPoints = 16;
titleFont.IsBold = true;
titleStyle.SetFont(titleFont);
titleStyle.Alignment = NPOIHorizontalAlignment.Center;
titleStyle.VerticalAlignment = VerticalAlignment.Center;
// 表头样式(灰色背景)
ICellStyle headerStyle = workbook.CreateCellStyle();
headerStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
headerStyle.FillPattern = FillPattern.SolidForeground;
SetBorders(headerStyle);
IFont headerFont = workbook.CreateFont();
headerFont.IsBold = true;
headerStyle.SetFont(headerFont);
headerStyle.Alignment = NPOIHorizontalAlignment.Center;
headerStyle.VerticalAlignment = VerticalAlignment.Center;
// 数据样式(白色背景)
ICellStyle dataStyle = workbook.CreateCellStyle();
SetBorders(dataStyle);
dataStyle.Alignment = NPOIHorizontalAlignment.Center;
dataStyle.VerticalAlignment = VerticalAlignment.Center;
// 黄色背景样式
ICellStyle yellowStyle = workbook.CreateCellStyle();
yellowStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightYellow.Index;
yellowStyle.FillPattern = FillPattern.SolidForeground;
SetBorders(yellowStyle);
yellowStyle.Alignment = NPOIHorizontalAlignment.Center;
yellowStyle.VerticalAlignment = VerticalAlignment.Center;
return (titleStyle, headerStyle, dataStyle, yellowStyle);
}
private void SetBorders(ICellStyle style)
{
style.BorderBottom = NPOIBorderStyle.Thin;
style.BorderTop = NPOIBorderStyle.Thin;
style.BorderLeft = NPOIBorderStyle.Thin;
style.BorderRight = NPOIBorderStyle.Thin;
}
2025-12-31 09:43:35 +08:00
}
}