This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Modbus.Device;
|
||||
using OfficeOpenXml;
|
||||
using OfficeOpenXml.Style;
|
||||
using Sunny.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -118,7 +120,7 @@ namespace 全自动水压检测仪
|
||||
{
|
||||
Name = "Id",
|
||||
HeaderText = "编号",
|
||||
Width = 80,
|
||||
Width = 70,
|
||||
DataPropertyName = "Id", // 绑定到ConductivityTestData的Id属性
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter }
|
||||
@@ -128,7 +130,7 @@ namespace 全自动水压检测仪
|
||||
{
|
||||
Name = "barcode",
|
||||
HeaderText = "条码",
|
||||
Width = 200,
|
||||
Width = 280,
|
||||
DataPropertyName = "Barcode", // 注意属性名大小写
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter }
|
||||
@@ -138,7 +140,7 @@ namespace 全自动水压检测仪
|
||||
{
|
||||
Name = "CreateTime",
|
||||
HeaderText = "时间日期",
|
||||
Width = 180,
|
||||
Width = 170,
|
||||
DataPropertyName = "CreateTime",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = {
|
||||
@@ -150,8 +152,8 @@ namespace 全自动水压检测仪
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "startpressure",
|
||||
HeaderText = "初始压力",
|
||||
Width = 120,
|
||||
HeaderText = "初始压力(PSI)",
|
||||
Width = 95,
|
||||
DataPropertyName = "StartPressure",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = {
|
||||
@@ -162,22 +164,24 @@ namespace 全自动水压检测仪
|
||||
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "dwelltime",
|
||||
HeaderText = "保压时间",
|
||||
Width = 120,
|
||||
DataPropertyName = "DwellTime",
|
||||
Name = "endpressure",
|
||||
HeaderText = "结束压力(PSI)",
|
||||
Width = 95,
|
||||
DataPropertyName = "EndPressure",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = {
|
||||
Alignment = DataGridViewContentAlignment.MiddleCenter,
|
||||
Format = "F1" // 保留1位小数
|
||||
Format = "F2"
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "diffpressure",
|
||||
HeaderText = "压差",
|
||||
Width = 130,
|
||||
HeaderText = "压差(PSI)",
|
||||
Width = 75,
|
||||
DataPropertyName = "DiffPressure",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = {
|
||||
@@ -188,14 +192,14 @@ namespace 全自动水压检测仪
|
||||
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "endpressure",
|
||||
HeaderText = "结束压力",
|
||||
Width = 130,
|
||||
DataPropertyName = "EndPressure",
|
||||
Name = "dwelltime",
|
||||
HeaderText = "保压时间(h)",
|
||||
Width = 80,
|
||||
DataPropertyName = "DwellTime",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = {
|
||||
Alignment = DataGridViewContentAlignment.MiddleCenter,
|
||||
Format = "F2"
|
||||
Format = "F1" // 保留1位小数
|
||||
}
|
||||
});
|
||||
|
||||
@@ -426,20 +430,29 @@ namespace 全自动水压检测仪
|
||||
using (StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8))
|
||||
{
|
||||
// 写入表头
|
||||
writer.WriteLine("编号,条码,时间日期,初始压力,保压时间,压差,结束压力,温度模式");
|
||||
writer.WriteLine("编号,条码,时间日期,初始压力(PSI),结束压力(PSI),压差(PSI),保压时间(h),温度模式");
|
||||
|
||||
// 写入数据
|
||||
foreach (var data in CurrentReport)
|
||||
{
|
||||
string tempMode = GetTemperatureModeDisplay(data.Type);
|
||||
|
||||
//writer.WriteLine($"{data.Id}," +
|
||||
// $"{data.barcode}," +
|
||||
// $"{data.CreateTime:yyyy-MM-dd HH:mm:ss}," +
|
||||
// $"{data.startpressure:F2}," +
|
||||
// $"{data.dwelltime:F1}," +
|
||||
// $"{data.diffpressure:F2}," +
|
||||
// $"{data.endpressure:F2}," +
|
||||
// $"{tempMode}");
|
||||
|
||||
writer.WriteLine($"{data.Id}," +
|
||||
$"{data.barcode}," +
|
||||
$"{data.CreateTime:yyyy-MM-dd HH:mm:ss}," +
|
||||
$"{data.startpressure:F2}," +
|
||||
$"{data.dwelltime:F1}," +
|
||||
$"{data.diffpressure:F2}," +
|
||||
$"{data.endpressure:F2}," +
|
||||
$"{data.diffpressure:F2}," +
|
||||
$"{data.dwelltime:F1}," +
|
||||
$"{tempMode}");
|
||||
}
|
||||
}
|
||||
@@ -485,12 +498,116 @@ namespace 全自动水压检测仪
|
||||
/// </summary>
|
||||
private void ExportToExcel(string filePath)
|
||||
{
|
||||
string csvPath = Path.ChangeExtension(filePath, ".csv");
|
||||
ExportToCsv(csvPath);
|
||||
//string csvPath = Path.ChangeExtension(filePath, ".csv");
|
||||
//ExportToCsv(csvPath);
|
||||
|
||||
MessageBox.Show($"由于未安装Excel库,已导出为CSV文件:\n{csvPath}\n\n" +
|
||||
"您可以将CSV文件导入Excel使用。", "导出提示",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
//MessageBox.Show($"由于未安装Excel库,已导出为CSV文件:\n{csvPath}\n\n" +
|
||||
// "您可以将CSV文件导入Excel使用。", "导出提示",
|
||||
// MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
//ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途
|
||||
|
||||
using (var package = new ExcelPackage())
|
||||
{
|
||||
var worksheet = package.Workbook.Worksheets.Add("测试数据报表");
|
||||
|
||||
// 设置默认样式
|
||||
worksheet.Cells.Style.Font.Name = "微软雅黑";
|
||||
worksheet.Cells.Style.Font.Size = 11;
|
||||
|
||||
int currentRow = 1;
|
||||
|
||||
// 主标题
|
||||
worksheet.Cells[currentRow, 1, currentRow, 8].Merge = true;
|
||||
var titleCell = worksheet.Cells[currentRow, 1];
|
||||
titleCell.Value = "测试数据报表";
|
||||
titleCell.Style.Font.Size = 16;
|
||||
titleCell.Style.Font.Bold = true;
|
||||
titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||||
titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||||
|
||||
currentRow++;
|
||||
|
||||
// 副标题
|
||||
worksheet.Cells[currentRow, 1, currentRow, 8].Merge = true;
|
||||
var subTitleCell = worksheet.Cells[currentRow, 1];
|
||||
subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{CurrentReport.Count}条";
|
||||
subTitleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||||
subTitleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||||
|
||||
currentRow += 2;
|
||||
|
||||
// 表头
|
||||
string[] headers = { "编号", "条码", "时间日期", "初始压力(PSI)", "结束压力(PSI)", "压差(PSI)", "保压时间(h)", "温度模式" };
|
||||
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
{
|
||||
var cell = worksheet.Cells[currentRow, i + 1];
|
||||
cell.Value = headers[i];
|
||||
cell.Style.Font.Bold = true;
|
||||
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||||
cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||||
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
|
||||
cell.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue);
|
||||
cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.Black);
|
||||
|
||||
// 设置列宽
|
||||
worksheet.Column(i + 1).Width = 15;
|
||||
}
|
||||
|
||||
// 设置表头行高
|
||||
worksheet.Row(currentRow).Height = 35;
|
||||
|
||||
currentRow++;
|
||||
|
||||
// 数据行
|
||||
foreach (var data in CurrentReport)
|
||||
{
|
||||
string tempMode = GetTemperatureModeDisplay(data.Type);
|
||||
|
||||
worksheet.Cells[currentRow, 1].Value = data.Id;
|
||||
worksheet.Cells[currentRow, 2].Value = data.barcode;
|
||||
worksheet.Cells[currentRow, 3].Value = data.CreateTime;
|
||||
worksheet.Cells[currentRow, 4].Value = data.startpressure;
|
||||
worksheet.Cells[currentRow, 5].Value = data.endpressure;
|
||||
worksheet.Cells[currentRow, 6].Value = data.diffpressure;
|
||||
worksheet.Cells[currentRow, 7].Value = data.dwelltime;
|
||||
worksheet.Cells[currentRow, 8].Value = tempMode;
|
||||
|
||||
// 设置数据行样式
|
||||
for (int col = 1; col <= 8; col++)
|
||||
{
|
||||
var cell = worksheet.Cells[currentRow, col];
|
||||
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||||
cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
||||
cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.LightGray);
|
||||
|
||||
// 隔行变色
|
||||
if (currentRow % 2 == 0)
|
||||
{
|
||||
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
|
||||
cell.Style.Fill.BackgroundColor.SetColor(Color.AliceBlue);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置格式
|
||||
worksheet.Cells[currentRow, 3].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
|
||||
worksheet.Cells[currentRow, 4].Style.Numberformat.Format = "0.00";
|
||||
worksheet.Cells[currentRow, 5].Style.Numberformat.Format = "0.00";
|
||||
worksheet.Cells[currentRow, 6].Style.Numberformat.Format = "0.00";
|
||||
worksheet.Cells[currentRow, 7].Style.Numberformat.Format = "0.0";
|
||||
|
||||
// 设置行高
|
||||
worksheet.Row(currentRow).Height = 25;
|
||||
|
||||
currentRow++;
|
||||
}
|
||||
|
||||
// 自动调整列宽
|
||||
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
|
||||
|
||||
// 保存
|
||||
package.SaveAs(new FileInfo(filePath));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user