简体   繁体   English

发布到服务器后无法显示/打开Excel文档! 在本地计算机上很好,但在服务器上没有

[英]Unable to display/open excel document after published to server! It's fine on local machine but not on server

I want to display the excel document template from a LinkButton click. 我想通过LinkBut​​ton单击显示excel文档模板。 However on the server side, I'm not able to do so (instead it just seems like refreshing that page). 但是,在服务器端,我无法做到这一点(相反,似乎只是刷新该页面)。

How do I do this? 我该怎么做呢?

 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
        {
        }
    }

You need to install Office interOp runtime at deployment machine but I think that is not good solution. 您需要在部署计算机上安装Office interOp 运行时 ,但是我认为这不是一个好的解决方案。 Take a look at How to create Excel file dynamically in ASP.NET and Reading Excel files from C# . 看一下如何在ASP.NET中动态创建Excel文件以及如何 从C#读取Excel文件

The Office Interop assemblies were never intended to be used for server-side development. Office Interop程序集从未打算用于服务器端开发。 You may want to consider using Open XML instead. 您可能需要考虑使用Open XML。 The following KB article provides a detailed explanation: Considerations for server-side Automation of Office 下面的知识库文章提供了详细的解释: Office的服务器端自动化的注意事项

As @AVD mentioned, you do need to have Office Primary Interop Assemblies installed on the server. 如@AVD所述,您确实需要在服务器上安装Office Primary Interop程序集。 Also, depending on the MS Office version you are trying to use, the 64bit Office Interop assemblies may not be available. 另外,根据您尝试使用的MS Office版本,64位Office Interop程序集可能不可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ajax自动完成CSS在iis和服务器上工作正常,但在本地计算机上工作不正常 - Ajax Auto complete Css works fine in iis and the server but not in local machine 服务器计算机上的程序集引用异常。 在本地运行良好 - Assembly reference exception on Server machine. Running fine on local DirectX 应用程序无法通过 Process.Start 在 Windows Server 2012 R2 上加载,通过 CMD 或在本地计算机上正常工作 - DirectX app unable to load through Process.Start on Windows Server 2012 R2, works fine through CMD or on local machine Excel Interop将文件保存在主机服务器而不是本地计算机上 - Excel Interop saving file on host server instead of local machine 将本地计算机上的excel文件导入SQL Server 2005 - Import excel file which is on local machine to SQL server 2005 Rotativa 在本地工作正常,但无法重定向服务器中的视图 - Rotativa locally works fine but it's unable to redirect the view in the server Visual Studio 在本地机器上运行,服务器的 IP - Visual Studio runs on local machine with server's IP 我的asmx服务在我的本地机器的iis上运行正常,即win10。 但不适用于Windows Server 2008 r2 - my asmx service works fine on my local machine's iis which is win10. but does not work on windows server 2008 r2 ViewContext.RouteData.Values [“action”]在服务器上为空...在本地计算机上正常工作 - ViewContext.RouteData.Values[“action”] is null on server… works fine on local machine Linq查询在本地计算机上运行良好,但在服务器上收到“连接已重置”错误 - Linq query runs fine on local machine, but get “The connection was reset” error on the server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM