238 lines
8.8 KiB
C#
238 lines
8.8 KiB
C#
|
|
using OfficeOpenXml;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Drawing;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Windows.Forms;
|
|||
|
|
using System.Windows.Forms.DataVisualization.Charting;
|
|||
|
|
|
|||
|
|
namespace 口罩泄露测试仪控制系统_定制款.窗体
|
|||
|
|
{
|
|||
|
|
public partial class frm_ExperData : Form
|
|||
|
|
{
|
|||
|
|
private ToolTip toolTip=new ToolTip();
|
|||
|
|
public frm_ExperData()
|
|||
|
|
{
|
|||
|
|
InitializeComponent();
|
|||
|
|
dataGridView1.Font= new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); ;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void frm_ExperData_Load(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
// DataTable datatable = new DataTable();
|
|||
|
|
private void button1_Click(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
// 弹出文件选择框
|
|||
|
|
OpenFileDialog openFileDialog = new OpenFileDialog
|
|||
|
|
{
|
|||
|
|
Filter = "Excel Files|*.xlsx;*.xls",
|
|||
|
|
Title = "选择一个 Excel 文件"
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|||
|
|
{
|
|||
|
|
string filePath = openFileDialog.FileName;
|
|||
|
|
textBox1.Text = filePath;
|
|||
|
|
// 读取 Excel 文件并写入 DataTable
|
|||
|
|
DataTable dataTable = ReadExcelToDataTable(filePath);
|
|||
|
|
|
|||
|
|
// 显示 DataTable 中的数据(示例:输出到控制台)
|
|||
|
|
DisplayDataTable(dataTable);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private DataTable ReadExcelToDataTable(string filePath)
|
|||
|
|
{
|
|||
|
|
// 创建 DataTable
|
|||
|
|
DataTable dataTable = new DataTable();
|
|||
|
|
|
|||
|
|
// 使用 EPPlus 读取 Excel 文件
|
|||
|
|
using (var package = new ExcelPackage(new FileInfo(filePath)))
|
|||
|
|
{
|
|||
|
|
// 获取第一个工作表
|
|||
|
|
var worksheet = package.Workbook.Worksheets[0];
|
|||
|
|
|
|||
|
|
// 读取表头
|
|||
|
|
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
|
|||
|
|
{
|
|||
|
|
string columnName = worksheet.Cells[1, col].Text;
|
|||
|
|
dataTable.Columns.Add(columnName);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 读取数据
|
|||
|
|
for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
|
|||
|
|
{
|
|||
|
|
DataRow dataRow = dataTable.NewRow();
|
|||
|
|
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
|
|||
|
|
{
|
|||
|
|
dataRow[col - 1] = worksheet.Cells[row, col].Text;
|
|||
|
|
}
|
|||
|
|
dataTable.Rows.Add(dataRow);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return dataTable;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void DisplayDataTable(DataTable dataTable)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
dataGridView1.DataSource = dataTable;
|
|||
|
|
dataGridView1.Refresh();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
|
|||
|
|
{
|
|||
|
|
// 确保点击的是第一列
|
|||
|
|
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
|
|||
|
|
{
|
|||
|
|
int rowIndex = e.RowIndex + 1; // Excel 行号从 1 开始
|
|||
|
|
string excelFilePath = textBox1.Text; // 替换为实际路径
|
|||
|
|
|
|||
|
|
// 读取 Excel 数据
|
|||
|
|
ReadExcelData(excelFilePath, rowIndex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private void ReadExcelData(string excelFilePath, int rowIndex)
|
|||
|
|
{
|
|||
|
|
// 定义列表
|
|||
|
|
List<string> stringList = new List<string>();
|
|||
|
|
List<float> floatList1 = new List<float>();
|
|||
|
|
List<float> floatList2 = new List<float>();
|
|||
|
|
List<float> floatList3 = new List<float>();
|
|||
|
|
List<float> floatList4 = new List<float>();
|
|||
|
|
List<float> floatList5 = new List<float>();
|
|||
|
|
List<float> floatList6 = new List<float>();
|
|||
|
|
List<float> floatList7 = new List<float>();
|
|||
|
|
List<float> floatList8 = new List<float>();
|
|||
|
|
|
|||
|
|
// 使用 EPPlus 读取 Excel 文件
|
|||
|
|
using (var package = new ExcelPackage(new FileInfo(excelFilePath)))
|
|||
|
|
{
|
|||
|
|
// 获取第一个工作表
|
|||
|
|
var worksheet = package.Workbook.Worksheets[rowIndex];
|
|||
|
|
|
|||
|
|
// 读取数据
|
|||
|
|
for (int row = 2; row <= worksheet.Dimension.End.Row; row++)
|
|||
|
|
{
|
|||
|
|
// 读取第一列数据(字符串)
|
|||
|
|
stringList.Add(worksheet.Cells[row, 1].Text);
|
|||
|
|
|
|||
|
|
// 读取第二列到第九列数据(浮点数)
|
|||
|
|
floatList1.Add(float.Parse(worksheet.Cells[row, 2].Text));
|
|||
|
|
floatList2.Add(float.Parse(worksheet.Cells[row, 3].Text));
|
|||
|
|
floatList3.Add(float.Parse(worksheet.Cells[row, 4].Text));
|
|||
|
|
floatList4.Add(float.Parse(worksheet.Cells[row, 5].Text));
|
|||
|
|
floatList5.Add(float.Parse(worksheet.Cells[row, 6].Text));
|
|||
|
|
floatList6.Add(float.Parse(worksheet.Cells[row, 7].Text));
|
|||
|
|
floatList7.Add(float.Parse(worksheet.Cells[row, 8].Text));
|
|||
|
|
floatList8.Add(float.Parse(worksheet.Cells[row, 9].Text));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 输出结果(示例:输出到控制台)
|
|||
|
|
//chart_TSI.Series[0].Points.DataBindXY(stringList, floatList1);
|
|||
|
|
chart_TSI.Series[0].Points.DataBindXY(stringList, floatList1);
|
|||
|
|
chart_TSI.Series[1].Points.DataBindXY(stringList, floatList2);
|
|||
|
|
chart_TSI.Series[2].Points.DataBindXY(stringList, floatList3);
|
|||
|
|
chart2.Series[0].Points.DataBindXY(stringList, floatList4);
|
|||
|
|
chart2.Series[1].Points.DataBindXY(stringList, floatList5);
|
|||
|
|
chart2.Series[2].Points.DataBindXY(stringList, floatList6);
|
|||
|
|
chart2.Series[3].Points.DataBindXY(stringList, floatList7);
|
|||
|
|
chart2.Series[4].Points.DataBindXY(stringList, floatList8);
|
|||
|
|
//chart_TSI.Series[1].Points.DataBindY(floatList1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void chart2_MouseMove(object sender, MouseEventArgs e)
|
|||
|
|
{
|
|||
|
|
// 获取鼠标位置的坐标
|
|||
|
|
var hitTestResult = chart2.HitTest(e.X, e.Y);
|
|||
|
|
|
|||
|
|
if (hitTestResult.ChartElementType == ChartElementType.DataPoint)
|
|||
|
|
{
|
|||
|
|
// 获取数据点
|
|||
|
|
DataPoint dataPoint = hitTestResult.Series.Points[hitTestResult.PointIndex];
|
|||
|
|
|
|||
|
|
// 显示十字坐标
|
|||
|
|
DrawCrosshair(hitTestResult.ChartArea, dataPoint);
|
|||
|
|
|
|||
|
|
// 显示 ToolTip
|
|||
|
|
toolTip.Show($"X: {dataPoint.XValue}, Y: {dataPoint.YValues[0]}", chart2, e.X + 10, e.Y + 10);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// 隐藏十字坐标和 ToolTip
|
|||
|
|
ClearCrosshair(hitTestResult.ChartArea);
|
|||
|
|
toolTip.Hide(chart2);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private void DrawCrosshair(ChartArea chartArea, DataPoint dataPoint)
|
|||
|
|
{
|
|||
|
|
// 清除之前的十字坐标
|
|||
|
|
ClearCrosshair(chartArea);
|
|||
|
|
|
|||
|
|
// 添加垂直线
|
|||
|
|
StripLine verticalLine = new StripLine
|
|||
|
|
{
|
|||
|
|
Interval = 0,
|
|||
|
|
IntervalOffset = dataPoint.XValue,
|
|||
|
|
StripWidth = 0.1,
|
|||
|
|
BorderColor = Color.Red,
|
|||
|
|
BorderWidth = 1
|
|||
|
|
};
|
|||
|
|
chartArea.AxisX.StripLines.Add(verticalLine);
|
|||
|
|
|
|||
|
|
// 添加水平线
|
|||
|
|
StripLine horizontalLine = new StripLine
|
|||
|
|
{
|
|||
|
|
Interval = 0,
|
|||
|
|
IntervalOffset = dataPoint.YValues[0],
|
|||
|
|
StripWidth = 0.1,
|
|||
|
|
BorderColor = Color.Red,
|
|||
|
|
BorderWidth = 1
|
|||
|
|
};
|
|||
|
|
chartArea.AxisY.StripLines.Add(horizontalLine);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ClearCrosshair(ChartArea chartArea)
|
|||
|
|
{
|
|||
|
|
if (chartArea != null)
|
|||
|
|
{ // 清除垂直线
|
|||
|
|
chartArea.AxisX.StripLines.Clear();
|
|||
|
|
|
|||
|
|
// 清除水平线
|
|||
|
|
chartArea.AxisY.StripLines.Clear();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void chart_TSI_MouseMove(object sender, MouseEventArgs e)
|
|||
|
|
{
|
|||
|
|
// 获取鼠标位置的坐标
|
|||
|
|
var hitTestResult = chart_TSI.HitTest(e.X, e.Y);
|
|||
|
|
|
|||
|
|
if (hitTestResult.ChartElementType == ChartElementType.DataPoint)
|
|||
|
|
{
|
|||
|
|
// 获取数据点
|
|||
|
|
DataPoint dataPoint = hitTestResult.Series.Points[hitTestResult.PointIndex];
|
|||
|
|
|
|||
|
|
// 显示十字坐标
|
|||
|
|
DrawCrosshair(hitTestResult.ChartArea, dataPoint);
|
|||
|
|
|
|||
|
|
// 显示 ToolTip
|
|||
|
|
toolTip.Show($"X: {dataPoint.XValue}, Y: {dataPoint.YValues[0]}", chart_TSI, e.X + 10, e.Y + 10);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// 隐藏十字坐标和 ToolTip
|
|||
|
|
ClearCrosshair(hitTestResult.ChartArea);
|
|||
|
|
toolTip.Hide(chart_TSI);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|