Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发布到服务器后无法显示/打开excel文档!它';在本地计算机上可以,但在服务器上不行_C#_Asp.net - Fatal编程技术网

C# 发布到服务器后无法显示/打开excel文档!它';在本地计算机上可以,但在服务器上不行

C# 发布到服务器后无法显示/打开excel文档!它';在本地计算机上可以,但在服务器上不行,c#,asp.net,C#,Asp.net,我想通过单击链接按钮显示excel文档模板。然而,在服务器端,我无法这样做(相反,它似乎只是刷新了该页面) 我该怎么做 public class CreateExcelDoc { public static Excel.Application app = null; public static Excel.Workbook workbook = null; public static Excel.Worksheet worksheet =

我想通过单击链接按钮显示excel文档模板。然而,在服务器端,我无法这样做(相反,它似乎只是刷新了该页面)

我该怎么做

 public class CreateExcelDoc
    {
        public static Excel.Application app = null;
        public static Excel.Workbook workbook = null;
        public static Excel.Worksheet worksheet = null;
        public static Excel.Range workSheet_range = null;

        public CreateExcelDoc()
        {
            createDoc();
        }

        public void createDoc()
        {
            try
            {
                app = new Excel.Application();
                app.Visible = true;
                workbook = app.Workbooks.Add(1);
                worksheet = (Excel.Worksheet)workbook.Sheets[1];

            }
            catch (Exception)
            {
                //MessageBox.Show("Error!");
            }
            finally
            {
            }
        }

        public void createHeaders(int row, int col, string htext, string cell1, string cell2, int mergeColumns, string b, bool font, int size, string fcolor)
        {
            object misValue = System.Reflection.Missing.Value;
            worksheet.Cells[row, col] = htext;
            workSheet_range = worksheet.get_Range(cell1, cell2);
            workSheet_range.Merge(mergeColumns);
            //workSheet_range.Validation.Add(Excel.XlDVType.xlValidateInputOnly, Excel.XlDVAlertStyle.xlValidAlertStop, Excel.XlFormatConditionOperator.xlBetween, misValue, misValue);
            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 "WHITE":
                    workSheet_range.Interior.Color = System.Drawing.Color.White.ToArgb();
                    break;
                case "ANWHITE":
                    workSheet_range.Interior.Color = System.Drawing.Color.AntiqueWhite.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 = font;
            workSheet_range.ColumnWidth = size;
            if (fcolor.Equals(""))
            {
                workSheet_range.Font.Color = System.Drawing.Color.White.ToArgb();
            }
            else
            {
                workSheet_range.Font.Color = System.Drawing.Color.Black.ToArgb();
            }
        }

        public void addData(int row, int col, string data, string cell1, string cell2, string format)
        {
            worksheet.Cells[row, col] = data;
            workSheet_range = worksheet.get_Range(cell1, cell2);
            workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
            workSheet_range.NumberFormat = format;
        }
    }



 protected void DownloadLinkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                #region Create excel format & show

                //create the excel template for the users
                CreateExcelDoc excell_app = new CreateExcelDoc();
                excell_app.createHeaders(1, 1, "a", "A1", "A1", 0, "YELLOW", true, 30, "");
                excell_app.createHeaders(1, 2, "b", "B1", "B1", 0, "YELLOW", true, 10, "");
                excell_app.createHeaders(1, 3, "c", "C1", "C1", 0, "YELLOW", true, 30, "");
                excell_app.createHeaders(1, 4, "d", "D1", "D1", 0, "YELLOW", true, 10, "");
                excell_app.createHeaders(1, 5, "e", "E1", "E1", 0, "YELLOW", true, 10, "");
                excell_app.createHeaders(1, 6, "f", "F1", "F1", 0, "YELLOW", true, 20, "");
                excell_app.createHeaders(1, 7, "g", "G1", "G1", 0, "ANWHITE", true, 20, "");
                excell_app.createHeaders(1, 8, "h", "H1", "H1", 0, "ANWHITE", true, 20, "");
                excell_app.createHeaders(1, 9, "i", "I1", "I1", 0, "ANWHITE", true, 20, "");
                excell_app.createHeaders(1, 10, "j", "J1", "J1", 0, "ANWHITE", true, 20, "");

            //add data into cells
            excell_app.addData(2, 1, "", "A2", "J501", "");

            object misValue = System.Reflection.Missing.Value;
            CreateExcelDoc.workbook = CreateExcelDoc.app.Workbooks.Add(misValue);

            CreateExcelDoc.workbook.Close(true, misValue, misValue);

            ////prevent stacking of worksheet
            releaseObject(CreateExcelDoc.worksheet);
            releaseObject(CreateExcelDoc.workbook);
            releaseObject(CreateExcelDoc.app);

            #endregion
        }
        catch
        {
        }
    }

您需要在部署计算机上安装Office interOp,但我认为这不是一个好的解决方案。请看和。

Office互操作程序集从未打算用于服务器端开发。您可能需要考虑使用OpenXML来代替。以下知识库文章提供了详细的解释:

正如@AVD所提到的,您确实需要在服务器上安装Office主互操作程序集。此外,根据您尝试使用的MS Office版本,64位Office互操作程序集可能不可用