不受控制的异常-System.Runtime.InteropServices.COMException:不受控制的异常:HRESULT:0X800A03EC

时间:2019-03-26 04:25:47

标签: c# image visual-studio export-to-excel

我正在尝试导出一个包含位图图像的datagridview,但这标志着我一个不受控制的异常。 “ System.Runtime.InteropServices.COMException:HRESULT:0x800A03EC”

我有此代码:

void exportexcel(DataGridView dgwObservaciones)
        {
            Excel.Application oXL; //Se va usar Excel e interactuar con la aplicación
            Excel._Workbook oWB; //Crea el libro
            Excel._Worksheet oSheet;

            try
            {
                //Inicia Excel
                oXL = new Excel.Application();
                oXL.Visible = true;
                //Obtiene el libro y la hoja con la que se va a trabajar
                oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;

                //Ancho de columnas
                oSheet.get_Range("A1").ColumnWidth = 30;
                oSheet.get_Range("B1", "D1").ColumnWidth = 50;
                //Empieza llenado
                oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.get_Item(1);

                //Recorremos el DataGridView rellenando la hoja de trabajo
                for (int i = 0; i < dgwObservaciones.Rows.Count; i++)
                {
                    for (int j = 0; j < dgwObservaciones.Columns.Count; j++)
                    {
                        DataGridViewCell cell = dgwObservaciones[j, i];
                        if (cell.Value.GetType() == typeof(Bitmap))
                        {
                            // You have to get original bitmap path here
                            string imagString = "bitmap1.bmp";
                            Excel.Range oRange = (Excel.Range)oSheet.Cells[i + 1, j + 1];
                            float Left = (float)((double)oRange.Left);
                            float Top = (float)((double)oRange.Top);
                            const float ImageSize = 32;
                            oSheet.Shapes.AddPicture(imagString, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
                            oRange.RowHeight = ImageSize + 2;
                        }
                        else
                        {
                            oSheet.Cells[i + 1, j + 1] = cell.Value; //the error is in this line
                        }
                    }
                }
            }
      catch (Exception theException)
      {
          String errorMessage;
        errorMessage = "Error: ";
          errorMessage = String.Concat(errorMessage, theException.Message);
          errorMessage = String.Concat(errorMessage, " Line: ");
          errorMessage = String.Concat(errorMessage, theException.Source);
          MessageBox.Show(errorMessage, "Error");
      }
}

enter image description here 请帮帮我!

0 个答案:

没有答案