保存Excel文件在IIS 7中不工作,虽然它在本地ASP中工作.网络应用程序

本文关键字:工作 ASP 应用程序 网络 IIS Excel 保存 文件 | 更新日期: 2023-09-27 17:50:40

我试图用数据库中的记录保存excel文件。它在asp.net应用程序中运行良好。但是当我将它移到IIS服务器时,它不起作用。我不知道如何从IIS中调试应用程序。

下面是一些代码:
string str, filename;
        connection = new SqlConnection(con_dm);
        connection.Open();
        str = "SELECT * FROM Person";
        SqlDataAdapter adp = new SqlDataAdapter(str, connection);
        System.Data.DataTable dataTable = new System.Data.DataTable();
        adp.Fill(dataTable);
        if (dataTable.Rows.Count > 0)
        {
            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            oXL = new Excel.Application();
            oXL.Visible = false;
            oXL.SheetsInNewWorkbook = 1;
            oWB = (Excel._Workbook)(oXL.Workbooks.Add());
            oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            try
            {
                string[] colNames = new string[dataTable.Columns.Count];
                int col = 0;
                foreach (DataColumn dc in dataTable.Columns)
                    colNames[col++] = dc.ColumnName;
                char lastColumn = (char)(65 + dataTable.Columns.Count - 1);
                oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
                oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
                oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                DataRow[] dr = dataTable.Select();
                string[,] rowData = new string[dr.Count<DataRow>(), dataTable.Columns.Count + 1];
                int rowCnt = 0;
                foreach (DataRow row in dr)
                {
                    for (col = 0; col < dataTable.Columns.Count; col++)
                    {
                        rowData[rowCnt, col] = row[col].ToString();
                    }
                    rowCnt++;
                }
                rowCnt++;
                oSheet.get_Range("A2", lastColumn + rowCnt.ToString()).Value = rowData;
                oXL.Visible = false;
                oXL.UserControl = true;
                String sNewFolderName = "Report_" + intReportId;
                filename = COMMON_FILE.SAVE_EXCEPTION_FILE_PATH + sNewFolderName + "''" + "Exception_Person" + intReportId + "_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + Extension;
                DirectoryInfo strDirectoryInfo = new DirectoryInfo(COMMON_FILE.SAVE_EXCEPTION_FILE_PATH + sNewFolderName);
                if (!strDirectoryInfo.Exists)
                {
                    strDirectoryInfo.Create();
                }
// Set "Everyone" Permission to Folder
                string redirectionFolder = Convert.ToString(strDirectoryInfo);
                FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow);
                DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
                dirSecurity.AddAccessRule(everyOne);
                Directory.SetAccessControl(redirectionFolder, dirSecurity);
                oSheet.SaveAs(filename);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
                oXL.Quit();
                Marshal.ReleaseComObject(oSheet);
                Marshal.ReleaseComObject(oWB);
                Marshal.ReleaseComObject(oXL);
                oSheet = null;
                oWB = null;
                oXL = null;
                GC.GetTotalMemory(false);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.GetTotalMemory(true);
                The excel is created and opened for insert value. We most close this excel using this system        
                Process[] localByName = Process.GetProcessesByName("EXCEL");
                foreach (Process process in localByName)
                {
                    process.Kill();
                }
            }
            catch (Exception ex)
            {
                // To write log.
                logFile.writeLog(ex.Message.ToString());
            }
            finally
            {   
                Marshal.ReleaseComObject(oWB);
            }
        }
谁来帮帮我吧。由于

保存Excel文件在IIS 7中不工作,虽然它在本地ASP中工作.网络应用程序

将代码放入try catch中并打印异常,在您保存到的路径上归零,与asp.net开发服务器环境相比,它可能在IIS上下文中发生变化

谢谢大家。

它现在工作了。:)

这是因为文件夹权限。

我添加了以下内容:

// Set "Everyone" Permission to Folder 
string redirectionFolder = Convert.ToString(strDirectoryInfo); 
FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", 
FileSystemRights.FullControl, AccessControlType.Allow); DirectorySecurity 
            dirSecurity = new DirectorySecurity(redirectionFolder, 
            AccessControlSections.Group); 
dirSecurity.AddAccessRule(everyOne); 
Directory.SetAccessControl(redirectionFolder, dirSecurity);