C#怎么根据excel数据绘制坐标图
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍了C#怎么根据excel数据绘制坐标图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#怎么根据excel数据绘制坐标图文章都会有所收获,下面我们一起来看
千家信息网最后更新 2026年11月03日C#怎么根据excel数据绘制坐标图
这篇文章主要介绍了C#怎么根据excel数据绘制坐标图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#怎么根据excel数据绘制坐标图文章都会有所收获,下面我们一起来看看吧。
效果如下图
界面

代码
using System;using System.Collections;using System.Collections.Generic;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;using System.Windows.Forms.DataVisualization.Charting;namespace WindowsFormsApp2{ public partial class Form1 : Form { //x和y轴数据 double[] x = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] y = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; List xList = new List(); List yList = new List(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string fname = ""; OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Excel File Dialog"; fdlg.InitialDirectory = @"c:\"; fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; fdlg.FilterIndex = 2; fdlg.RestoreDirectory = true; if (fdlg.ShowDialog() == DialogResult.OK) { fname = fdlg.FileName; } Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname); Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { double px = System.Convert.ToDouble(xlRange.Cells[i, 1].Value2.ToString()); double py = System.Convert.ToDouble(xlRange.Cells[i, 2].Value2.ToString()); Console.Out.WriteLine("第" + i + "行 :" + px + "," + py); xList.Add(px); yList.Add(py); //for (int j = 1; j <= colCount; j++) //{ //write the value to the Grid //if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) //{ //xList.Add(xlRange.Cells[i, j]); // Console.WriteLine(xlRange.Cells[i, j].Value2.ToString()); //add useful things here! // } //} } chart1.Series[0].Points.DataBindXY(xList, yList); //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); //rule of thumb for releasing com objects: // never use two dots, all COM objects must be referenced and released individually // ex: [somthing].[something].[something] is bad //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet); //close and release xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook); //quit and release xlApp.Quit(); Marshal.ReleaseComObject(xlApp); } //Graphics g = this.CreateGraphics(); //Pen pen = new Pen(Brushes.Red, 1); //g.DrawLine(pen, new Point(30, 50), new Point(250, 250)); private void Form1_Load(object sender, EventArgs e) { //控件chart背景色 //chart1.BackColor = Color.Transparent;//Color.Transparent系统定义的颜色 //chart1.BackColor = Color.White; //图表标题, chart1.Titles.Add("测试数据"); //添加title到titleCollection集合的末尾 chart1.Titles[0].ForeColor = Color.DarkBlue;//设置title的文本颜色 chart1.Titles[0].Font = new Font("微软雅黑", 15f, FontStyle.Regular);//设置title的字体 chart1.Titles[0].Alignment = ContentAlignment.TopCenter;//设置title的对齐方式 //图表区chartAreas chart1.ChartAreas[0].BackColor = Color.White;//chartAreas背景颜色 chart1.ChartAreas[0].BorderColor = Color.Red;//chartAreas边框颜色 chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;//chartAreas背景渐变,不使用 //AxisX表示图表的主x轴; chart1.ChartAreas[0].AxisX.LineColor = Color.Red; //线条颜色 chart1.ChartAreas[0].AxisX.Interval = 0.5;//设置x轴的间隔 chart1.ChartAreas[0].AxisX.Minimum = 0; chart1.ChartAreas[0].AxisX.Maximum = 25;//Y轴坐标固定,不会随绑定的数据而变 chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置X轴标签间距,如果不设置默认为x轴的间隔 chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false; chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微软雅黑", 13f, FontStyle.Regular); //标签字体 //设置x轴标题的字体样式和颜色 chart1.ChartAreas[0].AxisX.Title = "圆周位置,mm"; chart1.ChartAreas[0].AxisX.TitleFont = new Font("微软雅黑", 15f, FontStyle.Regular);// 标题字体 chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue; //轴标题颜色 chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;//轴标题文本方向 chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;//轴标题对齐方式 //X轴网格线 chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; //启用网格刻度线,一排竖线 //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d"); //线条颜色 //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Yellow; //y轴 chart1.ChartAreas[0].AxisY.LineColor = Color.Red; //线条颜色 chart1.ChartAreas[0].AxisY.Interval = 0.05;//设置Y轴的间隔 chart1.ChartAreas[0].AxisY.Minimum = 5;//Y轴坐标固定,不会随绑定的数据而变 chart1.ChartAreas[0].AxisY.Maximum = 6.35;//Y轴坐标固定,不会随绑定的数据而变 chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 0.05;//设置X轴标签间距,如果不设置默认为x轴的间隔 //Y坐标轴标题 chart1.ChartAreas[0].AxisY.Title = "圆周半径,mm"; //轴标题 chart1.ChartAreas[0].AxisY.TitleFont = new Font("微软雅黑", 15f, FontStyle.Regular); //标题字体 chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue; //轴标题颜色 chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270; //标题文本方向 chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far; //y轴标签样式 chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black; //标签颜色 chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微软雅黑", 13f, FontStyle.Regular); //标签字体 //Y轴网格线条 chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//一排横线 //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow; //#VAL为y轴的值,#VALX为x轴数据 //chart1.Series[0].Label = "hello";//数据点标签文本 //chart1.Series[0].Label = "#VAL";//数据点标签为其对于的y值 //chart1.Series[0].LabelBackColor = Color.Blue; //数据点标签背景色 //chart1.Series[0].LabelForeColor = Color.White; //数据点标签颜色 //chart1.Series[0].Color = Color.Red; //数据点颜色,数据点之间曲线的颜色 //chart1.Series[0].BorderWidth = 3;//数据点边框宽度,曲线的宽度 //chart1.Series[0].ToolTip = "#VALX:#VAL";//鼠标移动到对应点显示数值 元素的工具提示 chart1.Series[0].ChartType = SeriesChartType.Spline; //图表类型(折线) 绘制该序列的图表类型 Legend legend = new Legend("波形显示");//初始化具有指定的图例名称 legend.Title = "legendTitle"; //图例标题文本 chart1.Series[0].LegendText = legend.Name; //图例中项的文本 chart1.Legends.Add(legend); chart1.Legends[0].Position.Auto = false; //图例矩形位置 - 元素自动定位标志 //绑定数据 //数据绑定到指定数据源的第一列的x值和y值的集合的数据点 chart1.Series[0].Color = Color.Black; chart1.Series[0].Points.DataBindXY(x, y); } }} 关于"C#怎么根据excel数据绘制坐标图"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"C#怎么根据excel数据绘制坐标图"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
数据
颜色
标题
标签
字体
文本
图表
微软
C#
标图
图例
坐标
线条
背景
知识
网格
位置
元素
内容
圆周
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
咖啡网
广州万易互联网科技有限公司
金融科技还是互联网
互联网扶贫科技
齐家互联网科技有限公司
互联网金融科技委员会
江苏南京365互联网科技
互联网之后的科技是什么
苏州五指互联网络科技有限
知域互联网科技有限公司大骗子
恒生互联网科技指数跳水
深圳育鹏互联网科技有限公司招聘
四川慕妮娅互联网科技有限公司
互联网科技实习生岗位
广州富喜互联网络科技
中国互联网公司的科技投入
米泉互联网科技专业怎么样
承德科技产业互联网平台
上海旺乐尼互联网科技有限公司
银川新华互联网科技学校招聘
伟宏互联网科技
互联网佳科技有限公司
极限挑战互联网科技
天下捷融互联网科技
四川狮子座互联网科技
海南科技产业互联网平台
现代科技和互联网有关系吗
华夏恒生互联网科技013171
张家口科技产业互联网平台
科技互联网企业在行动