Pergunta

I'm writing LigthSwitch application and one of the requirements is to export data to Excel. I have accomplished such task. It works perfectly when application is being run on localhost. However, when I upload application into the SharePoint site I get following error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) https://437aa483-68ef-4ae1-9269-b206f5beb418.o365apps.net/setExcelDocument.ashx

Here is the sample code(Just copy, reference excel.dll and 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";

        }
Foi útil?

Solução

Your code executes on the host system and when deployed to a SharePoint site that host system would need to have Excel installed and security access enabled to run it. That solution would not scale or work well as you would be trying to attempt multithreaded access from a web server thread.

You need to follow a different design pattern like working with OpenXml (on client or server) or interact with the Office365 API's as this is in the cloud.

API's for Excel Services on the Web

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top