导出到Excel无法在iis服务器上运行 [英] Export to Excel not working on iis server

查看:81
本文介绍了导出到Excel无法在iis服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导出到excel工作正常我的本地机器但是当我在iis服务器上部署网站不起作用时,页面会回发。



我的代码如下

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

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

推荐答案

看来,这是一个Web应用程序。

它可以在您的本地计算机上运行,​​因为Excel Interop可用在您托管的网站中不存在。



如果您使用的是专用服务器,但如果不是第三方使用共享服务器则不可能托管服务提供商。



现在我的建议是,当你使用excel interop构建桌面应用程序时很好并且很酷。但是当您在Web中移植应用程序时,请不要使用interop / com



而是使用 - Gridview Data to Excel。它超级快,效果很好。



这是一个网址,你可以在这里找到示例代码,

在ASP.NET中将Gridview数据导出到Excel [ ^ ]



希望这会有所帮助。

干杯
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


这篇关于导出到Excel无法在iis服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆