C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的方法
更新時(shí)間:2022年02月17日 09:40:48 作者:神奇小白
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的具體代碼,供大家參考,具體內(nèi)容如下
效果如下圖

界面

代碼
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軸數(shù)據(jù)
? ? ? ? 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<Double> xList = new List<Double>();
? ? ? ? List<Double> yList = new List<Double>();
? ? ? ? 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系統(tǒng)定義的顏色
? ? ? ? ? ? //chart1.BackColor = Color.White;
? ? ? ? ? ? //圖表標(biāo)題,
? ? ? ? ? ? chart1.Titles.Add("測試數(shù)據(jù)"); //添加title到titleCollection集合的末尾
? ? ? ? ? ? chart1.Titles[0].ForeColor = Color.DarkBlue;//設(shè)置title的文本顏色
? ? ? ? ? ? chart1.Titles[0].Font = new Font("微軟雅黑", 15f, FontStyle.Regular);//設(shè)置title的字體
? ? ? ? ? ? chart1.Titles[0].Alignment = ContentAlignment.TopCenter;//設(shè)置title的對齊方式
? ? ? ? ? ? //圖表區(qū)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;//設(shè)置x軸的間隔
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.Minimum = 0;
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.Maximum = 25;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//設(shè)置X軸標(biāo)簽間距,如果不設(shè)置默認(rèn)為x軸的間隔
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false;
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 13f, FontStyle.Regular); //標(biāo)簽字體
? ? ? ? ? ? //設(shè)置x軸標(biāo)題的字體樣式和顏色
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.Title = "圓周位置,mm";
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 15f, FontStyle.Regular);// 標(biāo)題字體
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue; //軸標(biāo)題顏色
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;//軸標(biāo)題文本方向
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;//軸標(biāo)題對齊方式
? ? ? ? ? ? //X軸網(wǎng)格線
? ? ? ? ? ? chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; //啟用網(wǎng)格刻度線,一排豎線
? ? ? ? ? ? //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;//設(shè)置Y軸的間隔
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Minimum = 5;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Maximum = 6.35;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 0.05;//設(shè)置X軸標(biāo)簽間距,如果不設(shè)置默認(rèn)為x軸的間隔
? ? ? ? ? ? //Y坐標(biāo)軸標(biāo)題
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Title = "圓周半徑,mm"; //軸標(biāo)題
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 15f, FontStyle.Regular); //標(biāo)題字體
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue; //軸標(biāo)題顏色
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270; //標(biāo)題文本方向
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;
? ? ? ? ? ? //y軸標(biāo)簽樣式
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black; //標(biāo)簽顏色
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 13f, FontStyle.Regular); //標(biāo)簽字體
? ? ? ? ? ? //Y軸網(wǎng)格線條
? ? ? ? ? ? chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//一排橫線
? ? ? ? ? ? //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow;
? ? ? ? ? ? //#VAL為y軸的值,#VALX為x軸數(shù)據(jù)
? ? ? ? ? ? //chart1.Series[0].Label = "hello";//數(shù)據(jù)點(diǎn)標(biāo)簽文本 ?
? ? ? ? ? ? //chart1.Series[0].Label = "#VAL";//數(shù)據(jù)點(diǎn)標(biāo)簽為其對于的y值
? ? ? ? ? ? //chart1.Series[0].LabelBackColor = Color.Blue; //數(shù)據(jù)點(diǎn)標(biāo)簽背景色
? ? ? ? ? ? //chart1.Series[0].LabelForeColor = Color.White; //數(shù)據(jù)點(diǎn)標(biāo)簽顏色
? ? ? ? ? ? //chart1.Series[0].Color = Color.Red; //數(shù)據(jù)點(diǎn)顏色,數(shù)據(jù)點(diǎn)之間曲線的顏色
? ? ? ? ? ? //chart1.Series[0].BorderWidth = 3;//數(shù)據(jù)點(diǎn)邊框?qū)挾?,曲線的寬度
? ? ? ? ? ? //chart1.Series[0].ToolTip = "#VALX:#VAL";//鼠標(biāo)移動到對應(yīng)點(diǎn)顯示數(shù)值 元素的工具提示
? ? ? ? ? ? chart1.Series[0].ChartType = SeriesChartType.Spline; //圖表類型(折線) 繪制該序列的圖表類型
? ? ? ? ? ? Legend legend = new Legend("波形顯示");//初始化具有指定的圖例名稱
? ? ? ? ? ? legend.Title = "legendTitle"; //圖例標(biāo)題文本
? ? ? ? ? ? chart1.Series[0].LegendText = legend.Name; //圖例中項(xiàng)的文本
? ? ? ? ? ? chart1.Legends.Add(legend);
? ? ? ? ? ? chart1.Legends[0].Position.Auto = false; //圖例矩形位置 - 元素自動定位標(biāo)志
? ? ? ? ? ? //綁定數(shù)據(jù)
? ? ? ? ? ? //數(shù)據(jù)綁定到指定數(shù)據(jù)源的第一列的x值和y值的集合的數(shù)據(jù)點(diǎn)
? ? ? ? ? ? chart1.Series[0].Color = Color.Black;
? ? ? ? ? ? chart1.Series[0].Points.DataBindXY(x, y);
? ? ? ? }
? ? }
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Unity Shader實(shí)現(xiàn)水波紋效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)水波紋效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C#中LINQ to Objects查詢的實(shí)現(xiàn)
LINQ to Objects是LINQ技術(shù)在C#中的一種應(yīng)用,它專門用于對內(nèi)存中的對象集合進(jìn)行查詢和操作,本文就詳細(xì)的介紹C#中LINQ to Objects查詢的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
C#添加Windows服務(wù) 定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
C#訪問SQL Server數(shù)據(jù)庫的實(shí)現(xiàn)方法
這篇文章主要介紹了C#訪問SQL Server數(shù)據(jù)庫的實(shí)現(xiàn)方法,以實(shí)例形式簡單分析了C#連接、查詢SQL Server數(shù)據(jù)庫的具體技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法
工作和生活中經(jīng)??梢钥吹揭恍┏绦蛟?寫代碼的時(shí)候只關(guān)注代碼的邏輯性,而不考慮運(yùn)行效率,其實(shí)這對大多數(shù)程序猿來說都是沒有問題的,不過作為一只有理想的CodeMonkey,我還是希望給大家分享一些性能優(yōu)化心得2017-08-08
C#使用FtpWebRequest與FtpWebResponse完成FTP操作
這篇文章介紹了C#使用FtpWebRequest與FtpWebResponse完成FTP操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

