优化
This commit is contained in:
@@ -128,10 +128,20 @@ namespace 全自动水压检测仪
|
||||
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "barcode",
|
||||
HeaderText = "联络单号和件号",
|
||||
Width = 280,
|
||||
DataPropertyName = "Barcode", // 注意属性名大小写
|
||||
Name = "ContactNumber",
|
||||
HeaderText = "联络单号",
|
||||
Width = 150,
|
||||
DataPropertyName = "ContactNumber",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter }
|
||||
});
|
||||
|
||||
dataGridView.Columns.Add(new DataGridViewTextBoxColumn
|
||||
{
|
||||
Name = "ItemNumber",
|
||||
HeaderText = "件号",
|
||||
Width = 130,
|
||||
DataPropertyName = "ItemNumber",
|
||||
SortMode = DataGridViewColumnSortMode.NotSortable,
|
||||
DefaultCellStyle = { Alignment = DataGridViewContentAlignment.MiddleCenter }
|
||||
});
|
||||
@@ -430,24 +440,16 @@ namespace 全自动水压检测仪
|
||||
using (StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8))
|
||||
{
|
||||
// 写入表头
|
||||
writer.WriteLine("编号,联络单号和件号,时间日期,初始压力(PSI),结束压力(PSI),压差(PSI),保压时间(h),温度模式");
|
||||
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.ContactNumber ?? ""}," +
|
||||
$"{data.ItemNumber ?? ""}," +
|
||||
$"{data.CreateTime:yyyy-MM-dd HH:mm:ss}," +
|
||||
$"{data.startpressure:F2}," +
|
||||
$"{data.endpressure:F2}," +
|
||||
@@ -472,23 +474,24 @@ namespace 全自动水压检测仪
|
||||
writer.WriteLine("=======================================================\n");
|
||||
|
||||
// 写入表头
|
||||
writer.WriteLine(string.Format("{0,-8}{1,-20}{2,-20}{3,-12}{4,-12}{5,-12}{6,-12}{7,-10}",
|
||||
"编号", "联络单号和件号", "时间日期", "初始压力", "保压时间", "压差", "结束压力", "温度模式"));
|
||||
writer.WriteLine(new string('-', 120));
|
||||
writer.WriteLine(string.Format("{0,-8}{1,-15}{2,-15}{3,-20}{4,-12}{5,-12}{6,-12}{7,-12}{8,-10}",
|
||||
"编号", "联络单号", "件号", "时间日期", "初始压力", "结束压力", "压差", "保压时间", "温度模式"));
|
||||
writer.WriteLine(new string('-', 130));
|
||||
|
||||
// 写入数据
|
||||
foreach (var data in CurrentReport)
|
||||
{
|
||||
string tempMode = GetTemperatureModeDisplay(data.Type);
|
||||
|
||||
writer.WriteLine(string.Format("{0,-8}{1,-20}{2,-20}{3,-12:F2}{4,-12:F1}{5,-12:F2}{6,-12:F2}{7,-10}",
|
||||
writer.WriteLine(string.Format("{0,-8}{1,-15}{2,-15}{3,-20}{4,-12:F2}{5,-12:F2}{6,-12:F2}{7,-12:F1}{8,-10}",
|
||||
data.Id,
|
||||
data.barcode,
|
||||
data.ContactNumber ?? "",
|
||||
data.ItemNumber ?? "",
|
||||
data.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
|
||||
data.startpressure,
|
||||
data.dwelltime,
|
||||
data.diffpressure,
|
||||
data.endpressure,
|
||||
data.diffpressure,
|
||||
data.dwelltime,
|
||||
tempMode));
|
||||
}
|
||||
}
|
||||
@@ -498,14 +501,6 @@ namespace 全自动水压检测仪
|
||||
/// </summary>
|
||||
private void ExportToExcel(string filePath)
|
||||
{
|
||||
//string csvPath = Path.ChangeExtension(filePath, ".csv");
|
||||
//ExportToCsv(csvPath);
|
||||
|
||||
//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("测试数据报表");
|
||||
@@ -517,7 +512,7 @@ namespace 全自动水压检测仪
|
||||
int currentRow = 1;
|
||||
|
||||
// 主标题
|
||||
worksheet.Cells[currentRow, 1, currentRow, 8].Merge = true;
|
||||
worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true;
|
||||
var titleCell = worksheet.Cells[currentRow, 1];
|
||||
titleCell.Value = "测试数据报表";
|
||||
titleCell.Style.Font.Size = 16;
|
||||
@@ -528,7 +523,7 @@ namespace 全自动水压检测仪
|
||||
currentRow++;
|
||||
|
||||
// 副标题
|
||||
worksheet.Cells[currentRow, 1, currentRow, 8].Merge = true;
|
||||
worksheet.Cells[currentRow, 1, currentRow, 9].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;
|
||||
@@ -537,7 +532,7 @@ namespace 全自动水压检测仪
|
||||
currentRow += 2;
|
||||
|
||||
// 表头
|
||||
string[] headers = { "编号", "联络单号和件号", "时间日期", "初始压力(PSI)", "结束压力(PSI)", "压差(PSI)", "保压时间(h)", "温度模式" };
|
||||
string[] headers = { "编号", "联络单号", "件号", "时间日期", "初始压力(PSI)", "结束压力(PSI)", "压差(PSI)", "保压时间(h)", "温度模式" };
|
||||
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
{
|
||||
@@ -565,16 +560,17 @@ namespace 全自动水压检测仪
|
||||
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;
|
||||
worksheet.Cells[currentRow, 2].Value = data.ContactNumber ?? "";
|
||||
worksheet.Cells[currentRow, 3].Value = data.ItemNumber ?? "";
|
||||
worksheet.Cells[currentRow, 4].Value = data.CreateTime;
|
||||
worksheet.Cells[currentRow, 5].Value = data.startpressure;
|
||||
worksheet.Cells[currentRow, 6].Value = data.endpressure;
|
||||
worksheet.Cells[currentRow, 7].Value = data.diffpressure;
|
||||
worksheet.Cells[currentRow, 8].Value = data.dwelltime;
|
||||
worksheet.Cells[currentRow, 9].Value = tempMode;
|
||||
|
||||
// 设置数据行样式
|
||||
for (int col = 1; col <= 8; col++)
|
||||
for (int col = 1; col <= 9; col++)
|
||||
{
|
||||
var cell = worksheet.Cells[currentRow, col];
|
||||
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
||||
@@ -590,11 +586,11 @@ namespace 全自动水压检测仪
|
||||
}
|
||||
|
||||
// 设置格式
|
||||
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, 4].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
|
||||
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.Cells[currentRow, 7].Style.Numberformat.Format = "0.00";
|
||||
worksheet.Cells[currentRow, 8].Style.Numberformat.Format = "0.0";
|
||||
|
||||
// 设置行高
|
||||
worksheet.Row(currentRow).Height = 25;
|
||||
|
||||
Reference in New Issue
Block a user