更新
This commit is contained in:
@@ -196,8 +196,358 @@ namespace WindowsFormsApp6
|
||||
Application.Exit();
|
||||
}
|
||||
|
||||
private void CreateForm1Sheet(IWorkbook workbook) { }
|
||||
private void CreateForm2Sheet(IWorkbook workbook) { }
|
||||
private void CreateForm3Sheet(IWorkbook workbook) { }
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user