This commit is contained in:
@@ -91,6 +91,9 @@
|
||||
Column2 = new DataGridViewTextBoxColumn();
|
||||
uiPanel1 = new Sunny.UI.UIPanel();
|
||||
uiGroupBox1 = new Sunny.UI.UIGroupBox();
|
||||
uiComboBox3 = new Sunny.UI.UIComboBox();
|
||||
uiComboBox2 = new Sunny.UI.UIComboBox();
|
||||
uiButton4 = new Sunny.UI.UIButton();
|
||||
uiComboBox1 = new Sunny.UI.UIComboBox();
|
||||
uiLabel1 = new Sunny.UI.UILabel();
|
||||
uiButton3 = new Sunny.UI.UIButton();
|
||||
@@ -107,8 +110,6 @@
|
||||
dataGridViewTextBoxColumn13 = new DataGridViewTextBoxColumn();
|
||||
dataGridViewTextBoxColumn14 = new DataGridViewTextBoxColumn();
|
||||
dataGridViewTextBoxColumn15 = new DataGridViewTextBoxColumn();
|
||||
uiComboBox2 = new Sunny.UI.UIComboBox();
|
||||
uiButton4 = new Sunny.UI.UIButton();
|
||||
uiTableLayoutPanel1.SuspendLayout();
|
||||
uiPanel2.SuspendLayout();
|
||||
uiGroupBox2.SuspendLayout();
|
||||
@@ -692,6 +693,7 @@
|
||||
//
|
||||
// uiGroupBox1
|
||||
//
|
||||
uiGroupBox1.Controls.Add(uiComboBox3);
|
||||
uiGroupBox1.Controls.Add(uiComboBox2);
|
||||
uiGroupBox1.Controls.Add(uiButton4);
|
||||
uiGroupBox1.Controls.Add(uiComboBox1);
|
||||
@@ -721,6 +723,70 @@
|
||||
uiGroupBox1.Text = "⚙️ 报告设置";
|
||||
uiGroupBox1.TextAlignment = ContentAlignment.MiddleCenter;
|
||||
//
|
||||
// uiComboBox3
|
||||
//
|
||||
uiComboBox3.DataSource = null;
|
||||
uiComboBox3.FillColor = Color.White;
|
||||
uiComboBox3.Font = new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiComboBox3.ItemHoverColor = Color.FromArgb(235, 245, 255);
|
||||
uiComboBox3.ItemSelectForeColor = Color.White;
|
||||
uiComboBox3.Location = new Point(121, 623);
|
||||
uiComboBox3.Margin = new Padding(4, 5, 4, 5);
|
||||
uiComboBox3.MinimumSize = new Size(63, 0);
|
||||
uiComboBox3.Name = "uiComboBox3";
|
||||
uiComboBox3.Padding = new Padding(0, 0, 30, 2);
|
||||
uiComboBox3.Radius = 8;
|
||||
uiComboBox3.RectColor = Color.FromArgb(52, 152, 219);
|
||||
uiComboBox3.Size = new Size(225, 44);
|
||||
uiComboBox3.SymbolSize = 24;
|
||||
uiComboBox3.TabIndex = 13;
|
||||
uiComboBox3.Text = "请选择报告";
|
||||
uiComboBox3.TextAlignment = ContentAlignment.MiddleLeft;
|
||||
uiComboBox3.Watermark = "";
|
||||
//
|
||||
// uiComboBox2
|
||||
//
|
||||
uiComboBox2.DataSource = null;
|
||||
uiComboBox2.FillColor = Color.White;
|
||||
uiComboBox2.Font = new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiComboBox2.ItemHoverColor = Color.FromArgb(235, 245, 255);
|
||||
uiComboBox2.ItemSelectForeColor = Color.White;
|
||||
uiComboBox2.Location = new Point(121, 477);
|
||||
uiComboBox2.Margin = new Padding(4, 5, 4, 5);
|
||||
uiComboBox2.MinimumSize = new Size(63, 0);
|
||||
uiComboBox2.Name = "uiComboBox2";
|
||||
uiComboBox2.Padding = new Padding(0, 0, 30, 2);
|
||||
uiComboBox2.Radius = 8;
|
||||
uiComboBox2.RectColor = Color.FromArgb(52, 152, 219);
|
||||
uiComboBox2.Size = new Size(225, 44);
|
||||
uiComboBox2.SymbolSize = 24;
|
||||
uiComboBox2.TabIndex = 12;
|
||||
uiComboBox2.Text = "请选择报告";
|
||||
uiComboBox2.TextAlignment = ContentAlignment.MiddleLeft;
|
||||
uiComboBox2.Watermark = "";
|
||||
//
|
||||
// uiButton4
|
||||
//
|
||||
uiButton4.Cursor = Cursors.Hand;
|
||||
uiButton4.FillColor = Color.FromArgb(108, 117, 125);
|
||||
uiButton4.FillColor2 = Color.FromArgb(108, 117, 125);
|
||||
uiButton4.FillHoverColor = Color.FromArgb(84, 91, 98);
|
||||
uiButton4.FillPressColor = Color.FromArgb(62, 68, 73);
|
||||
uiButton4.Font = new Font("微软雅黑", 13F, FontStyle.Bold, GraphicsUnit.Point, 134);
|
||||
uiButton4.Location = new Point(143, 529);
|
||||
uiButton4.MinimumSize = new Size(1, 1);
|
||||
uiButton4.Name = "uiButton4";
|
||||
uiButton4.Radius = 8;
|
||||
uiButton4.RectColor = Color.FromArgb(108, 117, 125);
|
||||
uiButton4.RectHoverColor = Color.FromArgb(84, 91, 98);
|
||||
uiButton4.Size = new Size(180, 60);
|
||||
uiButton4.Style = Sunny.UI.UIStyle.Custom;
|
||||
uiButton4.StyleCustomMode = true;
|
||||
uiButton4.TabIndex = 11;
|
||||
uiButton4.Text = "🗑️ 清除";
|
||||
uiButton4.TipsFont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiButton4.Click += uiButton4_Click;
|
||||
//
|
||||
// uiComboBox1
|
||||
//
|
||||
uiComboBox1.DataSource = null;
|
||||
@@ -763,7 +829,7 @@
|
||||
uiButton3.FillHoverColor = Color.FromArgb(243, 156, 18);
|
||||
uiButton3.FillPressColor = Color.FromArgb(243, 156, 18);
|
||||
uiButton3.Font = new Font("微软雅黑", 13F, FontStyle.Bold, GraphicsUnit.Point, 134);
|
||||
uiButton3.Location = new Point(152, 811);
|
||||
uiButton3.Location = new Point(143, 780);
|
||||
uiButton3.MinimumSize = new Size(1, 1);
|
||||
uiButton3.Name = "uiButton3";
|
||||
uiButton3.Radius = 8;
|
||||
@@ -785,7 +851,7 @@
|
||||
uiButton2.FillHoverColor = Color.FromArgb(39, 174, 96);
|
||||
uiButton2.FillPressColor = Color.FromArgb(39, 174, 96);
|
||||
uiButton2.Font = new Font("微软雅黑", 13F, FontStyle.Bold, GraphicsUnit.Point, 134);
|
||||
uiButton2.Location = new Point(152, 711);
|
||||
uiButton2.Location = new Point(143, 675);
|
||||
uiButton2.MinimumSize = new Size(1, 1);
|
||||
uiButton2.Name = "uiButton2";
|
||||
uiButton2.Radius = 8;
|
||||
@@ -797,6 +863,7 @@
|
||||
uiButton2.TabIndex = 7;
|
||||
uiButton2.Text = "📤 导出报告";
|
||||
uiButton2.TipsFont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiButton2.Click += uiButton2_Click;
|
||||
//
|
||||
// uiButton1
|
||||
//
|
||||
@@ -938,48 +1005,6 @@
|
||||
dataGridViewTextBoxColumn15.Name = "dataGridViewTextBoxColumn15";
|
||||
dataGridViewTextBoxColumn15.Width = 150;
|
||||
//
|
||||
// uiComboBox2
|
||||
//
|
||||
uiComboBox2.DataSource = null;
|
||||
uiComboBox2.FillColor = Color.White;
|
||||
uiComboBox2.Font = new Font("微软雅黑", 12F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiComboBox2.ItemHoverColor = Color.FromArgb(235, 245, 255);
|
||||
uiComboBox2.ItemSelectForeColor = Color.White;
|
||||
uiComboBox2.Location = new Point(121, 477);
|
||||
uiComboBox2.Margin = new Padding(4, 5, 4, 5);
|
||||
uiComboBox2.MinimumSize = new Size(63, 0);
|
||||
uiComboBox2.Name = "uiComboBox2";
|
||||
uiComboBox2.Padding = new Padding(0, 0, 30, 2);
|
||||
uiComboBox2.Radius = 8;
|
||||
uiComboBox2.RectColor = Color.FromArgb(52, 152, 219);
|
||||
uiComboBox2.Size = new Size(225, 44);
|
||||
uiComboBox2.SymbolSize = 24;
|
||||
uiComboBox2.TabIndex = 12;
|
||||
uiComboBox2.Text = "请选择报告";
|
||||
uiComboBox2.TextAlignment = ContentAlignment.MiddleLeft;
|
||||
uiComboBox2.Watermark = "";
|
||||
//
|
||||
// uiButton4
|
||||
//
|
||||
uiButton4.Cursor = Cursors.Hand;
|
||||
uiButton4.FillColor = Color.FromArgb(52, 152, 219);
|
||||
uiButton4.FillColor2 = Color.FromArgb(52, 152, 219);
|
||||
uiButton4.FillHoverColor = Color.FromArgb(41, 128, 185);
|
||||
uiButton4.FillPressColor = Color.FromArgb(41, 128, 185);
|
||||
uiButton4.Font = new Font("微软雅黑", 13F, FontStyle.Bold, GraphicsUnit.Point, 134);
|
||||
uiButton4.Location = new Point(143, 529);
|
||||
uiButton4.MinimumSize = new Size(1, 1);
|
||||
uiButton4.Name = "uiButton4";
|
||||
uiButton4.Radius = 8;
|
||||
uiButton4.RectColor = Color.FromArgb(52, 152, 219);
|
||||
uiButton4.RectHoverColor = Color.FromArgb(41, 128, 185);
|
||||
uiButton4.Size = new Size(180, 60);
|
||||
uiButton4.Style = Sunny.UI.UIStyle.Custom;
|
||||
uiButton4.StyleCustomMode = true;
|
||||
uiButton4.TabIndex = 11;
|
||||
uiButton4.Text = "🔍 清除";
|
||||
uiButton4.TipsFont = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
//
|
||||
// ReportForm
|
||||
//
|
||||
AutoScaleMode = AutoScaleMode.None;
|
||||
@@ -1072,5 +1097,6 @@
|
||||
private DataGridViewTextBoxColumn Column2;
|
||||
private Sunny.UI.UIComboBox uiComboBox2;
|
||||
private Sunny.UI.UIButton uiButton4;
|
||||
private Sunny.UI.UIComboBox uiComboBox3;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Sunny.UI;
|
||||
using OfficeOpenXml;
|
||||
using OfficeOpenXml.Style;
|
||||
using Sunny.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
@@ -10,6 +12,7 @@ using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using 材料热传导系数;
|
||||
|
||||
|
||||
namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
{
|
||||
public partial class ReportForm : UIForm
|
||||
@@ -20,14 +23,20 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
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) =>
|
||||
{
|
||||
// 当子窗体关闭时,显示主窗体
|
||||
@@ -59,6 +68,30 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
};
|
||||
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();//返回主页面
|
||||
@@ -73,7 +106,6 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
// 终止时间:今天
|
||||
uiDatetimePicker2.Value = DateTime.Now;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void uiButton1_Click(object sender, EventArgs e)
|
||||
@@ -132,6 +164,328 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
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);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="EPPlus" Version="5.4.2" />
|
||||
<PackageReference Include="MySql.Data" Version="9.5.0" />
|
||||
<PackageReference Include="NModbus" Version="3.0.81" />
|
||||
<PackageReference Include="SunnyUI" Version="3.9.1" />
|
||||
|
||||
Reference in New Issue
Block a user