Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14548 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > How to Edit Chart Sheet in Excel with C#, VB.NET ...
Lacy

How to Edit Chart Sheet in Excel with C#, VB.NET

 Blog author: Lacy | Posted on: 5/30/2012 | Category: C# Blogs | Views: 1469 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Introduction
An excel chart can be embedded in a worksheet or placed in a separate Chart sheet. People can choose from a variety of chart types, including column, pie, line, bar, area and scatter on the Insert tab. It is not an easy task for people to create a new excel chart when we only need to change some parts of data, but it is indeed not a hard thing to edit an excel chart sheet. Below is a chart sheet of which the old numbers have been cleared and we need to insert the new information in it. This text considers a quick and easy way to edit chart sheet in Excel with C#, VB.NET. 
Example
                         

The picture above shows an excel chart sheet and we need to insert the following new data in it. 

       Country           Jun              Aug
       Cuba                $6,000        $4,000
       Mexico              $8,000        $7,000
       France              $9,000        $2,000
       German            $8,500        $5,000

Note: Please make sure that Spire.XLS and Visual Studio are installed in your computer. 

How to edit chart sheet in excel with C#, VB.NET?
For Spire.XLS users, you only need to follow the four steps below to finish editing the excel chart sheet with C#, VB.NET.

Step1: Create Edit Chart Sheet project and add references.

Create the Edit Chart Sheet project in Visual Studio, then, add Spire. XLS dll and System.Drawing as references. 

Step 2: Add the following namespace to the top of the file.

C#
using Spire.Xls;
VB,NET
Import Spire.XLS
Step 3: Add the following codes to the method.

C#
private void button1_Click(object sender, EventArgs e)
        {
            Workbook workbook = new Workbook();
            //Initialize worksheet
            workbook.LoadFromFile("../../../../../../Data/EditChartSample.xls", true);
            Worksheet sheet = workbook.Worksheets[0];
            //Writes chart data
            CreateChartData(sheet);
            workbook.SaveToFile("Sample.xls");
            ExcelDocViewer(workbook.FileName);
        }
        private void CreateChartData(Worksheet sheet)
        {
            //Jun
            sheet.Range["F6"].NumberValue = 6000;
            sheet.Range["F7"].NumberValue = 8000;
            sheet.Range["F8"].NumberValue = 9000;
            sheet.Range["F9"].NumberValue = 8500;
            //Aug
            sheet.Range["G6"].NumberValue = 4000;
            sheet.Range["G7"].NumberValue = 7000;
            sheet.Range["G8"].NumberValue = 2000;
            sheet.Range["G9"].NumberValue = 5000;
            sheet.Range["F6:F9"].Style.NumberFormat = "\"$\"#,##0";
            sheet.Range["G6:G9"].Style.NumberFormat = "\"$\"#,##0";
        }
        private void ExcelDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

VB.NET
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click
        Dim workbook As Workbook = New Workbook()
        'Initialize worksheet
        workbook.LoadFromFile("../../../../.././Dat/EditChartSample.xls",True)
        Dim sheet As Worksheet = workbook.Worksheets(0)
        'Writes chart data
        CreateChartData(sheet)
        workbook.SaveToFile("Sample.xls")
        ExcelDocViewer(workbook.FileName)
End Sub
Private Sub CreateChartData(ByVal sheet As Worksheet)
        'Jun
        sheet.Range("F6").NumberValue = 6000
        sheet.Range("F7").NumberValue = 8000
        sheet.Range("F8").NumberValue = 9000
        sheet.Range("F9").NumberValue = 8500
        'Aug
        sheet.Range("G6").NumberValue = 4000
        sheet.Range("G7").NumberValue = 7000
        sheet.Range("G8").NumberValue = 2000
        sheet.Range("G9").NumberValue = 5000
        sheet.Range("F6:F9").Style.NumberFormat = """$""#,##0"
        sheet.Range("G6:G9").Style.NumberFormat = """$""#,##0"
End Sub
Private Sub ExcelDocViewer(ByVal fileName As String)
        Try
               System.Diagnostics.Process.Start(fileName)
        Catch
        End Try
End Sub

Note:If you download the Spire.XLS, you can find the "EditChartSample.xls" in Spire. XLS profile-Demos-Data- EditChartSample.xls.

Step 4: Run the project Edit Chart Sheet.
Press F5 to run the project, you can see picture 4.1:

                              
                                                                        4.1

More About Spire. XLS

Spire. XLS, as a professional Excel component, allows its users to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. 



Found interesting? Add this to:


About Keating Arly

Experience:1 year(s)
Home page:http://www.e-iceblue.com
Member since:Tuesday, March 27, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 4:24:07 AM