C# 使用Interop.Excel dll发布Asp.net web应用程序以生成Excel文件

C# 使用Interop.Excel dll发布Asp.net web应用程序以生成Excel文件,c#,asp.net,excel,iis,web-deployment,C#,Asp.net,Excel,Iis,Web Deployment,当我从visual studio运行应用程序时,excel文件是使用代码生成的。但当我发布asp.net web应用程序时,它不会显示excel文件 我所做的事情: 1添加参考:1 Interop.excel.dll2 Interop.Microsoft.Office.Core.dll和Interop.Microsoft.Office.Interop.excel 2更改所有dll的属性以复制local=true。因此dll将添加到bin文件夹中 3I通过附加w3wp进程调试了应用程序,但代码执行

当我从visual studio运行应用程序时,excel文件是使用代码生成的。但当我发布asp.net web应用程序时,它不会显示excel文件

我所做的事情: 1添加参考:1 Interop.excel.dll2 Interop.Microsoft.Office.Core.dll和Interop.Microsoft.Office.Interop.excel

2更改所有dll的属性以复制local=true。因此dll将添加到bin文件夹中

3I通过附加w3wp进程调试了应用程序,但代码执行良好,没有异常或错误,但未生成excel 以下是生成excel的代码:


请告诉我可以做哪些更改,以便它可以在iis上工作。

您可以显示保存excel文件方法的代码吗?是否允许目录的权限写入?否我不保存文件用户如果需要保存文件,文件将自动生成在此行:excel=new excel.Application;excel.Visible=true;当我从visual Studio运行时,服务器端不支持Office automation,使用excel互操作可能复制ASP.Net应用程序不是一个好主意。
 public class ExcelUtlity
    {
        /// <summary>
        /// FUNCTION FOR EXPORT TO EXCEL
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="worksheetName"></param>
        /// <param name="saveAsLocation"></param>
        /// <returns></returns>
        public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType, string fromdate, string todate, string pubicationname)
        {
            Excel.Application excel;
            Excel.Workbook excelworkBook;
            Excel.Worksheet excelSheet;
            Excel.Range excelCellrange = null;

            try
            {
                // Start Excel and get Application object.
                excel = new Excel.Application();

                // for making Excel visible
                excel.Visible = true;
                excel.DisplayAlerts = false;

                // Creation a new Workbook
                excelworkBook = excel.Workbooks.Add(Type.Missing);

                // Workk sheet
                excelSheet = (Excel.Worksheet)excelworkBook.Sheets[1];
                excelSheet.Name = worksheetName;


                excelSheet.Cells[1, 1] = ReporType;
                excelCellrange = excelSheet.get_Range("A1", "C1");
                excelCellrange.Merge(3);
                if (pubicationname != "")
                {
                    excelSheet.Cells[2, 1] = "PublicationName :- " + pubicationname;
                    excelCellrange = excelSheet.get_Range("A2", "C2");
                    excelCellrange.Merge(3);
                }
                else
                {

                }
                excelSheet.Cells[3, 1] = "From Date :- " + fromdate + " To Date :- " + todate;
                excelCellrange = excelSheet.get_Range("A3", "C4");
                excelCellrange.Merge(3);
                // excelSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();

                // loop through each row and add values to our sheet
                int rowcount = 6;

                foreach (DataRow datarow in dataTable.Rows)
                {
                    rowcount += 1;
                    for (int i = 1; i <= dataTable.Columns.Count; i++)
                    {
                        // on the first iteration we add the column headers
                        if (rowcount == 7)
                        {
                            excelSheet.Cells[6, i] = dataTable.Columns[i - 1].ColumnName;
                            excelSheet.Cells.Font.Color = System.Drawing.Color.Black;

                        }

                        excelSheet.Cells[rowcount, i] = datarow[i - 1].ToString();

                        //for alternate rows
                        if (rowcount > 7)
                        {
                            if (i == dataTable.Columns.Count)
                            {
                                if (rowcount % 2 == 0)
                                {
                                    excelCellrange = excelSheet.Range[excelSheet.Cells[rowcount, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
                                    //    FormattingExcelCells(excelCellrange, "#CCCCFF", System.Drawing.Color.Black, false);
                                }

                            }
                        }

                    }

                }

                // now we resize the columns
                excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
                excelCellrange.EntireColumn.AutoFit();
                Excel.Borders border = excelCellrange.Borders;
                border.LineStyle = Excel.XlLineStyle.xlContinuous;
                border.Weight = 2d;


                excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[2, dataTable.Columns.Count]];
                //  FormattingExcelCells(excelCellrange, "#000099", System.Drawing.Color.White, true);


                //now save the workbook and exit Excel


        //        excelworkBook.SaveAs(saveAsLocation);
                //excelworkBook.Close();
                //excel.Quit();
                return true;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                excelSheet = null;
                excelCellrange = null;
                excelworkBook = null;
            }

        }

        /// <summary>
        /// FUNCTION FOR FORMATTING EXCEL CELLS
        /// </summary>
        /// <param name="range"></param>
        /// <param name="HTMLcolorCode"></param>
        /// <param name="fontColor"></param>
        /// <param name="IsFontbool"></param>
        public void FormattingExcelCells(Excel.Range range, string HTMLcolorCode, System.Drawing.Color fontColor, bool IsFontbool)
        {
            range.Interior.Color = System.Drawing.ColorTranslator.FromHtml(HTMLcolorCode);
            range.Font.Color = System.Drawing.ColorTranslator.ToOle(fontColor);
            if (IsFontbool == true)
            {
                range.Font.Bold = IsFontbool;
            }
        }

    }