Sharing violation error while exporting an excel chart to an image
Hi all..i really need help here..
I build a windows application that can display an excel chart in a picture box, where the data to display the chart is coming from the sql db. The chart initially displayed in the excel file and i need to export the chart as an bmp image to the C://path, so that i can display it in the picture box of the windows form. This situation was successfully achieved only if try to display the chart (that already converted as bmp & displayed in the picture box) by clicking a button at the 1st time. The problem started when i try to display the chart again at the 2nd time. It throws the following exception :
A share violation has occurred. (Exception from HRESULT: 0x80030020 (STG_E_SHAREVIOLATION))
I think this problem appear when i try to export the chart as the image at the same path while its cannot overwrite that path.
Below is the snipped codes that i used in my program:
Excel.Range xRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type. Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 300);
Excel.Chart xChart = myChart.Chart;
//display the chart
xRange = xlWorkSheet.get_Range("A1", "M3");
xChart.SetSourceData(xRange, misValue);
//export chart as picture file
xChart.Export(@"C:\excel_chart_export.bmp", "BMP", misValue);
//load picture to picturebox
pictureBox1.Image = new Bitmap(@"C:\excel_chart_export.bmp");
The error was highlighted on the export chart as picture file part..Can anyone please help me to overcome this..i really appreciate ur help..tq a lot..
|