Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel VB.NET Chart
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("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);
 
          MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
            
        }
 
        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();
            }
        }
 

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))"
Source="Microsoft.Office.Interop.Excel"
ErrorCode=-2147287008
StackTrace:
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()
InnerException:

 

Edit: Moved OP's answer here
There is nothing like Interlop.Microsoft.office.Core.dll
but the version of Interop.Microsoft.Office.Interop.Excel.dll is 12
Posted 14-Apr-12 21:13pm
Edited 15-Apr-12 1:37am
Wes Aday94.3K
v3
Comments
Nelek at 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
good
Please Sign up or sign in to vote.

Solution 1

Please cross check the version of the
 
Interop.Microsoft.Office.Interop.Excel.dll
Interop.Microsoft.Office.Core.dll
 
these two dll s with your refence.
  Permalink  
Comments
Nelek at 15-Apr-12 6:06am
   
The OP answered to you, I added the info to the question
ankitpsaraogi at 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
good
Please Sign up or sign in to vote.

Solution 3

int flag=0;
if(flag==0)
{
chartPage.Export(@"D:\excel_chart_export.bmp", "BMP", misValue);
            pictureBox1.Image = new Bitmap(@"D:\excel_chart_export.bmp");
flag=1;
}
else
{
chartPage.Export(@"D:\excel_chart_export2.bmp", "BMP", misValue);
            pictureBox1.Image = new Bitmap(@"D:\excel_chart_export2.bmp");
flag=0;
}
Here the file which keeps the bmp file open can hold only one file at a time. Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 16 Apr 2012
Copyright © CodeProject, 1999-2014
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