尝试Excel.Shapes.AddPicture时出现错误0x800A03EC

时间:2018-04-17 09:12:45

标签: c# excel

我正在尝试制作一个c#windows窗体程序,用于创建包含数据和图像的excel文件。

在我的电脑上使用时,效果很好。没有错误。但是当我把它交给同事时, 他们在使用Excel.Shapes.AddPicture.

时遇到错误

异常文本

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Microsoft.Office.Interop.Excel.Shapes.AddPicture(String Filename, MsoTriState LinkToFile, MsoTriState SaveWithDocument, Single Left, Single Top, Single Width, Single Height)
   at ItemTableValidator_2.Form1.insertItemImage()
   at ItemTableValidator_2.Form1.BTN_START_Click(Object sender, EventArgs e)

我使用的是Microsoft.Office.Interop.Excel。和代码在下面。

    private void insertItemImage()
    {

        Excel.Application targetExcel = new Excel.Application();
        Excel.Workbook targetWorkbook = targetExcel.Workbooks.Open(targetFileName);
        Excel.Worksheet targetWorksheet = targetWorkbook.Worksheets["Item_Chart"];

        int filenameIdx = 5;
        int imageIdx = 4;
        string[] resourceDir = Directory.GetFiles(editDirectory + "\\Icon","*.png");
        List<string> resourceList = new List<string>();
        List<string> nameList = new List<string>();

        foreach (string dir in resourceDir)
        {
            resourceList.Add(Path.GetFileName(dir));
        }


        targetWorksheet.Rows.RowHeight = 100.0f;
        targetWorksheet.Rows[1].RowHeight = 10.0f;

        for (int idx = 0; idx < resourceList.Count; idx++)
        {
            for (int rowIdx = 2; rowIdx <= targetWorksheet.UsedRange.Rows.Count; rowIdx++)
            {
                string tmpString = targetWorksheet.Cells[rowIdx, filenameIdx].Value.ToString() + ".png";

                if ( tmpString == resourceList[idx])
                {
                    Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)targetWorksheet.Cells[rowIdx, imageIdx];

                    float Left = (float)((double)oRange.Left);
                    float Top = (float)((double)oRange.Top);
                    targetWorksheet.Shapes.AddPicture(resourceDir[idx], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, 80, 80);
                }
            }

        }
        targetWorkbook.Save();
        targetWorkbook.Close(true);
        targetExcel.Quit();

        System.Runtime.InteropServices.Marshal.ReleaseComObject(targetExcel);
    }

谢谢。

0 个答案:

没有答案