效果图像:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public event Action<string> errMsg;
private delegate void DelSetProVisi(ProgressBar proBar);
DataTable table = new DataTable();
public double A, B;
public int count=0;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
while (true)
{
dispaly();
Thread.Sleep(1000);
}
});
}
public int ReadExcelXlsxSheetToTable(string filepath, string sheetName, ref DataTable table )
{
//读取表格
lock (filepath)
{
try
{
if (!filepath.Contains(".xlsx"))
{
ErrMsgWrite("File type is fail!Please check you file type");
return -1;
}
FileStream fs = new FileStream(filepath, FileMode.Open);
XSSFWorkbook hssfworkbook = new XSSFWorkbook(fs);
XSSFSheet sheet = (XSSFSheet)hssfworkbook.GetSheet(sheetName);
if (sheet == null)
{
ErrMsgWrite("File is not Exist this Sheet,Please check your file!");
return -1;
}
table = new DataTable();
for (int i = 0; i < sheet.GetRow(0).Cells.Count; i++)
{
table.Columns.Add(sheet.GetRow(0).Cells[i].StringCellValue);
}
int count = 1;
while (true)
{
if (sheet.GetRow(count) != null)
{
DataRow row = table.NewRow();
row.ItemArray = sheet.GetRow(count).Cells.ToArray();
table.Rows.Add(row);
}
else
{
progressBar1.Value = 100;
DialogResult dr = MessageBox.Show("ProgressBar has been finished!");
if (dr.Equals(DialogResult.OK))
{
SetProgressBarVisi(progressBar1);
}
break;
}
count++;
Thread.Sleep(1);
}
return 1;
}
catch (Exception ex)
{
ErrMsgWrite(ex.Message);
return -1;
}
}
}
void ErrMsgWrite(string errStr)
{
if (errMsg != null)
{
errMsg(errStr);
}
}
private void button1_Click(object sender, EventArgs e)
{
ReadExcelXlsxSheetToTable(@"C:\Users\liulang\Desktop\GlueInfo.xlsx", "编号(" + 0 + ")", ref table);
}
void dispaly()//显示图形
{
BeginInvoke(new Action(() =>
{
dataGridView1.DataSource = table;
dataGridView1.Visible = true;
chart1.Series[0].Points.Clear();
for (int i = 0; i < table.Rows.Count; i++)
{
A = Convert.ToDouble(table.Rows[i][1]);
B = Convert.ToDouble(table.Rows[i][2]);
chart1.Series[0].Points.AddXY(A, B);
}
}));
}
private void button3_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(3);
}
private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{
//这个是鼠标滑动对应点显示
if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
{
this.Cursor = Cursors.Cross;
int pointIndex = e.HitTestResult.PointIndex;
DataPoint dp1 = chart1.Series[0].Points[pointIndex];
string YValue1 = dp1.YValues[0].ToString("0.0");
//另外一种遍历显示的方式
//StringBuilder dpStr = new StringBuilder();
//foreach (var item in chart1.Series)
//{
// DataPoint dp = item.Points[pointIndex];
// dpStr.Append(item.Name + " X:" + dp.XValue + " Y:" + dp.YValues[0] + "\r\n");
//}
//e.Text = dpStr.ToString();
}
else
{
this.Cursor = Cursors.Default;
}
//显示点的坐标
for (int i = 0; i < chart1.Series["Series1"].Points.Count; i++)
{
chart1.Series["Series1"].Points[i].ToolTip = "#VALX,#VALY";
}
}
private void chart1_MouseClick(object sender, MouseEventArgs e)
{
//这个是鼠标点击事件
if (e.Button == MouseButtons.Left)
{
HitTestResult hit = chart1.HitTest(e.X, e.Y, ChartElementType.DataPoint);
if (hit.Series != null)
{
int pointIndex = hit.PointIndex;
DataPoint dp; //点中就把y值显示出来
dp = chart1.Series[0].Points[pointIndex];
MessageBox.Show($"X轴:{ chart1.Series[0].Points[pointIndex].XValue.ToString()} Y轴:{chart1.Series[0].Points[pointIndex].YValues[0].ToString()}");
}
else
{
int pointIndex = hit.PointIndex;
DataPoint dp; //点不中的话就清零喽
dp = chart1.Series[0].Points[pointIndex];
}
}
}
private void SetProgressBarVisi(ProgressBar pro)
{
//进度条完成后隐藏
if (this.InvokeRequired)
{
DelSetProVisi setProVisi = new DelSetProVisi(SetProgressBarVisi);
this.Invoke(setProVisi, new object[] { pro });
}
else
{
pro.Visible = false;
}
}
}
}