Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create graph in Excel with C#

Status
Not open for further replies.

volpebianca

Programmer
Oct 19, 2004
3
0
0
IT
Good evening,
i have a problem: how can I create a graph in excel via code? I have some documents in which there are some values in two different coloums. Now i want to create a graph, in the same document excel, about these values. I want to write the title and names of x and y in the graph. Thank you thousand.
 
There are two problems you have to solve to do this:

1) Have your C# application talk to Excel. Once you can populate a workbook with some data, you'll have this solved. Do a search here and in the VB.NET forum.

2) Tell Excel to make a chart from your data. This is simply understanding the Excel object model. Since this is the same whether you're calling it from C# or VB6 or any other COM-aware language, you can use any Excel reference guide to solve this.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you very much, but i know that you can call Excel when you add a reference to excel in your project and, for example you can create an exce3l application with the Excel.Application object, but i don't know what is the command for creating a chart. Do you know what is? thank you very much.
 
Hi
---
You maby can use this, what I found today
Code:
[u][URL unfurl="true"]http://msdn.microsoft.com/vstudio/office/default.aspx?pull=/library/en-us/odc_vsto2003_ta/html/odc_vstexlchrt.asp[/URL]
[/u]
 
volpebianca:

I've done a lot with automating Excel/Excel graphs through C#. Let me know if you need any help, I'd be glad to help.

-Smitty
 
Hello all,

I would like to send data to excel in IE. I followed different scripts that posted on web, but I could not make any of them work. I would like to use either C# or javascript.

Can you show me a simple C# script that will display data in excel in the IE?

Thanks
 
Follow either of the links in the posts above for sample code.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chiph,

The first link is for windows application,

the second link I am not sure, but I could not follow the below steps:

In the Project Types pane, expand Microsoft Office System Projects, and then select Visual Basic Projects or Visual C# Projects.
From the Templates pane of the New Project dialog box, select Excel Workbook.

Thanks
 
OK, I didn't know that you want to show excel inside of internet explorer. This will require your users to all have excel installed on their machines. If that's OK, then all you have to do is put a link to the .xls file in your ASP.NET file.

If you are creating dynamic excel content, then you would have to have excel installed on the server, and you would use the code above to populate a spreadsheet, and then save it to a temp file. You would then point a link to the temp file.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
This is a small Excel Example that I've created. The user must have Excel installed on their computer. You, the developer, must have the Excel Com object 11.0 included as a reference. The code should be fairly simple to follow.

Enjoy!

using System.Reflection;

Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;

private void btnProcess_Click(object sender, system.EventArgs e)
{
try
{
oXL = new Excel.Application();
oXL.Visible = true;

oWB = ( Excel._Workbook ) ( oXL.Workbooks.Add( Missing.Value ) );
oSheet = ( Excel._Worksheet ) oWB.ActiveSheet;

oSheet.Cells[1,1] = "Row 1 Column 1";
oSheet.Cells[1,2] = "Row 1 Column 2";

oSheet.Cells[2,1] = "Row 2 Column 1";
oSheet.Cells[2,1] = "Row 2 Column 2";

// Add a worksheet
Excel.Worksheet newWorkSheet;
newWorkSheet = ( Excel.Worksheet ) oWB.Worksheets.Add( Type.Missing, Type.Missing, Type.Missing, Type.Missing );

// Select a worksheet
( ( Excel.Worksheet ) oXL.ActiveWorkbook.Sheets[3] ).Select( Type.Missing ); // The third worksheet will get focus.
oSheet = ( Excel._Worksheet ) oWB.ActiveSheet;

// Name the WorkSheet
oSheet.Name = "This is Worksheet 3";

// Add data to the Worksheet
oSheet.Cells[1,1] = "Worksheet 3 Row 1 Column 1";
oSheet.Cells[1,2] = "Worksheet 3 Row 1 Column 2";

oSheet.Cells[2,1] = "Worksheet 3 Row 2 Column 1";
oSheet.Cells[2,2] = "Worksheet 3 Row 2 Column 2";

// Save file "Will prompt for an overwrite condition" so you should handle this progmaticly
oWB.SaveAs(@"C:\Excel Demo.XLS", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );

// Close down the workbooks
oWB.Close( false, Type.Missing, Type.Missing );

// Close Down Excel
oXL.Quit();
}
catch ( Exception theException )
{
MessageBox.Show( "Error " + theException );
}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top