Excel 500(内部服务器错误)

时间:2014-03-19 10:08:01

标签: c# excel sharepoint visual-studio-lightswitch

我正在编写LigthSwitch应用程序,其中一个要求是将数据导出到Excel。我完成了这样的任务。当应用程序在localhost上运行时,它可以正常工作。但是,当我将应用程序上载到SharePoint站点时,我收到以下错误:

无法加载资源:服务器响应状态为500(内部服务器错误)https://437aa483-68ef-4ae1-9269-b206f5beb418.o365apps.net/setExcelDocument.ashx

以下是示例代码(Just copy,reference excel.dll和add trigger):

        private Microsoft.Office.Interop.Excel.Application app = null;
        private Microsoft.Office.Interop.Excel.Workbook workbook = null;
        private Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
        private Microsoft.Office.Interop.Excel.Range workSheet_range = null;

        private void createOpportunity()
        {
            try
            {
                app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = true;
                workbook = app.Workbooks.Add(1);
                worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }

            worksheet.Cells[5,1] = "hello World";
            workSheet_range = worksheet.get_Range("B5", "Q5");
            workSheet_range.Merge(14);
            string b = "YELLOW";
            switch (b)
            {
                case "YELLOW":
                    workSheet_range.Interior.Color = System.Drawing.Color.Yellow.ToArgb();
                    break;
                case "GRAY":
                    workSheet_range.Interior.Color = System.Drawing.Color.Gray.ToArgb();
                    break;
                case "GAINSBORO":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.Gainsboro.ToArgb();
                    break;
                case "Turquoise":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.Turquoise.ToArgb();
                    break;
                case "PeachPuff":
                    workSheet_range.Interior.Color =
            System.Drawing.Color.PeachPuff.ToArgb();
                    break;
                default:
                    //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                    break;
            }

            workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
            workSheet_range.Font.Bold = true;
            workSheet_range.ColumnWidth = 14;
            string fcolor = "n";
            if (fcolor.Equals(""))
            {
                workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
            }
            else
            {
                workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
            }

            worksheet.Cells[7,2] = "hi";
            workSheet_range = worksheet.get_Range("B7", "C7");
            workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
            workSheet_range.NumberFormat = "#,##0";

        }

1 个答案:

答案 0 :(得分:2)

您的代码在主机系统上执行,当部署到SharePoint站点时,主机系统需要安装Excel并启用安全访问才能运行它。该解决方案无法扩展或运行良好,因为您尝试从Web服务器线程尝试多线程访问。

您需要遵循不同的设计模式,例如使用OpenXml(在客户端或服务器上)或与Office 365 API进行交互,因为它位于云中。

API's for Excel Services on the Web