Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Export to excel working fine my local machine but when i deploy the site on iis server is not working just page is post back.

my code is given below
C#
protected void btn_export_to_excel_Click(object sender, EventArgs e)
{
    Export_To_Excel();
}

public void Export_To_Excel()
{
    Excel.Application xlApp = new Excel.Application();
    object misValue = System.Reflection.Missing.Value;
    Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
    Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    char x = 'A';

    for (int i = 4; i < grd_employee.Columns.Count - 6; i++)
    {
        xlWorkSheet.Cells[1, i - 3] = grd_employee.Columns[i].ToString();
        xlWorkSheet.Range[x + "1"].Font.Bold = true;
        xlWorkSheet.Range[x + "1"].Borders.Color = Color.Black;
        x++;
    }

    int j = 2;
    for (int i = 0; i < grd_employee.Rows.Count; i++)
    {
        x = 'A';
        #region start
        Label lbl_emp_type_Name = (Label)grd_employee.Rows[i].FindControl("lbl_emp_type_Name");
        xlWorkSheet.Cells[j, 1] = lbl_emp_type_Name.Text;
        Label lbl_designation_Name = (Label)grd_employee.Rows[i].FindControl("lbl_designation_Name");
        xlWorkSheet.Cells[j, 2] = lbl_designation_Name.Text;
        Label lbl_company_name = (Label)grd_employee.Rows[i].FindControl("lbl_company_name");
        xlWorkSheet.Cells[j, 3] = lbl_company_name.Text;
        Label lbl_employee_name = (Label)grd_employee.Rows[i].FindControl("lbl_employee_name");
        xlWorkSheet.Cells[j, 4] = lbl_employee_name.Text;
        Label lbl_emp_name_thai = (Label)grd_employee.Rows[i].FindControl("lbl_emp_name_thai");
        xlWorkSheet.Cells[j, 5] = lbl_emp_name_thai.Text;
        Label lbl_emp_last_name = (Label)grd_employee.Rows[i].FindControl("lbl_emp_last_name");
        xlWorkSheet.Cells[j, 6] = lbl_emp_last_name.Text;
        Label lbl_last_name_thai = (Label)grd_employee.Rows[i].FindControl("lbl_last_name_thai");
        xlWorkSheet.Cells[j, 7] = lbl_last_name_thai.Text;
        Label lbl_nick_name = (Label)grd_employee.Rows[i].FindControl("lbl_nick_name");
        xlWorkSheet.Cells[j, 8] = lbl_nick_name.Text;
        Label lbl_permenent_address = (Label)grd_employee.Rows[i].FindControl("lbl_permenent_address");
        xlWorkSheet.Cells[j, 9] = lbl_permenent_address.Text;
        Label lbl_Present_address = (Label)grd_employee.Rows[i].FindControl("lbl_Present_address");
        xlWorkSheet.Cells[j, 10] = lbl_Present_address.Text;
        Label lbl_mobile_no = (Label)grd_employee.Rows[i].FindControl("lbl_mobile_no");
        xlWorkSheet.Cells[j, 11] = lbl_mobile_no.Text;
        Label lbl_telephone_no = (Label)grd_employee.Rows[i].FindControl("lbl_telephone_no");
        xlWorkSheet.Cells[j, 12] = lbl_telephone_no.Text;
        Label lbl_student_date = (Label)grd_employee.Rows[i].FindControl("lbl_student_date");
        xlWorkSheet.Cells[j, 13] = lbl_student_date.Text;
        Label lbl_employement_date = (Label)grd_employee.Rows[i].FindControl("lbl_employement_date");
        xlWorkSheet.Cells[j, 14] = lbl_employement_date.Text;
        Label lbl_date_of_birth = (Label)grd_employee.Rows[i].FindControl("lbl_date_of_birth");
        xlWorkSheet.Cells[j, 15] = lbl_date_of_birth.Text;
        Label lbl_place_of_birth = (Label)grd_employee.Rows[i].FindControl("lbl_place_of_birth");
        xlWorkSheet.Cells[j, 16] = lbl_place_of_birth.Text;
        Label lbl_nationality = (Label)grd_employee.Rows[i].FindControl("lbl_nationality");
        xlWorkSheet.Cells[j, 17] = lbl_nationality.Text;
        Label lbl_religion = (Label)grd_employee.Rows[i].FindControl("lbl_religion");
        xlWorkSheet.Cells[j, 18] = lbl_religion.Text;
        Label lbl_education = (Label)grd_employee.Rows[i].FindControl("lbl_education");
        xlWorkSheet.Cells[j, 19] = lbl_education.Text;
        Label lbl_marital_status = (Label)grd_employee.Rows[i].FindControl("lbl_marital_status");
        xlWorkSheet.Cells[j, 20] = lbl_marital_status.Text;
        Label lbl_id_card_no = (Label)grd_employee.Rows[i].FindControl("lbl_id_card_no");
        xlWorkSheet.Cells[j, 21] = lbl_id_card_no.Text;
        Label lbl_ss_card_no = (Label)grd_employee.Rows[i].FindControl("lbl_ss_card_no");
        xlWorkSheet.Cells[j, 22] = lbl_ss_card_no.Text;
        Label lbl_tax_card_no = (Label)grd_employee.Rows[i].FindControl("lbl_tax_card_no");
        xlWorkSheet.Cells[j, 23] = lbl_tax_card_no.Text;
        Label lbl_barcode_no = (Label)grd_employee.Rows[i].FindControl("lbl_barcode_no");
        xlWorkSheet.Cells[j, 24] = lbl_barcode_no.Text;
        Label lbl_resignd_date = (Label)grd_employee.Rows[i].FindControl("lbl_resignd_date");
        xlWorkSheet.Cells[j, 25] = lbl_resignd_date.Text;
        Label lbl_remarks = (Label)grd_employee.Rows[i].FindControl("lbl_remarks");
        xlWorkSheet.Cells[j, 26] = lbl_remarks.Text;

        x = 'A';
        for (int col = 1; col <= 26; col++)
        {
            string range = x.ToString() + j.ToString();
            xlWorkSheet.Range[range].Font.Color = grd_employee.Rows[i].ForeColor;
            xlWorkSheet.Range[range].Borders.Color = Color.Black;
            xlWorkSheet.Range[range].Font.Bold = true;
            x++;
        }
        j++;
        xlWorkSheet.Columns.AutoFit();
        #endregion
    }
    xlWorkSheet.Columns.AutoFit();
    xlApp.Visible = true;

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);

}

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception)
    {
        obj = null;

    }
    finally
    {
        GC.Collect();
    }
}


please help me

thanks in advanced
Posted
v2
Comments
Is Excel installed on the Server?
pbmehta 30-Dec-13 6:03am    
thanks for giving quick reply ..
no
i don't no how to install excel on server
No you need to make sure that Excel is installed on the Server System. Otherwise your code would not work. So, make sure the Server system has Excel.

1 solution

It seems, this is a web application.
It runs in your local machine as Excel Interop is available with does not exists in web where you are hosting.

This might be possible, if you are using dedicated server but not possible in shared server if not allwed by 3rd party hosting service provider.

Now my suggestion is, when you are building a desktop application using excel interop is fine and works cool. But when you are porting your application in a web, don't use interop/com

Instead use - Gridview Data to Excel. Its super fast and works great.

Here is a url where you can get sample code to do it,
Export Gridview Data to Excel in ASP.NET[^]

Hope this helps.
Cheers
 
Share this answer
 

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