515 lines
21 KiB
C#
515 lines
21 KiB
C#
using OfficeOpenXml;
|
||
using OfficeOpenXml.Style;
|
||
using Sunny.UI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using 材料热传导系数;
|
||
|
||
|
||
namespace 外科辅料和患者防护罩激光抗性测试仪
|
||
{
|
||
public partial class ReportForm : UIForm
|
||
{
|
||
ConductivityRepository conductivityRepository;
|
||
public ReportForm()
|
||
{
|
||
InitializeComponent();
|
||
|
||
InitComboBoxPattern();
|
||
InitComboBoxPattern2();
|
||
InitComboBoxPattern3();
|
||
|
||
conductivityRepository = new ConductivityRepository();
|
||
|
||
}
|
||
|
||
private void ReportForm_Load(object sender, EventArgs e)
|
||
{
|
||
SetDefaultDateTime();
|
||
|
||
// 授权EPPlus使用(非商业用途)
|
||
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
||
|
||
this.FormClosed += (s, args) =>
|
||
{
|
||
// 当子窗体关闭时,显示主窗体
|
||
Application.OpenForms["MainForm"]?.Show();
|
||
};
|
||
}
|
||
|
||
private void ReportForm_FormClosed(object sender, FormClosedEventArgs e)
|
||
{
|
||
foreach (Form form in Application.OpenForms)
|
||
{
|
||
if (form.Name == "MainForm" || form.Text.Contains("主界面"))
|
||
{
|
||
form.Show();
|
||
form.Activate(); // 激活窗体
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void InitComboBoxPattern()
|
||
{
|
||
string[] options =
|
||
{
|
||
"全部报告",
|
||
"初次点火测试报告",
|
||
"穿透性测试报告",
|
||
"二次点火测试报告"
|
||
};
|
||
uiComboBox1.Items.AddRange(options);
|
||
}
|
||
|
||
private void InitComboBoxPattern2()
|
||
{
|
||
string[] options =
|
||
{
|
||
"全部报告",
|
||
"初次点火测试报告",
|
||
"穿透性测试报告",
|
||
"二次点火测试报告"
|
||
};
|
||
uiComboBox2.Items.AddRange(options);
|
||
}
|
||
|
||
private void InitComboBoxPattern3()
|
||
{
|
||
string[] options =
|
||
{
|
||
"初次点火测试报告",
|
||
"穿透性测试报告",
|
||
"二次点火测试报告"
|
||
};
|
||
uiComboBox3.Items.AddRange(options);
|
||
}
|
||
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();//返回主页面
|
||
}
|
||
|
||
// 设置默认时间的方法
|
||
private void SetDefaultDateTime()
|
||
{
|
||
// 起始时间:昨天
|
||
uiDatetimePicker1.Value = DateTime.Now.AddDays(-1);
|
||
|
||
// 终止时间:今天
|
||
uiDatetimePicker2.Value = DateTime.Now;
|
||
|
||
}
|
||
|
||
private void uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
string reportType = uiComboBox1.Text;
|
||
|
||
// 如果没有选择报表类型,可以设置默认值或提示
|
||
if (string.IsNullOrEmpty(reportType))
|
||
{
|
||
MessageBox.Show("请选择报表类型!");
|
||
return;
|
||
}
|
||
|
||
DateTime startDate = uiDatetimePicker1.Value;
|
||
DateTime endDate = uiDatetimePicker2.Value;
|
||
|
||
// 根据不同的报表类型查询对应的数据
|
||
switch (reportType)
|
||
{
|
||
case "全部报告":
|
||
var alldata1 = conductivityRepository.GetTestData(startDate, endDate);
|
||
uiDataGridView1.DataSource = alldata1;
|
||
uiDataGridView1.Refresh();
|
||
var alldata2 = conductivityRepository.GetTestData2(startDate, endDate);
|
||
uiDataGridView2.DataSource = alldata2;
|
||
uiDataGridView2.Refresh();
|
||
var alldata3 = conductivityRepository.GetTestData3(startDate, endDate);
|
||
uiDataGridView3.DataSource = alldata3;
|
||
uiDataGridView3.Refresh();
|
||
|
||
break;
|
||
|
||
case "初次点火测试报告":
|
||
var data1 = conductivityRepository.GetTestData(startDate, endDate);
|
||
uiDataGridView1.DataSource = data1;
|
||
uiDataGridView1.Refresh();
|
||
|
||
break;
|
||
|
||
case "穿透性测试报告":
|
||
var data2 = conductivityRepository.GetTestData2(startDate, endDate);
|
||
uiDataGridView2.DataSource = data2;
|
||
uiDataGridView2.Refresh();
|
||
|
||
|
||
break;
|
||
|
||
case "二次点火测试报告":
|
||
var data3 = conductivityRepository.GetTestData3(startDate, endDate);
|
||
uiDataGridView3.DataSource = data3;
|
||
uiDataGridView3.Refresh();
|
||
|
||
break;
|
||
|
||
default:
|
||
MessageBox.Show("未知的报表类型!");
|
||
break;
|
||
}
|
||
}//查询
|
||
|
||
private void uiButton4_Click(object sender, EventArgs e)
|
||
{
|
||
string reportType = uiComboBox2.Text;
|
||
|
||
// 如果没有选择报表类型,可以设置默认值或提示
|
||
if (string.IsNullOrEmpty(reportType))
|
||
{
|
||
MessageBox.Show("请选择报表类型!");
|
||
return;
|
||
}
|
||
|
||
DateTime startDate = uiDatetimePicker1.Value;
|
||
DateTime endDate = uiDatetimePicker2.Value;
|
||
|
||
// 根据不同的报表类型查询对应的数据
|
||
switch (reportType)
|
||
{
|
||
case "全部报告":
|
||
DialogResult result = MessageBox.Show(
|
||
"确定要清空所有记录吗?",
|
||
"确认清空",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
if (result == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
// 使用GetTestData方法获取空列表结构
|
||
uiDataGridView1.DataSource = conductivityRepository.GetTestData(startDate, endDate)
|
||
.Where(x => false) // 过滤掉所有数据
|
||
.ToList();
|
||
|
||
uiDataGridView2.DataSource = conductivityRepository.GetTestData2(startDate, endDate)
|
||
.Where(x => false)
|
||
.ToList();
|
||
|
||
uiDataGridView3.DataSource = conductivityRepository.GetTestData3(startDate, endDate)
|
||
.Where(x => false)
|
||
.ToList();
|
||
|
||
// 刷新显示
|
||
uiDataGridView1.Refresh();
|
||
uiDataGridView2.Refresh();
|
||
uiDataGridView3.Refresh();
|
||
|
||
MessageBox.Show("所有记录已清空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"清空失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
break;
|
||
|
||
case "初次点火测试报告":
|
||
result = MessageBox.Show(
|
||
"确定要清空初次点火测试报告吗?",
|
||
"确认清空",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
if (result == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
// 获取相同结构但为空的数据
|
||
uiDataGridView1.DataSource = conductivityRepository.GetTestData(startDate, endDate)
|
||
.Where(x => false)
|
||
.ToList();
|
||
uiDataGridView1.Refresh();
|
||
MessageBox.Show("初次点火测试报告已清空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"清空失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
break;
|
||
|
||
case "穿透性测试报告":
|
||
result = MessageBox.Show(
|
||
"确定要清空穿透性测试报告吗?",
|
||
"确认清空",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
if (result == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
// 获取相同结构但为空的数据
|
||
uiDataGridView2.DataSource = conductivityRepository.GetTestData2(startDate, endDate)
|
||
.Where(x => false)
|
||
.ToList();
|
||
uiDataGridView2.Refresh();
|
||
MessageBox.Show("穿透性测试报告已清空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"清空失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
break;
|
||
|
||
case "二次点火测试报告":
|
||
result = MessageBox.Show(
|
||
"确定要清空二次点火测试报告吗?",
|
||
"确认清空",
|
||
MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Warning
|
||
);
|
||
if (result == DialogResult.Yes)
|
||
{
|
||
try
|
||
{
|
||
// 获取相同结构但为空的数据
|
||
uiDataGridView3.DataSource = conductivityRepository.GetTestData3(startDate, endDate)
|
||
.Where(x => false)
|
||
.ToList();
|
||
uiDataGridView3.Refresh();
|
||
MessageBox.Show("二次点火测试报告已清空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"清空失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
break;
|
||
|
||
default:
|
||
MessageBox.Show("未知的报表类型!");
|
||
break;
|
||
}
|
||
}//清除
|
||
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
string reportType = uiComboBox3.Text;
|
||
|
||
// 如果没有选择报表类型,可以设置默认值或提示
|
||
if (string.IsNullOrEmpty(reportType))
|
||
{
|
||
MessageBox.Show("请选择要导出的报表类型!");
|
||
return;
|
||
}
|
||
|
||
// 创建保存文件对话框
|
||
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
||
saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx";
|
||
saveFileDialog.FileName = $"{reportType}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
|
||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
// 根据不同的报表类型导出对应的数据
|
||
switch (reportType)
|
||
{
|
||
case "初次点火测试报告":
|
||
ExportToExcel(uiDataGridView1, saveFileDialog.FileName, reportType);
|
||
break;
|
||
|
||
case "穿透性测试报告":
|
||
ExportToExcel(uiDataGridView2, saveFileDialog.FileName, reportType);
|
||
break;
|
||
|
||
case "二次点火测试报告":
|
||
ExportToExcel(uiDataGridView3, saveFileDialog.FileName, reportType);
|
||
break;
|
||
|
||
default:
|
||
MessageBox.Show("未知的报表类型!");
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"导出失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}//导出
|
||
|
||
private void ExportToExcel(DataGridView dataGridView, string filePath, string reportTitle)
|
||
{
|
||
// 检查是否有数据
|
||
//if (dataGridView.Rows.Count == 0)
|
||
//{
|
||
// MessageBox.Show("没有数据可导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
// return;
|
||
//}
|
||
|
||
using (ExcelPackage package = new ExcelPackage())
|
||
{
|
||
// 创建工作表
|
||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(reportTitle);
|
||
|
||
// 设置工作表标题
|
||
int titleRow = 1;
|
||
worksheet.Cells[titleRow, 1, titleRow, dataGridView.Columns.Count].Merge = true;
|
||
worksheet.Cells[titleRow, 1].Value = reportTitle;
|
||
worksheet.Cells[titleRow, 1].Style.Font.Size = 16;
|
||
worksheet.Cells[titleRow, 1].Style.Font.Bold = true;
|
||
worksheet.Cells[titleRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||
worksheet.Cells[titleRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||
|
||
// 设置时间范围
|
||
int timeRow = 2;
|
||
worksheet.Cells[timeRow, 1, timeRow, dataGridView.Columns.Count].Merge = true;
|
||
worksheet.Cells[timeRow, 1].Value = $"时间范围:{uiDatetimePicker1.Value:yyyy-MM-dd HH:mm} 至 {uiDatetimePicker2.Value:yyyy-MM-dd HH:mm}";
|
||
worksheet.Cells[timeRow, 1].Style.Font.Size = 12;
|
||
worksheet.Cells[timeRow, 1].Style.Font.Italic = true;
|
||
worksheet.Cells[timeRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||
|
||
// 设置表头开始行
|
||
int headerRow = 4;
|
||
|
||
// 写入表头
|
||
for (int i = 0; i < dataGridView.Columns.Count; i++)
|
||
{
|
||
if (dataGridView.Columns[i].Visible) // 只导出可见列
|
||
{
|
||
worksheet.Cells[headerRow, i + 1].Value = dataGridView.Columns[i].HeaderText;
|
||
worksheet.Cells[headerRow, i + 1].Style.Font.Bold = true;
|
||
worksheet.Cells[headerRow, i + 1].Style.Font.Size = 12;
|
||
worksheet.Cells[headerRow, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
|
||
worksheet.Cells[headerRow, i + 1].Style.Fill.BackgroundColor.SetColor(Color.LightGray);
|
||
worksheet.Cells[headerRow, i + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
|
||
worksheet.Cells[headerRow, i + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||
worksheet.Cells[headerRow, i + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||
|
||
// 自动调整列宽
|
||
worksheet.Column(i + 1).Width = Math.Max(dataGridView.Columns[i].HeaderText.Length * 2, 10);
|
||
}
|
||
}
|
||
|
||
// 写入数据
|
||
int dataRow = headerRow + 1;
|
||
int visibleColumnIndex = 0;
|
||
|
||
for (int i = 0; i < dataGridView.Rows.Count; i++)
|
||
{
|
||
visibleColumnIndex = 0;
|
||
for (int j = 0; j < dataGridView.Columns.Count; j++)
|
||
{
|
||
if (dataGridView.Columns[j].Visible)
|
||
{
|
||
var cellValue = dataGridView.Rows[i].Cells[j].Value;
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Value = cellValue?.ToString() ?? "";
|
||
|
||
// 设置单元格样式
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||
|
||
// 根据数据类型设置格式
|
||
if (cellValue is DateTime)
|
||
{
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
|
||
}
|
||
else if (cellValue is decimal || cellValue is double || cellValue is float)
|
||
{
|
||
worksheet.Cells[dataRow, visibleColumnIndex + 1].Style.Numberformat.Format = "0.00";
|
||
}
|
||
|
||
visibleColumnIndex++;
|
||
}
|
||
}
|
||
dataRow++;
|
||
}
|
||
|
||
// 设置最后一行的边框
|
||
int lastRow = dataRow - 1;
|
||
if (lastRow > headerRow)
|
||
{
|
||
worksheet.Cells[headerRow + 1, 1, lastRow, visibleColumnIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin);
|
||
}
|
||
|
||
// 应用交替行颜色
|
||
for (int i = headerRow + 1; i <= lastRow; i++)
|
||
{
|
||
if (i % 2 == 0) // 偶数行
|
||
{
|
||
worksheet.Cells[i, 1, i, visibleColumnIndex].Style.Fill.PatternType = ExcelFillStyle.Solid;
|
||
worksheet.Cells[i, 1, i, visibleColumnIndex].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(240, 240, 240));
|
||
}
|
||
}
|
||
|
||
// 冻结表头行
|
||
worksheet.View.FreezePanes(headerRow + 1, 1);
|
||
|
||
// 自动调整所有列的宽度
|
||
for (int i = 1; i <= visibleColumnIndex; i++)
|
||
{
|
||
worksheet.Column(i).AutoFit();
|
||
// 如果自动调整后的宽度太小,设置最小宽度
|
||
if (worksheet.Column(i).Width < 8)
|
||
{
|
||
worksheet.Column(i).Width = 12;
|
||
}
|
||
// 如果自动调整后的宽度太大,设置最大宽度
|
||
if (worksheet.Column(i).Width > 50)
|
||
{
|
||
worksheet.Column(i).Width = 50;
|
||
}
|
||
}
|
||
|
||
// 保存文件
|
||
package.SaveAs(new FileInfo(filePath));
|
||
|
||
MessageBox.Show($"导出成功!文件已保存到:{filePath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
|
||
|
||
//DialogResult openResult = MessageBox.Show("是否打开导出的Excel文件?", "打开文件",
|
||
// MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
||
|
||
//if (openResult == DialogResult.Yes)
|
||
//{
|
||
// System.Diagnostics.Process.Start(filePath);
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
|
||
//private void uiButton1_Click(object sender, EventArgs e)
|
||
//{
|
||
|
||
// var data= conductivityRepository.GetTestData(uiDatetimePicker1.Text.ToDateTime(),
|
||
// uiDatetimePicker2.Text.ToDateTime());
|
||
|
||
// uiDataGridView1.DataSource = data;
|
||
// uiDataGridView1.Refresh();
|
||
|
||
// var data2= conductivityRepository.GetTestData2(uiDatetimePicker1.Text.ToDateTime(),
|
||
// uiDatetimePicker2.Text.ToDateTime());
|
||
// uiDataGridView2.DataSource = data2;
|
||
// uiDataGridView2.Refresh();
|
||
|
||
// var data3 = conductivityRepository.GetTestData3(uiDatetimePicker1.Text.ToDateTime(),
|
||
// uiDatetimePicker2.Text.ToDateTime());
|
||
// uiDataGridView3.DataSource = data3;
|
||
// uiDataGridView3.Refresh();
|
||
|
||
//}
|
||
}
|
||
|