Click here to Skip to main content
13,046,356 members (70,127 online)
Rate this:
Please Sign up or sign in to vote.
private void button1_Click(object sender, EventArgs e)
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            //add data 
            xlWorkSheet.Cells[1, 1] = "";
            xlWorkSheet.Cells[1, 2] = "Student1";
            xlWorkSheet.Cells[1, 3] = "Student2";
            xlWorkSheet.Cells[1, 4] = "Student3";
            xlWorkSheet.Cells[2, 1] = "Term1";
            xlWorkSheet.Cells[2, 2] = textBox1.Text ;
            xlWorkSheet.Cells[2, 3] = "65";
            xlWorkSheet.Cells[2, 4] = "45";
            xlWorkSheet.Cells[3, 1] = "Term2";
            xlWorkSheet.Cells[3, 2] = "78";
            xlWorkSheet.Cells[3, 3] = "72";
            xlWorkSheet.Cells[3, 4] = "60";
            xlWorkSheet.Cells[4, 1] = "Term3";
            xlWorkSheet.Cells[4, 2] = "82";
            xlWorkSheet.Cells[4, 3] = "80";
            xlWorkSheet.Cells[4, 4] = "65";
            xlWorkSheet.Cells[5, 1] = "Term4";
            xlWorkSheet.Cells[5, 2] = "75";
            xlWorkSheet.Cells[5, 3] = "82";
            xlWorkSheet.Cells[5, 4] = "68";
            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;
            chartPage.Export(@"D:\excel_chart_export.bmp", "BMP", misValue);
            pictureBox1.Image = new Bitmap(@"D:\excel_chart_export.bmp");
           xlWorkBook.SaveAs("", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
          MessageBox.Show("Excel file created , you can find the file c:\\");
        private void releaseObject(object obj)
                obj = null;
            catch (Exception ex)
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

When I press Button1 twice I get the error
A share violation has occurred. (Exception from HRESULT: 0x80030020 (STG_E_SHAREVIOLATION))

Please help me solve this issue. I want to change the value in the textbox and generate charts accordingly.

The Exception which I copied to clipboard
System.Runtime.InteropServices.COMException was unhandled
Message="A share violation has occurred. (Exception from HRESULT: 0x80030020 (STG_E_SHAREVIOLATION))"
at Microsoft.Office.Interop.Excel._Chart.Export(String Filename, Object FilterName, Object Interactive)
at ExcelChartTest.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Ankit\Documents\Visual Studio 2008\Projects\ExcelChartTest\ExcelChartTest\Form1.cs:line 65
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms. UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at ExcelChartTest.Program.Main() in C:\Users\Ankit\Documents\Visual Studio 2008\Projects\ExcelChartTest\ExcelChartTest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Edit: Moved OP's answer here
There is nothing like
but the version of Interop.Microsoft.Office.Interop.Excel.dll is 12
Posted 14-Apr-12 20:13pm
Updated 15-Apr-12 0:37am
Wes Aday104.2K
Nelek 15-Apr-12 6:04am
If you need to add information relevant to your question, you can use the "improve question" to add it to the original message or the "have a question or comment" button to directly speak with the person who answered you. But please don't add answers for that.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please cross check the version of the


these two dll s with your refence.
Nelek 15-Apr-12 6:06am
The OP answered to you, I added the info to the question
ankitpsaraogi 15-Apr-12 12:28pm
Its 12.0 of Interop.Microsoft.Office.Interop.Excel.dll
Interop.Microsoft.Office.Core.dll is not there
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

int flag=0;
chartPage.Export(@"D:\excel_chart_export.bmp", "BMP", misValue);
            pictureBox1.Image = new Bitmap(@"D:\excel_chart_export.bmp");
chartPage.Export(@"D:\excel_chart_export2.bmp", "BMP", misValue);
            pictureBox1.Image = new Bitmap(@"D:\excel_chart_export2.bmp");

Here the file which keeps the bmp file open can hold only one file at a time. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100