我正在使用excel interop.dll v11.0與Excel實例交互的C#應用程序。 我使用下面的代碼圖表複製從Excel工作表到剪貼板:Excel異常HRESULT:來自ChartArea.Copy的0x800A03EC()
public Image ReadChart(Chart chartAccess) {
try {
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets[chartAccess.Sheet.Name];
Microsoft.Office.Interop.Excel.ChartObject chart = sheet.ChartObjects(chartAccess.Name);
chart.Chart.ChartArea.Copy(); // exception gets thrown here
return System.Windows.Forms.Clipboard.GetImage();
} catch (COMException) {
// display error dialog etc...
}
這工作得很好用Excel 2007。然而,由於切換到Excel 2013的功能ChartArea.Copy()導致以下COMExceptions被拋出:
Message: "Dimension not valid for chart type"
Stack Trace: System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
Microsoft.Office.Interop.Excel.ChartArea.Copy()
Message: "HRESULT: 0x800A03EC"
Stack Trace: System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
Microsoft.Office.Interop.Excel.ChartArea.Copy()
這僅與老.xls格式的Excel表格,XLSM/XLSX文件做工精細發生。 使用較新版本的interop.dll(v14.0和v15.0)沒有幫助。
任何幫助表示讚賞!
編輯:
我用以下解決方案解決了這個問題:
// ...
chart.Chart.activate();
chart.Chart.Export(filename, "PNG", false);
using (Stream reader = File.OpenRead(filename)) {
image = Image.fromStream(stream);
}
return image;
這是ERROR_INVALID_FLAGS,所以可能不會添加任何東西到第一個異常。由於它說維度,我建議看看ChartArea屬性的值,例如高度和寬度來查看這兩種情況下發生了什麼,並且作爲長時間拍攝,可能會嘗試使用.Copy()之前的ChartArea.Select()? – Rup 2015-04-01 17:12:31