使用c#从Excel创建图表 [英] Creating Diagrams from Excel with c#
问题描述
Hello Guys!
Hello Guys!
我需要从excel表创建一个图表。用户将表上传到我的程序。 Excel文件是default-template。程序采用第2张和第3张,并将每一行表示为一个矩形。矩形之间没有连接。
I need to create a diagram from an excel table. User uploads the table to the my program. Excel file is default-template. Program takes the 2nd and 3rd sheet and represent every row into a rectangle. There is no connection between rectangles.
现在我可以上传我的文件并在我的程序中创建excel查询但是我问你怎么能在这里想象它们是我的代码!
Now i'm able to upload my file and create excel queries in my program but I'm asking you that how can I visualize them here is my code!
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string dosya_yolu;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "Upload";
button2.Text = "Run!";
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
file_path = openFileDialog1.FileName;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + file_path + "; Extended Properties = Excel 12.0");
try
{
conn.Open();
string query= "select * from [Sheet2$]";
string query2= "select [F1],[F3],[F5],[F6],[F9],[F12] from [Sheet3$]";
OleDbDataAdapter data_Adapter = new OleDbDataAdapter(sorgu, conn);
OleDbDataAdapter data_Adapter2 = new OleDbDataAdapter(sorgu2, conn);
conn.Close();
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
data_Adapter.Fill(dt);
data_Adapter2.Fill(dt2);
dataGridView1.DataSource = dt;
dataGridView2.DataSource = dt2;
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
例如,在第二张表格的excel文件中,我有3行和4列。我想将每一行表示为一个矩形,并从特定单元格中获取其名称属性。在矩形内部,将再次为该行设置列数据,并将
指定的单元格作为我的标题。
For example in my excel file in the 2nd sheet I have 3 rows and four columns. I want to represent every row as a rectangle and get their name properties from a specific cell. And inside the rectangles there will be column datas for that row again with the specified cell their headlines by me.
我可以将System.drawings用于此项目或gojs或者其他什么否?
Can I use System.drawings for this project or gojs or something else ?
我希望我能说清楚。
如果您有任何疑问,请询问me.Thank you ..
If you have any questions please ask me.Thank you..
推荐答案
您可以使用本机Excel库绘制专业图表:例如,Microsoft Excel 12.0对象库:
You can use the native Excel library to draw professional graphs : Microsoft Excel 12.0 Object Library for example:
using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range chartRange ;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
//export chart as picture file
chartPage.Export(@"C:\excel_chart_export.bmp", "BMP", misValue);
//load picture to picturebox
pictureBox1.Image = new Bitmap(@"C:\excel_chart_export.bmp");
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
祝你好运。
Good luck.
这篇关于使用c#从Excel创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!