0

Dear All,

I am opening the excel file to create the object of that excel file using 'Microsoft.Office.Interop.Excel'. It is working fine with file system and local iis server on windows xp operating system. But when running the same code on windows 2003 operating sytem on local iis pagerequest time out error raise.

below is the code which i am using.


public Excel.Sheets m_objSheets = null;
public Excel._Worksheet m_objSheet = null;
public Excel.Range m_objRange = null;
public object m_objOpt = System.Reflection.Missing.Value;


string strFileName = "ICTC_Monthly_Reports123.xls";
string frmImpreg = Server.MapPath("ICTC_Monthly_Reports.xls");
string toImpreg = Server.MapPath("..\\Excel\\" + strFileName);
string filename = frmImpreg;

//code for open excel file
Excel.Application m_objExcel = new Excel.Application();
Excel.Workbook m_objBooks = m_objExcel.Workbooks.Open(filename, 0, true, 5, "", "", true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
m_objSheets = (Excel.Sheets)m_objBooks.Worksheets;
//end code here

waiting for your response, It's urgent

Thanks
Deepak Sonee

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by DeepakSonee
0

Dear All,

I am opening the excel file to create the object of that excel file using 'Microsoft.Office.Interop.Excel'. It is working fine with file system and local iis server on windows xp operating system. But when running the same code on windows 2003 operating sytem on local iis pagerequest time out error raise.

below is the code which i am using.

public Excel.Sheets m_objSheets = null;
public Excel._Worksheet m_objSheet = null;
public Excel.Range m_objRange = null;
public object m_objOpt = System.Reflection.Missing.Value;


string strFileName = "ICTC_Monthly_Reports123.xls";
            string frmImpreg = Server.MapPath("ICTC_Monthly_Reports.xls");
            string toImpreg = Server.MapPath("..\\Excel\\" + strFileName);            
            string filename = frmImpreg;

            //code for open excel file
            Excel.Application m_objExcel = new Excel.Application();
            Excel.Workbook m_objBooks = m_objExcel.Workbooks.Open(filename, 0, true, 5, "", "", true,
                    Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            m_objSheets = (Excel.Sheets)m_objBooks.Worksheets;
            //end code here

waiting for your response, It's urgent

Thanks
Deepak Sonee

Hi Deepak,

If you are trying to export data from DB to Excel try this. It would help you without using the Excel objects.

private void ExportQuestionsToExcel()
{
    string FileNameToExport = "Filename.xls";
    string Delimiter = "\"";
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.Clear();

    // Add the header that specifies the filename and for the Download/SaveAs dialog
    response.AddHeader("Content-Disposition", "attachment;filename=" + FileNameToExport);

    response.ContentType = "Application/x-msexcel";

    string _exportContent = string.Empty;
    DataView _exportData = null;
    DataSet result = new DataSet();

    //Get the resultant from the DB as dataset and store it in result

    result = _exportData == result.Tables[0].DefaultView;

    if (((_exportData != null)) && _exportData.Table.Rows.Count > 0) {
        _exportContent = ConvertDataViewToString(_exportData, Delimiter, Constants.vbTab);
    }

    if (_exportContent.Length <= 0) {
        _exportContent = "Data not found.";
    }

    System.Text.UnicodeEncoding Encoding = new System.Text.UnicodeEncoding();
    response.AddHeader("Content-Length", Encoding.GetByteCount(_exportContent).ToString());
    response.BinaryWrite(Encoding.GetBytes(_exportContent));
    response.Charset = "utf-8";

    // Stop execution of the current page
    response.End();

}

public string ConvertDataViewToString(DataView srcDataView, string Delimiter = null, string Separator = ",")
{

    StringBuilder ResultBuilder = default(StringBuilder);
    ResultBuilder = new StringBuilder();
    ResultBuilder.Length = 0;

    DataColumn aCol = null;
    foreach (DataColumn aCol_loopVariable in srcDataView.Table.Columns) {
        aCol = aCol_loopVariable;
        if ((Delimiter != null) && (Delimiter.Trim().Length > 0)) {
            ResultBuilder.Append(Delimiter);
        }

        ResultBuilder.Append(aCol.ColumnName);

        if ((Delimiter != null) && (Delimiter.Trim().Length > 0)) {
            ResultBuilder.Append(Delimiter);
        }

        ResultBuilder.Append(Separator);
    }

    if (ResultBuilder.Length > Separator.Trim().Length) {
        ResultBuilder.Length = ResultBuilder.Length - Separator.Trim().Length;
    }

    ResultBuilder.Append(Environment.NewLine);

    DataRowView aRow = null;

    foreach (DataRowView aRow_loopVariable in srcDataView) {
        aRow = aRow_loopVariable;
        foreach (DataColumn aCol_loopVariable in srcDataView.Table.Columns) {
            aCol = aCol_loopVariable;
            if ((Delimiter != null) && (Delimiter.Trim().Length > 0)) {
                ResultBuilder.Append(Delimiter);
            }

            ResultBuilder.Append(aRow[aCol.ColumnName]);

            if ((Delimiter != null) && (Delimiter.Trim().Length > 0)) {
                ResultBuilder.Append(Delimiter);
            }

            ResultBuilder.Append(Separator);
        }

        ResultBuilder.Length = ResultBuilder.Length - 1;
        ResultBuilder.Append(Constants.vbNewLine);
    }

    if ((ResultBuilder != null)) {
        return ResultBuilder.ToString();
    } else {
        return string.Empty;
    }

}

Hope this helps...!!!!

Edited by mike_2000_17: Fixed formatting

0

no dear,

My requirement is to edit the cell value of excel file. For that i need to open the excel file through creating object.

Please help

Thanks
Deepak

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.