Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to read an exist excel sheet values and generate text format report using Microsoft.Office.Interop.Excel namespace, when I deploy an ASP.NET web application on IIS 7.5 at windows server 8 r2 I can't access that event after IIS hosting but when I run local machine it'll working fine... kindly give me the solution on this problem? Following my aspx.cs code.


C#
using System.Data;
using System.Reflection;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel; 

public void GetTextValue()
    {

        string sUploadFilePath = string.Empty;
        sUploadFilePath = Server.MapPath(".") + @"\Report\GAD_Model_Template_keivsan1_D2038_00.xls";

        DataSet ds = new DataSet();

        //Office Application Object Declaretion

        Microsoft.Office.Interop.Excel.Application oXL = null;

        //Office workbook object  Declaretion

        Microsoft.Office.Interop.Excel.Workbook oWB = null;

        //Office Worksheet object  Declaretion

        Microsoft.Office.Interop.Excel.Worksheet oSheet = null;

        //Office Range object  Declaretion

        Microsoft.Office.Interop.Excel.Range oRng = null;

        try
        {

            //Create an Application object                   

            oXL = new Microsoft.Office.Interop.Excel.Application();

            //Getting a WorkBook object

            oWB = oXL.Workbooks.Open(sUploadFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            //Getting a WorkSheet object                   

            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets["OUTPUT"];

            //Datatable with dtExcel Name

            System.Data.DataTable dt = new System.Data.DataTable("dtExcel");

            ds.Tables.Add(dt);

            DataRow dr;

            StringBuilder sb = new StringBuilder();

            //Total Column Count

            int jValue = oSheet.UsedRange.Cells.Columns.Count;

            //Total Row count   

            int iValue = oSheet.UsedRange.Cells.Rows.Count;

            for (int j = 1; j <= jValue; j++)
            {

                dt.Columns.Add("column" + j, System.Type.GetType("System.String"));

            }

            //Getting Data in Cell                   

            for (int i = 1; i <= iValue; i++)
            {

                dr = ds.Tables["dtExcel"].NewRow();

                for (int j = 1; j <= jValue; j++)
                {

                    oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];

                    string strValue = oRng.Text.ToString();

                    dr["column" + j] = strValue;

                }

                ds.Tables["dtExcel"].Rows.Add(dr);

            }

            //Release the Excel objects                

            oWB.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

            oXL.Workbooks.Close();

            oXL.Quit();

            oXL = null;

            oWB = null;

            oSheet = null;

            oRng = null;

            GC.GetTotalMemory(false);

            GC.Collect();

            GC.WaitForPendingFinalizers();

            GC.Collect();

            GC.GetTotalMemory(true);

            //Release the Excel objects          

            StringBuilder commaDelimitedText = new StringBuilder();
            commaDelimitedText.AppendLine("GAD Inputs");
            foreach (DataRow row in ds.Tables["dtExcel"].Rows)
            {
                string value = string.Format("{0}", row[0]); // how you format is up to you (spaces, tabs, delimiter, etc)
                commaDelimitedText.AppendLine(value);
            }

            File.WriteAllText("" + Server.MapPath(".") + @"\Report\" + "GADoutput" + ".txt" + "", commaDelimitedText.ToString());
            string txtfilepath = Server.MapPath(".") + @"\Report\" + "GADoutput" + ".txt";
            Session["filepath"] = txtfilepath;

            ScriptManager.RegisterStartupScript(this, typeof(string), "SITE", "alert('Text File write successfully!');", true);
        }
        catch (Exception ex)

        { }

        //return ds;
    }


Thanks and regards,

Parthiban K.
Posted

1 solution

hi,
please check your web applications application pool
 
Share this answer
 
Comments
Parthi_Karnan 16-May-14 0:42am    
Ya i cheeked with your concern It's DefaultAppPool...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900