这篇文章主要为大家详细介绍了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<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!
// }
/
织梦狗教程
本文标题为:C#根据excel数据绘制坐标图的方法


基础教程推荐
猜你喜欢
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity shader实现高斯模糊效果 2023-01-16
- C# 解析XML和反序列化的示例 2023-04-14