我们正在获取System.Runtime.InteropServices.COMException:'来自HRESULT的异常:0x800AC472'

时间:2019-06-28 10:14:06

标签: c#-4.0

有人可以帮我吗。问题发生在运行时合并方法。
我已经使用DLL Microsoft.Office.Interop.Excel。 合并2个单元格时,我们遇到了这个问题。

在控制台应用程序中,它运行良好,但是当我放入WebApI控制器时,它将引发错误。

using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;   

     public void ExcelSheet()
                {
                    Application excelApp = null;
                    Workbooks workBooks = null;
                    Workbook workBook = null;
                    Sheets sheets = null;
                    Worksheet workSheet = null;
                    Range tRange = null;
                    try
                    {

                        // Start Excel and get Application object. 
                        excelApp = new Application();
                        workBooks = excelApp.Workbooks;
                        workBook = workBooks.Add();
                        sheets = workBook.Sheets;
                        workSheet = sheets.Add();

                        excelApp.Visible = true;
                        excelApp.DisplayAlerts = false;
                        workBook.Author = "Fullaname";

                        workSheet.Name = "Managers";

                        workSheet.Cells[1, "A"] = "Portfolio";
                        workSheet.Cells[1, "B"] = "Manager";

                        workSheet.Cells[1, "C"] = "Total ;
                        workSheet.Cells[1, "D"] = "Incidents";
                        //workSheet.Range["D1:E1"].Merge();
                        workSheet.get_Range("D1:E1").Merge();
                        workSheet.Cells[2, "D"] = "HKI";
                        workSheet.Cells[2, "E"] = "Low";

                        workSheet.Cells[1, "F"] = "Trend";
                        workSheet.Range["F1:I1"].Merge();
                        workSheet.Cells[2, "F"] = " Avg Medium";
                        workSheet.Cells[2, "G"] = " Avg Low";
                        workSheet.Cells[2, "H"] = "Medium";
                        workSheet.Cells[2, "I"] = "Low";
                        workSheet.get_Range("$A1", "$S1").WrapText = true;

                        //Wrap and Color
                        workSheet.get_Range("$A1", "$S1").Interior.ColorIndex = 6;
                        workSheet.get_Range("$A2", "$S2").WrapText = true;
                        workSheet.get_Range("$A2", "$S2").Interior.ColorIndex = 6;

                        workBook.Close();
                        excelApp.Quit();
                    }
                    finally
                    {
                        if (tRange != null) Marshal.ReleaseComObject(tRange);
                        if (workSheet != null) Marshal.ReleaseComObject(workSheet);
                        if (sheets != null) Marshal.ReleaseComObject(sheets);
                        if (workBook != null) Marshal.ReleaseComObject(workBook);
                        if (workBooks != null) Marshal.ReleaseComObject(workBooks);
                        if (excelApp != null) Marshal.ReleaseComObject(excelApp);
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }

0 个答案:

没有答案